git ssb

0+

wanderer🌟 / referenceMap



Commit e1f2c200e1e82a761064a31fb0aebf800a54e6ea

add has function

Signed-off-by: wanderer <mjbecze@gmail.com>
wanderer committed on 8/16/2017, 2:04:39 AM
Parent: bc5892a83d0801555575a9330043864310164f8f

Files changed

index.jschanged
tests/index.jschanged
index.jsView
@@ -22,9 +22,9 @@
2222 * @return {*}
2323 */
2424 get (ref) {
2525 const obj = this._map[ref]
26- if (!obj) {
26 + if (obj === undefined) {
2727 throw new Error('invalid referance')
2828 }
2929 return obj
3030 }
@@ -51,5 +51,14 @@
5151 */
5252 get size () {
5353 return this._map.length
5454 }
55 +
56 + /**
57 + * tests wether a given referance is valid or not
58 + * @return {boolean}
59 + */
60 + has (ref) {
61 + const obj = this._map[ref]
62 + return obj !== undefined
63 + }
5564 }
tests/index.jsView
@@ -1,17 +1,19 @@
11 const tape = require('tape')
22 const ReferanceMap = require('../index.js')
33
44 tape('referance mapping', t => {
5- t.plan(7)
5 + t.plan(9)
66 const referanceMap = new ReferanceMap()
77 const obj1 = {}
88 const obj2 = {}
99 const ref1 = referanceMap.add(obj1)
1010 const ref2 = referanceMap.add(obj2)
1111 t.equals(ref1, 0, 'should produce correct refs')
1212 t.equals(ref2, 1, 'should produce correct refs')
1313
14 + t.equals(referanceMap.has(ref1), true, 'should detect if it has the ref')
15 +
1416 const foundObj1 = referanceMap.get(ref1)
1517 const foundObj2 = referanceMap.get(ref2)
1618
1719 t.equals(foundObj1, obj1, 'should get the correct object')
@@ -25,8 +27,10 @@
2527 } catch (e) {
2628 t.true(true, 'should delete refances')
2729 }
2830
31 + t.equals(referanceMap.has(ref1), false, 'should detect if it does not has the ref')
32 +
2933 referanceMap.clear()
3034 try {
3135 referanceMap.get(ref2)
3236 } catch (e) {

Built with git-ssb-web