git ssb

0+

wanderer🌟 / referenceMap



Commit f091b745b725f4df7e1161677370549cb6ff69ed

more informative error message

Maciej Woś committed on 4/5/2018, 8:53:00 PM
Parent: 712a65040746cdf61fd9958556f7327af8bd08c5

Files changed

index.jschanged
tests/index.jschanged
index.jsView
@@ -1,16 +1,16 @@
11 module.exports = class ReferanceMap {
22 /**
3- * Handle mapping arbitary JS object to ints
3 + * Handle mapping arbitrary JS object to ints
44 */
55 constructor () {
66 this.intRefProp = Symbol('int')
77 this._map = []
88 }
99
1010 /**
11- * Adds an object to the referance map returning an int to be used as a
12- * referance
11 + * Adds an object to the reference map returning an int to be used as a
12 + * reference
1313 * @param {*} obj
1414 * @param {*} type - optional
1515 * @return {integer}
1616 */
@@ -23,23 +23,25 @@
2323 return ref
2424 }
2525
2626 /**
27- * gets a POJO given a refernce as an int
27 + * gets a POJO given a reference as an int
2828 * @param {integer} ref
2929 * @param {Object} type - optional
3030 * @return {*}
3131 */
3232 get (ref, typeCheck) {
33- const {obj, type} = this._map[ref]
34- if (obj === undefined || (typeCheck && type !== typeCheck)) {
35- throw new Error('invalid referance')
33 + const result = this._map[ref]
34 + if (result === undefined) {
35 + throw new Error(`invalid reference "${ref}". Object doesn't exist`)
36 + } else if (typeCheck && result.type !== typeCheck) {
37 + throw new Error(`invalid reference "${ref}". Expected type: "${typeCheck}" actual type: "${result.type}"`)
3638 }
37- return obj
39 + return result.obj
3840 }
3941
4042 /**
41- * deletes an object given a referance as an int
43 + * deletes an object given a reference as an int
4244 * @param {integer}
4345 * @return {boolean} whether or not the object was deleted
4446 */
4547 delete (ref) {
@@ -47,9 +49,9 @@
4749 delete this._map[ref]
4850 }
4951
5052 /**
51- * clears the referance map of a objects
53 + * clears the reference map of a objects
5254 */
5355 clear () {
5456 this._map.forEach(el => {
5557 delete el.obj[this.intRefProp]
@@ -57,17 +59,17 @@
5759 this._map = []
5860 }
5961
6062 /**
61- * returns the number of items in the refernace map
63 + * returns the number of items in the reference map
6264 * @return {integer}
6365 */
6466 get size () {
6567 return this._map.length
6668 }
6769
6870 /**
69- * tests wether a given referance is valid or not
71 + * tests whether a given reference is valid or not
7072 * @return {boolean}
7173 */
7274 has (ref) {
7375 const obj = this._map[ref]
tests/index.jsView
@@ -50,9 +50,31 @@
5050 t.true(true, 'should throw if wrong type')
5151 }
5252 })
5353
54 +tape('get failures', t => {
55 + t.plan(2)
56 + const referenceMap = new ReferanceMap()
57 + const obj1 = {}
58 + const obj2 = {}
59 + const ref1 = referenceMap.add(obj1, 'correctType')
60 + const ref2 = referenceMap.add(obj2, 'someType')
61 + try {
62 + referenceMap.get(ref1, 'wrongType')
63 + } catch (e) {
64 + t.equal(e.toString(), `Error: invalid reference "${ref1}". Expected type: "wrongType" actual type: "correctType"`)
65 + }
66 + referenceMap.delete(ref2)
67 + try {
68 + referenceMap.get(ref2)
69 + } catch (e) {
70 + t.equal(e.toString(), `Error: invalid reference "${ref2}". Object doesn't exist`)
71 + }
72 + t.end()
73 +})
74 +
5475 tape('failures', t => {
76 + t.plan(1)
5577 const referanceMap = new ReferanceMap()
5678 const buf = Buffer.from('hello')
5779 referanceMap.add(buf)
5880 referanceMap.clear()

Built with git-ssb-web