reverse-iterable-map
Advanced tools
Comparing version
{ | ||
"name": "reverse-iterable-map", | ||
"version": "2.0.1", | ||
"version": "3.0.0", | ||
"description": "A reverse-iterable map implementation based on the built-in Map object", | ||
@@ -5,0 +5,0 @@ "main": "./src/reverse-iterable-map", |
368
README.md
@@ -5,2 +5,4 @@ # reverse-iterable-map | ||
It implements a linked list meaning that each element in the internal data structure (a `Map` object) knows about its previous and next element; thus, allowing iteration in both directions *at the same time*. This implies added memory usage because in addition to its key and value, an element also needs to store the two references for the previous and next elements. | ||
Links: | ||
@@ -28,16 +30,17 @@ | ||
* [Constructor](#constructor) | ||
* [`size`](#size) | ||
* [`clear()`](#clear) | ||
* [`delete()`](#delete) | ||
* [`entries()`](#entries) | ||
* [`forEach()`](#foreach) | ||
* [`forEachReverse()`](#foreachreverse) | ||
* [`get()`](#get) | ||
* [`has()`](#has) | ||
* [`get()`](#get) | ||
* [`iteratorFor()`](#iteratorfor) | ||
* [`keys()`](#keys) | ||
* [`reverseIterator()`](#reverseiterator) | ||
* [`set()`](#set) | ||
* [`setFirst()`](#setfirst) | ||
* [`delete()`](#delete) | ||
* [`forEach()`](#foreach) | ||
* [`forEachReverse()`](#foreachreverse) | ||
* [`[Symbol.iterator]()`](#symboliterator) | ||
* [`reverse()`](#reverse) | ||
* [`entries()`](#entries) | ||
* [`keys()`](#keys) | ||
* [`values()`](#values) | ||
* [`iteratorFor()`](#iteratorfor) | ||
* [Why this was implemented](#why-this-was-implemented) | ||
@@ -158,2 +161,29 @@ * [How to update this package](#how-to-update-this-package) | ||
### `size` | ||
The `size` accessor property returns the number of elements in a `ReverseIterableMap` object. | ||
#### Syntax | ||
``` | ||
map.size | ||
``` | ||
#### Usage | ||
```js | ||
const map = new ReverseIterableMap(); | ||
map | ||
.set('one', 'I') | ||
.set('two', 'lack') | ||
.set('three', 'creativity'); | ||
map.size | ||
//> 3 | ||
``` | ||
### `clear()` | ||
@@ -181,3 +211,3 @@ | ||
### `has()` | ||
### `delete()` | ||
@@ -187,3 +217,3 @@ #### Syntax | ||
``` | ||
map.has(key); | ||
map.delete(key); | ||
``` | ||
@@ -193,7 +223,7 @@ | ||
* **key**: Required. The key of the element to test for presence in the `ReverseIterableMap` object. | ||
* **key**: Required. The key of the element to remove from the `ReverseIterableMap` object. | ||
**Return value**: | ||
* **Boolean**: Returns `true` if an element with the specified key exists in the `ReverseIterableMap` object; otherwise `false`. | ||
* **Boolean**: Returns `true` if an element in the `ReverseIterableMap` object existed and has been removed, or `false` if the element does not exist. | ||
@@ -205,10 +235,10 @@ #### Usage | ||
map.has(0); | ||
//> true | ||
map.delete(0); | ||
//> true (deletes the key value pair [0, 'hey']) | ||
map.has(1); | ||
//> true | ||
map.delete(1); | ||
//> true (deletes the key value pair [1, 'beauty']) | ||
map.has(2); | ||
//> false | ||
map.delete(2); | ||
//> false (key 2 does not exist in map) | ||
``` | ||
@@ -218,41 +248,28 @@ | ||
### `get()` | ||
### `entries()` | ||
Returns an iterator containing the `[key, value]` pairs for each element in the `ReverseIterableMap` object in insertion order. | ||
An iterator containing the same pairs in reverse-insertion order can be obtained with `entries().reverseIterator()`. | ||
#### Syntax | ||
``` | ||
map.get(key); | ||
map.entries(); | ||
``` | ||
**Parameters**: | ||
* **key**: Required. The key of the element to return from the `ReverseIterableMap` object. | ||
**Return value**: | ||
* Returns the element associated with the specified key or `undefined` if the key can't be found in the `ReverseIterableMap` object. | ||
A new `ReverseIterableMap` iterator object. | ||
#### Usage | ||
```js | ||
const map = new ReverseIterableMap(['hey', 'beauty'].entries()); | ||
map.get(0); | ||
//> 'hey' | ||
### `forEach()` | ||
map.get(1); | ||
//> 'beauty' | ||
The `forEach()` method executes a provided function once per each `[key, value]` pair in the `ReverseIterableMap` object, in insertion order. | ||
map.get(2); | ||
//> undefined | ||
``` | ||
### `set()` | ||
#### Syntax | ||
``` | ||
map.set(key, value); | ||
map.forEach(callback[, thisArg]); | ||
``` | ||
@@ -262,39 +279,38 @@ | ||
* **key**: Required. The key of the element to add to the `ReverseIterableMap` object. | ||
* **value**: Required. The value of the element to add to the `ReverseIterableMap` object. | ||
* **callback**: Function to execute for each element. | ||
* **thisArg**: Value to use as `this` when executing `callback`. | ||
**Return value**: | ||
* The `ReverseIterableMap` object. | ||
[`undefined`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined). | ||
#### Usage | ||
```js | ||
const map = new ReverseIterableMap(); | ||
map.set('you', 'beauty'); | ||
//> map | ||
### `forEachReverse()` | ||
map.set('the-magic-key', 'hey'); | ||
//> map | ||
``` | ||
The `forEachReverse()` method executes a provided function once per each `[key, value]` pair in the `ReverseIterableMap` object, in reverse-insertion order. | ||
The `set()` method returns a reference to the map object. This makes the set operation chainable. | ||
#### Syntax | ||
```js | ||
const map = new ReverseIterableMap() | ||
.set('key', '… is spelled like tea') | ||
.set('hey', '… somehow ney'); | ||
``` | ||
map.forEachReverse(callback[, thisArg]); | ||
``` | ||
**Parameters**: | ||
* **callback**: Function to execute for each element. | ||
* **thisArg**: Value to use as `this` when executing `callback`. | ||
### `setFirst()` | ||
**Return value**: | ||
The `setFirst()` method functions like `set()` but uses reverse-insertion order. | ||
[`undefined`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined). | ||
### `get()` | ||
#### Syntax | ||
``` | ||
map.set(key, value); | ||
map.get(key); | ||
``` | ||
@@ -304,8 +320,7 @@ | ||
* **key**: Required. The key of the element to add to the `ReverseIterableMap` object. | ||
* **value**: Required. The value of the element to add to the `ReverseIterableMap` object. | ||
* **key**: Required. The key of the element to return from the `ReverseIterableMap` object. | ||
**Return value**: | ||
* The `ReverseIterableMap` object. | ||
* Returns the element associated with the specified key or `undefined` if the key can't be found in the `ReverseIterableMap` object. | ||
@@ -315,11 +330,12 @@ #### Usage | ||
```js | ||
const map = new ReverseIterableMap() | ||
.setFirst('key1', 'was inserted first') | ||
.setFirst('key2', 'was inserted last'); | ||
const map = new ReverseIterableMap(['hey', 'beauty'].entries()); | ||
map.getFirst(); | ||
//> 'was inserted last' | ||
map.get(0); | ||
//> 'hey' | ||
map.getLast(); | ||
//> 'was inserted first' | ||
map.get(1); | ||
//> 'beauty' | ||
map.get(2); | ||
//> undefined | ||
``` | ||
@@ -329,3 +345,3 @@ | ||
### `delete()` | ||
### `has()` | ||
@@ -335,3 +351,3 @@ #### Syntax | ||
``` | ||
map.delete(key); | ||
map.has(key); | ||
``` | ||
@@ -341,7 +357,7 @@ | ||
* **key**: Required. The key of the element to remove from the `ReverseIterableMap` object. | ||
* **key**: Required. The key of the element to test for presence in the `ReverseIterableMap` object. | ||
**Return value**: | ||
* **Boolean**: Returns `true` if an element in the `ReverseIterableMap` object existed and has been removed, or `false` if the element does not exist. | ||
* **Boolean**: Returns `true` if an element with the specified key exists in the `ReverseIterableMap` object; otherwise `false`. | ||
@@ -353,10 +369,10 @@ #### Usage | ||
map.delete(0); | ||
//> true (deletes the key value pair [0, 'hey']) | ||
map.has(0); | ||
//> true | ||
map.delete(1); | ||
//> true (deletes the key value pair [1, 'beauty']) | ||
map.has(1); | ||
//> true | ||
map.delete(2); | ||
//> false (key 2 does not exist in map) | ||
map.has(2); | ||
//> false | ||
``` | ||
@@ -366,10 +382,14 @@ | ||
### `forEach()` | ||
### `iteratorFor()` | ||
The `forEach()` method executes a provided function once per each `[key, value]` pair in the `ReverseIterableMap` object, in insertion order. | ||
Returns an iterator containing the `[key, value]` pairs for each element in the `ReverseIterableMap` object in insertion order **starting with the pair specified by the `key` parameter**. | ||
This allows starting iteration at a specific element in the map. | ||
An iterator containing the same pairs in reverse-insertion order can be obtained with `iteratorFor().reverseIterator()`. | ||
#### Syntax | ||
``` | ||
map.forEach(callback[, thisArg]); | ||
map.iteratorFor(key); | ||
``` | ||
@@ -379,40 +399,50 @@ | ||
* **callback**: Function to execute for each element. | ||
* **thisArg**: Value to use as `this` when executing `callback`. | ||
* **key**: Required. The key of the element to start iterating from. | ||
**Return value**: | ||
[`undefined`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined). | ||
A new `ReverseIterableMap` iterator object. | ||
#### Usage | ||
```js | ||
const map = new ReverseIterableMap([1, 2, 4].entries()); | ||
### `forEachReverse()` | ||
// Iterator, starting at the element with key 1. | ||
const iterator = map.iteratorFor(1); | ||
The `forEachReverse()` method executes a provided function once per each `[key, value]` pair in the `ReverseIterableMap` object, in reverse-insertion order. | ||
iterator.next().value; | ||
//> [1, 2] | ||
#### Syntax | ||
iterator.next().value; | ||
//> [2, 4] | ||
``` | ||
map.forEachReverse(callback[, thisArg]); | ||
``` | ||
iterator.next().value; | ||
//> undefined | ||
**Parameters**: | ||
// Reverse-iterator, starting at the element with key 1. | ||
const reverseIterator = map.iteratorFor(1).reverseIterator(); | ||
* **callback**: Function to execute for each element. | ||
* **thisArg**: Value to use as `this` when executing `callback`. | ||
reverseIterator.next().value; | ||
//> [1, 2] | ||
**Return value**: | ||
reverseIterator.next().value; | ||
//> [0, 1] | ||
[`undefined`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined). | ||
reverseIterator.next().value; | ||
//> undefined | ||
``` | ||
### `[Symbol.iterator]()` | ||
### `keys()` | ||
Returns the map iterator function. By default, this is the `entries()` function. | ||
Returns an iterator containing the keys for each element in the `ReverseIterableMap` object in insertion order. | ||
An iterator containing the same keys in reverse-insertion order can be obtained with `keys().reverseIterator()`. | ||
#### Syntax | ||
``` | ||
map[Symbol.iterator](); | ||
map.keys(); | ||
``` | ||
@@ -422,34 +452,14 @@ | ||
The map **iterator** function, which is the `entries()` function by default. | ||
A new `ReverseIterableMap` iterator object. | ||
#### Usage | ||
```js | ||
const map = new ReverseIterableMap([1, 2, 4].entries()); | ||
const iterator = map[Symbol.iterator](); | ||
### `reverseIterator()` | ||
iterator.next().value; | ||
//> [0, 1] | ||
In theory, following the semantics of `[Symbol.iterator]()`, this should be `[Symbol.reverseIterator]()`. However, as a developer, I cannot define a well-known symbol myself and make use of it. In the future, the a proposal like [The ReverseIterable Interface, by Lee Byron](https://github.com/leebyron/ecmascript-reverse-iterable) might make it’s way into the specification. For the time being, the `reverseIterator()` function serves the same purpose. | ||
iterator.next().value; | ||
//> [1, 2] | ||
iterator.next().value; | ||
//> [2, 4] | ||
iterator.next().value; | ||
//> undefined | ||
``` | ||
### `reverse()` | ||
In theory, following the semantics of `[Symbol.iterator]()`, this should be `[Symbol.reverseIterator]()`. However, as a developer, I cannot define a well-known symbol myself and make use of it. In the future, the a proposal like [The ReverseIterable Interface, by Lee Byron](https://github.com/leebyron/ecmascript-reverse-iterable) might make it’s way into the specification. For the time being, the `reverse()` function serves the same purpose. | ||
#### Syntax | ||
``` | ||
map.reverse(); | ||
map.reverseIterator(); | ||
``` | ||
@@ -459,3 +469,3 @@ | ||
The map **reverse-iterator** function, which is the `entries().reverse()` function by default. | ||
The map **reverse-iterator** function, which is the `entries().reverseIterator()` function by default. | ||
@@ -467,3 +477,3 @@ #### Usage | ||
const reverseIterator = map.reverse(); | ||
const reverseIterator = map.reverseIterator(); | ||
@@ -485,77 +495,89 @@ reverseIterator.next().value; | ||
### `entries()` | ||
### `set()` | ||
Returns an iterator containing the `[key, value]` pairs for each element in the `ReverseIterableMap` object in insertion order. | ||
An iterator containing the same pairs in reverse-insertion order can be obtained with `entries().reverse()`. | ||
#### Syntax | ||
``` | ||
map.entries(); | ||
map.set(key, value); | ||
``` | ||
**Return value**: | ||
**Parameters**: | ||
A new `ReverseIterableMap` iterator object. | ||
* **key**: Required. The key of the element to add to the `ReverseIterableMap` object. | ||
* **value**: Required. The value of the element to add to the `ReverseIterableMap` object. | ||
**Return value**: | ||
* The `ReverseIterableMap` object. | ||
### `keys()` | ||
#### Usage | ||
Returns an iterator containing the keys for each element in the `ReverseIterableMap` object in insertion order. | ||
```js | ||
const map = new ReverseIterableMap(); | ||
An iterator containing the same keys in reverse-insertion order can be obtained with `keys().reverse()`. | ||
map.set('you', 'beauty'); | ||
//> map | ||
#### Syntax | ||
map.set('the-magic-key', 'hey'); | ||
//> map | ||
``` | ||
map.keys(); | ||
``` | ||
**Return value**: | ||
The `set()` method returns a reference to the map object. This makes the set operation chainable. | ||
A new `ReverseIterableMap` iterator object. | ||
```js | ||
const map = new ReverseIterableMap() | ||
.set('key', '… is spelled like tea') | ||
.set('hey', '… somehow ney'); | ||
``` | ||
### `values()` | ||
### `setFirst()` | ||
Returns an iterator containing the values for each element in the `ReverseIterableMap` object in insertion order. | ||
The `setFirst()` method functions like `set()` but uses reverse-insertion order. | ||
An iterator containing the same values in reverse-insertion order can be obtained with `values().reverse()`. | ||
#### Syntax | ||
``` | ||
map.values(); | ||
map.set(key, value); | ||
``` | ||
**Parameters**: | ||
* **key**: Required. The key of the element to add to the `ReverseIterableMap` object. | ||
* **value**: Required. The value of the element to add to the `ReverseIterableMap` object. | ||
**Return value**: | ||
A new `ReverseIterableMap` iterator object. | ||
* The `ReverseIterableMap` object. | ||
#### Usage | ||
```js | ||
const map = new ReverseIterableMap() | ||
.setFirst('key1', 'was inserted first') | ||
.setFirst('key2', 'was inserted last'); | ||
### `iteratorFor()` | ||
map.values().next().value; | ||
//> 'was inserted last' | ||
Returns an iterator containing the `[key, value]` pairs for each element in the `ReverseIterableMap` object in insertion order **starting with the pair specified by the `key` parameter**. | ||
map.values().reverseIterator().next().value; | ||
//> 'was inserted first' | ||
``` | ||
This allows starting iteration at a specific element in the map. | ||
An iterator containing the same pairs in reverse-insertion order can be obtained with `iteratorFor().reverse()`. | ||
### `[Symbol.iterator]()` | ||
Returns the map iterator function. By default, this is the `entries()` function. | ||
#### Syntax | ||
``` | ||
map.iteratorFor(key); | ||
map[Symbol.iterator](); | ||
``` | ||
**Parameters**: | ||
* **key**: Required. The key of the element to start iterating from. | ||
**Return value**: | ||
A new `ReverseIterableMap` iterator object. | ||
The map **iterator** function, which is the `entries()` function by default. | ||
@@ -567,6 +589,8 @@ #### Usage | ||
// Iterator, starting at the element with key 1. | ||
const iterator = map.iteratorFor(1); | ||
const iterator = map[Symbol.iterator](); | ||
iterator.next().value; | ||
//> [0, 1] | ||
iterator.next().value; | ||
//> [1, 2] | ||
@@ -579,18 +603,24 @@ | ||
//> undefined | ||
``` | ||
// Reverse-iterator, starting at the element with key 1. | ||
const reverseIterator = map.iteratorFor(1).reverse(); | ||
reverseIterator.next().value; | ||
//> [1, 2] | ||
reverseIterator.next().value; | ||
//> [0, 1] | ||
### `values()` | ||
reverseIterator.next().value; | ||
//> undefined | ||
Returns an iterator containing the values for each element in the `ReverseIterableMap` object in insertion order. | ||
An iterator containing the same values in reverse-insertion order can be obtained with `values().reverseIterator()`. | ||
#### Syntax | ||
``` | ||
map.values(); | ||
``` | ||
**Return value**: | ||
A new `ReverseIterableMap` iterator object. | ||
## Why this was implemented | ||
@@ -597,0 +627,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
50820
2.14%878
0.11%657
4.78%