Commit 59a99b6e465120b17057b03bebda1ea33087fb45
furst
wanderer committed on 7/24/2017, 2:02:08 AMFiles changed
index.js | added |
package.json | added |
tests/index.js | added |
index.js | |||
---|---|---|---|
@@ -1,0 +1,47 @@ | |||
1 … | +module.exports = class ReferanceMap { | ||
2 … | + /** | ||
3 … | + * Handle mapping arbitary JS object to ints | ||
4 … | + */ | ||
5 … | + constructor () { | ||
6 … | + this._map = [] | ||
7 … | + } | ||
8 … | + | ||
9 … | + /** | ||
10 … | + * Adds an object to the referance map returning an int to be used as a | ||
11 … | + * referance | ||
12 … | + * @param {*} obj | ||
13 … | + * @return {integer} | ||
14 … | + */ | ||
15 … | + add (obj) { | ||
16 … | + return this._map.push(obj) - 1 | ||
17 … | + } | ||
18 … | + | ||
19 … | + /** | ||
20 … | + * gets a POJO given a refernce as an int | ||
21 … | + * @param {integer} ref | ||
22 … | + * @return {*} | ||
23 … | + */ | ||
24 … | + get (ref) { | ||
25 … | + const obj = this._map[ref] | ||
26 … | + if (!obj) { | ||
27 … | + throw new Error('invalid referance') | ||
28 … | + } | ||
29 … | + return obj | ||
30 … | + } | ||
31 … | + | ||
32 … | + /** | ||
33 … | + * deletes an object given a referance as an int | ||
34 … | + * @param {integer} | ||
35 … | + * @return {boolean} whether or not the object was deleted | ||
36 … | + */ | ||
37 … | + delete (ref) { | ||
38 … | + delete this._map[ref] | ||
39 … | + } | ||
40 … | + | ||
41 … | + /** | ||
42 … | + * clears the referance map of a objects | ||
43 … | + */ | ||
44 … | + clear () { | ||
45 … | + this._map = [] | ||
46 … | + } | ||
47 … | +} |
package.json | ||
---|---|---|
@@ -1,0 +1,25 @@ | ||
1 … | +{ | |
2 … | + "name": "referanceMap", | |
3 … | + "version": "0.0.0", | |
4 … | + "description": "This is a simple determinstic map that maps POJOs to an integer", | |
5 … | + "main": "index.js", | |
6 … | + "scripts": { | |
7 … | + "coverage": "node --expose-wasm --harmony ./node_modules/istanbul/lib/cli.js cover ./tests/index.js", | |
8 … | + "coveralls": "npm run coverage && coveralls <coverage/lcov.info", | |
9 … | + "lint": "standard", | |
10 … | + "test": "node --expose-wasm --harmony ./tests/index.js" | |
11 … | + }, | |
12 … | + "author": "mjbecze <mjbecze@gmail.com>", | |
13 … | + "license": "MPL-2.0", | |
14 … | + "standard": { | |
15 … | + "globals": [ | |
16 … | + "WebAssembly" | |
17 … | + ] | |
18 … | + }, | |
19 … | + "keywords": ["map"], | |
20 … | + "devDependencies": { | |
21 … | + "istanbul": "^1.1.0-alpha.1", | |
22 … | + "standard": "^10.0.0", | |
23 … | + "tape": "^4.6.3" | |
24 … | + } | |
25 … | +} |
tests/index.js | ||
---|---|---|
@@ -1,0 +1,33 @@ | ||
1 … | +const tape = require('tape') | |
2 … | +const ReferanceMap = require('../index.js') | |
3 … | + | |
4 … | +tape('referance mapping', t => { | |
5 … | + t.plan(6) | |
6 … | + const referanceMap = new ReferanceMap() | |
7 … | + const obj1 = {} | |
8 … | + const obj2 = {} | |
9 … | + const ref1 = referanceMap.add(obj1) | |
10 … | + const ref2 = referanceMap.add(obj2) | |
11 … | + t.equals(ref1, 0, 'should produce correct refs') | |
12 … | + t.equals(ref2, 1, 'should produce correct refs') | |
13 … | + | |
14 … | + const foundObj1 = referanceMap.get(ref1) | |
15 … | + const foundObj2 = referanceMap.get(ref2) | |
16 … | + | |
17 … | + t.equals(foundObj1, obj1, 'should get the correct object') | |
18 … | + t.equals(foundObj2, obj2, 'should get the correct object') | |
19 … | + | |
20 … | + referanceMap.delete(ref1) | |
21 … | + try { | |
22 … | + referanceMap.get(ref1) | |
23 … | + } catch (e) { | |
24 … | + t.true(true, 'should delete refances') | |
25 … | + } | |
26 … | + | |
27 … | + referanceMap.clear() | |
28 … | + try { | |
29 … | + referanceMap.get(ref2) | |
30 … | + } catch (e) { | |
31 … | + t.true(true, 'should clear refances') | |
32 … | + } | |
33 … | +}) |
Built with git-ssb-web