-
-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
191 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* Mnemonist DefaultWeakMap Typings | ||
* ================================ | ||
*/ | ||
export default class DefaultWeakMap<K extends object, V> { | ||
|
||
// Constructor | ||
constructor(factory: (key: K) => V); | ||
|
||
// Methods | ||
clear(): void; | ||
set(key: K, value: V): this; | ||
delete(key: K): boolean; | ||
has(key: K): boolean; | ||
get(key: K): V; | ||
peek(key: K): V | undefined; | ||
inspect(): any; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/** | ||
* Mnemonist DefaultWeakMap | ||
* ======================== | ||
* | ||
* JavaScript implementation of a default weak map that will return a constructed | ||
* value any time one tries to access an non-existing key. It is similar to | ||
* DefaultMap but uses ES6 WeakMap that only holds weak reference to keys. | ||
*/ | ||
|
||
/** | ||
* DefaultWeakMap. | ||
* | ||
* @constructor | ||
*/ | ||
function DefaultWeakMap(factory) { | ||
if (typeof factory !== 'function') | ||
throw new Error('mnemonist/DefaultWeakMap.constructor: expecting a function.'); | ||
|
||
this.items = new WeakMap(); | ||
this.factory = factory; | ||
} | ||
|
||
/** | ||
* Method used to clear the structure. | ||
* | ||
* @return {undefined} | ||
*/ | ||
DefaultWeakMap.prototype.clear = function() { | ||
|
||
// Properties | ||
this.items = new WeakMap(); | ||
}; | ||
|
||
/** | ||
* Method used to get the value set for given key. If the key does not exist, | ||
* the value will be created using the provided factory. | ||
* | ||
* @param {any} key - Target key. | ||
* @return {any} | ||
*/ | ||
DefaultWeakMap.prototype.get = function(key) { | ||
var value = this.items.get(key); | ||
|
||
if (typeof value === 'undefined') { | ||
value = this.factory(key); | ||
this.items.set(key, value); | ||
} | ||
|
||
return value; | ||
}; | ||
|
||
/** | ||
* Method used to get the value set for given key. If the key does not exist, | ||
* a value won't be created. | ||
* | ||
* @param {any} key - Target key. | ||
* @return {any} | ||
*/ | ||
DefaultWeakMap.prototype.peek = function(key) { | ||
return this.items.get(key); | ||
}; | ||
|
||
/** | ||
* Method used to set a value for given key. | ||
* | ||
* @param {any} key - Target key. | ||
* @param {any} value - Value. | ||
* @return {DefaultMap} | ||
*/ | ||
DefaultWeakMap.prototype.set = function(key, value) { | ||
this.items.set(key, value); | ||
return this; | ||
}; | ||
|
||
/** | ||
* Method used to test the existence of a key in the map. | ||
* | ||
* @param {any} key - Target key. | ||
* @return {boolean} | ||
*/ | ||
DefaultWeakMap.prototype.has = function(key) { | ||
return this.items.has(key); | ||
}; | ||
|
||
/** | ||
* Method used to delete target key. | ||
* | ||
* @param {any} key - Target key. | ||
* @return {boolean} | ||
*/ | ||
DefaultWeakMap.prototype.delete = function(key) { | ||
return this.items.delete(key); | ||
}; | ||
|
||
/** | ||
* Convenience known methods. | ||
*/ | ||
DefaultWeakMap.prototype.inspect = function() { | ||
return this.items; | ||
}; | ||
|
||
if (typeof Symbol !== 'undefined') | ||
DefaultWeakMap.prototype[Symbol.for('nodejs.util.inspect.custom')] = DefaultWeakMap.prototype.inspect; | ||
|
||
/** | ||
* Exporting. | ||
*/ | ||
module.exports = DefaultWeakMap; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* Mnemonist DefaultWeakMap Unit Tests | ||
* =================================== | ||
*/ | ||
var assert = require('assert'), | ||
DefaultWeakMap = require('../default-weak-map.js'); | ||
|
||
var FACTORY = function() { | ||
return []; | ||
}; | ||
|
||
describe('DefaultWeakMap', function() { | ||
|
||
it('should throw if passed factory is not a function.', function() { | ||
|
||
assert.throws(function() { | ||
new DefaultWeakMap(null); | ||
}, /function/); | ||
}); | ||
|
||
it('should be possible to set & get keys.', function() { | ||
var map = new DefaultWeakMap(FACTORY); | ||
const one = {}, two = {}, unknown = {}; | ||
|
||
map.get(one).push(1); | ||
map.set(two, [2]); | ||
|
||
assert.deepEqual(map.get(one), [1]); | ||
assert.deepEqual(map.get(two), [2]); | ||
|
||
assert.deepEqual(map.get(unknown), []); | ||
|
||
map.clear(); | ||
|
||
assert.deepEqual(map.get(one), []); | ||
}); | ||
|
||
it('should be possible to delete keys.', function() { | ||
var map = new DefaultWeakMap(FACTORY); | ||
const one = {}; | ||
|
||
map.set(one, 1); | ||
|
||
assert.strictEqual(map.has(one), true); | ||
assert.strictEqual(map.delete(one), true); | ||
assert.strictEqual(map.has(one), false); | ||
assert.strictEqual(map.delete(one), false); | ||
}); | ||
|
||
it('should be possible to peek.', function() { | ||
var map = new DefaultWeakMap(FACTORY); | ||
const one = {}, two = {}; | ||
|
||
map.get(one).push(1); | ||
|
||
assert.deepEqual(map.peek(one), [1]); | ||
assert.strictEqual(map.peek(two), undefined); | ||
assert.strictEqual(map.has(two), false); | ||
}); | ||
}); |