index.jsView |
---|
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 … | 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 | 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 | 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] |