Commit b3287b5da8e1c252351f41e6be8999482a580f5e
Merge pull request #7 from lostman/master
More informative error message when `get` failswanderer authored on 4/5/2018, 9:14:32 PM
GitHub committed on 4/5/2018, 9:14:32 PM
Parent: 712a65040746cdf61fd9958556f7327af8bd08c5
Parent: f091b745b725f4df7e1161677370549cb6ff69ed
Files changed
index.js | changed |
tests/index.js | changed |
index.js | ||
---|---|---|
@@ -1,16 +1,16 @@ | ||
1 | 1 … | module.exports = class ReferanceMap { |
2 | 2 … | /** |
3 | - * Handle mapping arbitary JS object to ints | |
3 … | + * Handle mapping arbitrary JS object to ints | |
4 | 4 … | */ |
5 | 5 … | constructor () { |
6 | 6 … | this.intRefProp = Symbol('int') |
7 | 7 … | this._map = [] |
8 | 8 … | } |
9 | 9 … | |
10 | 10 … | /** |
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 | |
13 | 13 … | * @param {*} obj |
14 | 14 … | * @param {*} type - optional |
15 | 15 … | * @return {integer} |
16 | 16 … | */ |
@@ -23,23 +23,25 @@ | ||
23 | 23 … | return ref |
24 | 24 … | } |
25 | 25 … | |
26 | 26 … | /** |
27 | - * gets a POJO given a refernce as an int | |
27 … | + * gets a POJO given a reference as an int | |
28 | 28 … | * @param {integer} ref |
29 | 29 … | * @param {Object} type - optional |
30 | 30 … | * @return {*} |
31 | 31 … | */ |
32 | 32 … | 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}"`) | |
36 | 38 … | } |
37 | - return obj | |
39 … | + return result.obj | |
38 | 40 … | } |
39 | 41 … | |
40 | 42 … | /** |
41 | - * deletes an object given a referance as an int | |
43 … | + * deletes an object given a reference as an int | |
42 | 44 … | * @param {integer} |
43 | 45 … | * @return {boolean} whether or not the object was deleted |
44 | 46 … | */ |
45 | 47 … | delete (ref) { |
@@ -47,9 +49,9 @@ | ||
47 | 49 … | delete this._map[ref] |
48 | 50 … | } |
49 | 51 … | |
50 | 52 … | /** |
51 | - * clears the referance map of a objects | |
53 … | + * clears the reference map of a objects | |
52 | 54 … | */ |
53 | 55 … | clear () { |
54 | 56 … | this._map.forEach(el => { |
55 | 57 … | delete el.obj[this.intRefProp] |
@@ -57,17 +59,17 @@ | ||
57 | 59 … | this._map = [] |
58 | 60 … | } |
59 | 61 … | |
60 | 62 … | /** |
61 | - * returns the number of items in the refernace map | |
63 … | + * returns the number of items in the reference map | |
62 | 64 … | * @return {integer} |
63 | 65 … | */ |
64 | 66 … | get size () { |
65 | 67 … | return this._map.length |
66 | 68 … | } |
67 | 69 … | |
68 | 70 … | /** |
69 | - * tests wether a given referance is valid or not | |
71 … | + * tests whether a given reference is valid or not | |
70 | 72 … | * @return {boolean} |
71 | 73 … | */ |
72 | 74 … | has (ref) { |
73 | 75 … | const obj = this._map[ref] |
tests/index.js | |||
---|---|---|---|
@@ -50,9 +50,31 @@ | |||
50 | 50 … | t.true(true, 'should throw if wrong type') | |
51 | 51 … | } | |
52 | 52 … | }) | |
53 | 53 … | ||
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 … | + | ||
54 | 75 … | tape('failures', t => { | |
76 … | + t.plan(1) | ||
55 | 77 … | const referanceMap = new ReferanceMap() | |
56 | 78 … | const buf = Buffer.from('hello') | |
57 | 79 … | referanceMap.add(buf) | |
58 | 80 … | referanceMap.clear() |
Built with git-ssb-web