reverse-iterable-map
Advanced tools
Comparing version
@@ -87,19 +87,15 @@ "use strict"; | ||
/** | ||
* The `add()` method adds a new element to the internal `Map` object. It does not link itself | ||
* with its neighboring elements which is why this method must never be called directly. | ||
* Updates a node’s value if one exists for the given key. | ||
* | ||
* @param key The key of the element to add to the `ReverseIterableMap` object. | ||
* @param value The value of the element to add to the `ReverseIterableMap` object. | ||
* @returns the `ReverseIterableMapNode` object. | ||
* @param key The key of the element to update. | ||
* @param value The new value to set on the element. | ||
* @returns `true` if a node was present and updated; `false` otherwise. | ||
*/ | ||
_add(key, value) { | ||
let node = this._map.get(key); | ||
_updateExistingNode(key, value) { | ||
const node = this._map.get(key); | ||
if (node !== undefined) { | ||
node.value = value; | ||
return true; | ||
} | ||
else { | ||
node = new ReverseIterableMapNode(key, value); | ||
this._map.set(key, node); | ||
} | ||
return node; | ||
return false; | ||
} | ||
@@ -115,3 +111,7 @@ /** | ||
set(key, value) { | ||
const node = this._add(key, value); | ||
if (this._updateExistingNode(key, value)) { | ||
return this; | ||
} | ||
const node = new ReverseIterableMapNode(key, value); | ||
this._map.set(key, node); | ||
if (this._lastNode !== null) { | ||
@@ -136,3 +136,7 @@ node.prevNode = this._lastNode; | ||
setFirst(key, value) { | ||
const node = this._add(key, value); | ||
if (this._updateExistingNode(key, value)) { | ||
return this; | ||
} | ||
const node = new ReverseIterableMapNode(key, value); | ||
this._map.set(key, node); | ||
if (this._firstNode !== null) { | ||
@@ -139,0 +143,0 @@ node.nextNode = this._firstNode; |
{ | ||
"name": "reverse-iterable-map", | ||
"description": "A reverse-iterable map implementation based on the built-in Map object", | ||
"version": "4.0.1", | ||
"version": "4.0.2", | ||
"repository": "kleinfreund/reverse-iterable-map", | ||
@@ -19,3 +19,5 @@ "author": { | ||
"scripts": { | ||
"prepublishOnly": "npm test", | ||
"prepare": "npm run build", | ||
"test": "ava tests/*.js", | ||
"build": "npm run build:esm && npm run build:cjs", | ||
@@ -25,5 +27,13 @@ "build:esm": "tsc --module es2015 --outDir dist/esm && npm run rename-to-mjs", | ||
"rename-to-mjs": "mv dist/esm/reverse-iterable-map.js dist/esm/reverse-iterable-map.mjs", | ||
"test": "node --experimental-modules tests/tests.mjs", | ||
"examples": "http-server" | ||
}, | ||
"ava": { | ||
"compileEnhancements": false, | ||
"extensions": [ | ||
"ts" | ||
], | ||
"require": [ | ||
"ts-node/register" | ||
] | ||
}, | ||
"keywords": [ | ||
@@ -40,5 +50,7 @@ "map", | ||
"devDependencies": { | ||
"ava": "^1.1.0", | ||
"http-server": "^0.11.1", | ||
"ts-node": "^8.0.2", | ||
"typescript": "^3.2.4" | ||
} | ||
} |
@@ -114,6 +114,6 @@ # reverse-iterable-map | ||
*(requires Node.js version 8.5 or higher for experimental ES module support)* | ||
In order to run the tests, clone the repository and run the following: | ||
```sh | ||
npm test | ||
npm install && npm test | ||
``` | ||
@@ -120,0 +120,0 @@ |
@@ -19,3 +19,3 @@ /** | ||
*/ | ||
constructor(iterable?: Iterable<[K, V]>) { | ||
constructor(iterable?: Iterable<[K, V]> | Array<Array<any>>) { | ||
this._map = new Map(); | ||
@@ -98,21 +98,17 @@ this._firstNode = null; | ||
/** | ||
* The `add()` method adds a new element to the internal `Map` object. It does not link itself | ||
* with its neighboring elements which is why this method must never be called directly. | ||
* Updates a node’s value if one exists for the given key. | ||
* | ||
* @param key The key of the element to add to the `ReverseIterableMap` object. | ||
* @param value The value of the element to add to the `ReverseIterableMap` object. | ||
* @returns the `ReverseIterableMapNode` object. | ||
* @param key The key of the element to update. | ||
* @param value The new value to set on the element. | ||
* @returns `true` if a node was present and updated; `false` otherwise. | ||
*/ | ||
private _add(key: K, value: V): ReverseIterableMapNode<K, V> { | ||
let node = this._map.get(key); | ||
private _updateExistingNode(key: K, value: V) { | ||
const node = this._map.get(key); | ||
if (node !== undefined) { | ||
node.value = value; | ||
} else { | ||
node = new ReverseIterableMapNode(key, value); | ||
this._map.set(key, node); | ||
return true; | ||
} | ||
return node; | ||
return false; | ||
} | ||
@@ -129,4 +125,9 @@ | ||
set(key: K, value: V): this { | ||
const node = this._add(key, value); | ||
if (this._updateExistingNode(key, value)) { | ||
return this; | ||
} | ||
const node = new ReverseIterableMapNode(key, value); | ||
this._map.set(key, node); | ||
if (this._lastNode !== null) { | ||
@@ -155,4 +156,9 @@ node.prevNode = this._lastNode; | ||
setFirst(key: K, value: V): this { | ||
const node = this._add(key, value); | ||
if (this._updateExistingNode(key, value)) { | ||
return this; | ||
} | ||
const node = new ReverseIterableMapNode(key, value); | ||
this._map.set(key, node); | ||
if (this._firstNode !== null) { | ||
@@ -271,3 +277,3 @@ node.nextNode = this._firstNode; | ||
entries(): ReverseIterableIterator<[K, V]> { | ||
const getIteratorValue = (node: ReverseIterableMapNode<K, V>) => [node.key, node.value]; | ||
const getIteratorValue = (node: ReverseIterableMapNode<K, V>): [K, V] => [node.key, node.value]; | ||
@@ -283,4 +289,4 @@ return this._iterableIterator(getIteratorValue); | ||
*/ | ||
keys(): ReverseIterableIterator<[K, V]> { | ||
const getIteratorValue = (node: ReverseIterableMapNode<K, V>) => node.key; | ||
keys(): ReverseIterableIterator<K> { | ||
const getIteratorValue = (node: ReverseIterableMapNode<K, V>): K => node.key; | ||
@@ -296,4 +302,4 @@ return this._iterableIterator(getIteratorValue); | ||
*/ | ||
values(): ReverseIterableIterator<[K, V]> { | ||
const getIteratorValue = (node: ReverseIterableMapNode<K, V>) => node.value; | ||
values(): ReverseIterableIterator<V> { | ||
const getIteratorValue = (node: ReverseIterableMapNode<K, V>): V => node.value; | ||
@@ -312,5 +318,5 @@ return this._iterableIterator(getIteratorValue); | ||
*/ | ||
iteratorFor(key: K): IterableIterator<[K, V]> { | ||
iteratorFor(key: K): ReverseIterableIterator<[K, V]> { | ||
let startNode = this._map.get(key); | ||
const getIteratorValue = (node: ReverseIterableMapNode<K, V>) => [node.key, node.value]; | ||
const getIteratorValue = (node: ReverseIterableMapNode<K, V>): [K, V] => [node.key, node.value]; | ||
@@ -339,3 +345,6 @@ return this._iterableIterator(getIteratorValue, startNode); | ||
*/ | ||
private _iterableIterator(getIteratorValue: Function, startNode?: ReverseIterableMapNode<K, V>): ReverseIterableIterator<[K, V]> { | ||
private _iterableIterator( | ||
getIteratorValue: (node: ReverseIterableMapNode<K, V>) => [K, V] | K | V, | ||
startNode?: ReverseIterableMapNode<K, V> | ||
): ReverseIterableIterator<any> { | ||
// Store `this._lastNode` because inside the `reverseIterator()` method, `this` will be | ||
@@ -398,3 +407,3 @@ // bound to the `_iterableIterator` method, not the `ReverseIterableMap` object. | ||
*/ | ||
function iteratorResult(value: any): IteratorResult<any> { | ||
function iteratorResult<T>(value: T): IteratorResult<T> { | ||
return { | ||
@@ -401,0 +410,0 @@ value: value, |
Sorry, the diff of this file is not supported yet
58112
0.58%1068
1.52%4
100%