https://github.com/xotic750/collections-x
ES6 collections fallback library: Map and Set.
https://github.com/xotic750/collections-x
browser collections ecmascript6 es6 map nodejs set
Last synced: 9 months ago
JSON representation
ES6 collections fallback library: Map and Set.
- Host: GitHub
- URL: https://github.com/xotic750/collections-x
- Owner: Xotic750
- License: mit
- Created: 2015-11-28T14:07:35.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2023-01-04T22:52:34.000Z (over 3 years ago)
- Last Synced: 2025-04-30T20:09:48.363Z (about 1 year ago)
- Topics: browser, collections, ecmascript6, es6, map, nodejs, set
- Language: JavaScript
- Homepage:
- Size: 4.7 MB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 21
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## collections-x
ES6 collections fallback library: Map and Set.
- [collections-x](#module_collections-x)
- [`.isMap`](#module_collections-x.isMap) ⇒ boolean
- [`.isSet`](#module_collections-x.isSet) ⇒ boolean
- [`.MapConstructor`](#module_collections-x.MapConstructor)
- [`.clear`](#module_collections-x.MapConstructor+clear) ⇒ Object
- [`.delete`](#module_collections-x.MapConstructor+delete) ⇒ boolean
- [`.entries`](#module_collections-x.MapConstructor+entries) ⇒ Object
- [`.forEach`](#module_collections-x.MapConstructor+forEach) ⇒ Object
- [`.get`](#module_collections-x.MapConstructor+get) ⇒ \*
- [`.keys`](#module_collections-x.MapConstructor+keys) ⇒ Object
- [`.set`](#module_collections-x.MapConstructor+set) ⇒ Object
- [`.size`](#module_collections-x.MapConstructor+size) : number
- [`.values`](#module_collections-x.MapConstructor+values) ⇒ Object
- [`.has(key)`](#module_collections-x.MapConstructor+has) ⇒ boolean
- [`.symIt()`](#module_collections-x.MapConstructor+symIt) ⇒ Object
- [`.SetConstructor`](#module_collections-x.SetConstructor)
- [`.add`](#module_collections-x.SetConstructor+add) ⇒ Object
- [`.clear`](#module_collections-x.SetConstructor+clear) ⇒ Object
- [`.delete`](#module_collections-x.SetConstructor+delete) ⇒ boolean
- [`.forEach`](#module_collections-x.SetConstructor+forEach) ⇒ Object
- [`.size`](#module_collections-x.SetConstructor+size) : number
- [`.entries()`](#module_collections-x.SetConstructor+entries) ⇒ Object
- [`.has(value)`](#module_collections-x.SetConstructor+has) ⇒ boolean
- [`.keys()`](#module_collections-x.SetConstructor+keys) ⇒ Object
- [`.values()`](#module_collections-x.SetConstructor+values) ⇒ Object
- [`.symIt()`](#module_collections-x.SetConstructor+symIt) ⇒ Object
- [`.symIt`](#module_collections-x.symIt)
### `collections-x.isMap` ⇒ boolean
Determine if an `object` is a `Map`.
**Kind**: static property of [collections-x](#module_collections-x)
**Returns**: boolean - `true` if the `object` is a `Map`,
else `false`.
| Param | Type | Description |
| ------ | --------------- | ------------------- |
| object | \* | The object to test. |
**Example**
```js
import {isMap} from 'collections-x';
const m = new MapConstructor();
console.log(isMap([])); // false
console.log(isMap(true)); // false
console.log(isMap(m)); // true
```
### `collections-x.isSet` ⇒ boolean
Determine if an `object` is a `Set`.
**Kind**: static property of [collections-x](#module_collections-x)
**Returns**: boolean - `true` if the `object` is a `Set`,
else `false`.
| Param | Type | Description |
| ------ | --------------- | ------------------- |
| object | \* | The object to test. |
**Example**
```js
import {isSet} from 'collections-x';
const s = new SetConstructor();
console.log(isSet([])); // false
console.log(isSet(true)); // false
console.log(isSet(s)); // true
```
### `collections-x.MapConstructor`
**Kind**: static property of [collections-x](#module_collections-x)
- [`.MapConstructor`](#module_collections-x.MapConstructor)
- [`.clear`](#module_collections-x.MapConstructor+clear) ⇒ Object
- [`.delete`](#module_collections-x.MapConstructor+delete) ⇒ boolean
- [`.entries`](#module_collections-x.MapConstructor+entries) ⇒ Object
- [`.forEach`](#module_collections-x.MapConstructor+forEach) ⇒ Object
- [`.get`](#module_collections-x.MapConstructor+get) ⇒ \*
- [`.keys`](#module_collections-x.MapConstructor+keys) ⇒ Object
- [`.set`](#module_collections-x.MapConstructor+set) ⇒ Object
- [`.size`](#module_collections-x.MapConstructor+size) : number
- [`.values`](#module_collections-x.MapConstructor+values) ⇒ Object
- [`.has(key)`](#module_collections-x.MapConstructor+has) ⇒ boolean
- [`.symIt()`](#module_collections-x.MapConstructor+symIt) ⇒ Object
#### `map.clear` ⇒ Object
The clear() method removes all elements from a Map object.
**Kind**: instance property of [Map](#module_collections-x.MapConstructor)
**Returns**: Object - The Map object.
**Example**
```js
import {MapConstructor} from 'collections-x';
const myMap = new MapConstructor();
myMap.set('bar', 'baz');
myMap.set(1, 'foo');
console.log(myMap.size); // 2
console.log(myMap.has('bar')); // true
myMap.clear();
console.log(myMap.size); // 0
console.log(myMap.has('bar')); // false
```
#### `map.delete` ⇒ boolean
The delete() method removes the specified element from a Map object.
**Kind**: instance property of [Map](#module_collections-x.MapConstructor)
**Returns**: boolean - Returns true if an element in the Map object has been
removed successfully.
| Param | Type | Description |
| ----- | --------------- | ----------------------------------------------------- |
| key | \* | The key of the element to remove from the Map object. |
**Example**
```js
import {MapConstructor} from 'collections-x';
const myMap = new MapConstructor();
myMap.set('bar', 'foo');
myMap.delete('bar'); // Returns true. Successfully removed.
myMap.has('bar'); // Returns false.
// The "bar" element is no longer present.
```
#### `map.entries` ⇒ Object
The entries() method returns a new Iterator object that contains the
[key, value] pairs for each element in the Map object in insertion order.
**Kind**: instance property of [Map](#module_collections-x.MapConstructor)
**Returns**: Object - A new Iterator object.
**Example**
```js
import {MapConstructor} from 'collections-x';
const myMap = new MapConstructor();
myMap.set('0', 'foo');
myMap.set(1, 'bar');
myMap.set({}, 'baz');
const mapIter = myMap.entries();
console.log(mapIter.next().value); // ["0", "foo"]
console.log(mapIter.next().value); // [1, "bar"]
console.log(mapIter.next().value); // [Object, "baz"]
```
#### `map.forEach` ⇒ Object
The forEach() method executes a provided function once per each
key/value pair in the Map object, in insertion order.
**Kind**: instance property of [Map](#module_collections-x.MapConstructor)
**Returns**: Object - The Map object.
| Param | Type | Description |
| --------- | --------------------- | --------------------------------------------- |
| callback | function | Function to execute for each element. |
| [thisArg] | \* | Value to use as this when executing callback. |
**Example**
```js
import {MapConstructor} from 'collections-x';
function logElements(value, key, map) {
console.log('m[' + key + '] = ' + value);
}
const myMap = new MapConstructor([['foo', 3], ['bar', {}], ['baz', undefined]]);
myMap.forEach(logElements);
// logs:
// "m[foo] = 3"
// "m[bar] = [object Object]"
// "m[baz] = undefined"
```
#### `map.get` ⇒ \*
The get() method returns a specified element from a Map object.
**Kind**: instance property of [Map](#module_collections-x.MapConstructor)
**Returns**: \* - Returns the element associated with the specified key or
undefined if the key can't be found in the Map object.
| Param | Type | Description |
| ----- | --------------- | ----------------------------------------------------- |
| key | \* | The key of the element to return from the Map object. |
**Example**
```js
import {MapConstructor} from 'collections-x';
const myMap = new MapConstructor();
myMap.set('bar', 'foo');
myMap.get('bar'); // Returns "foo".
myMap.get('baz'); // Returns undefined.
```
#### `map.keys` ⇒ Object
The keys() method returns a new Iterator object that contains the keys
for each element in the Map object in insertion order.
**Kind**: instance property of [Map](#module_collections-x.MapConstructor)
**Returns**: Object - A new Iterator object.
**Example**
```js
import {MapConstructor} from 'collections-x';
const myMap = new MapConstructor();
myMap.set('0', 'foo');
myMap.set(1, 'bar');
myMap.set({}, 'baz');
const mapIter = myMap.keys();
console.log(mapIter.next().value); // "0"
console.log(mapIter.next().value); // 1
console.log(mapIter.next().value); // Object
```
#### `map.set` ⇒ Object
The set() method adds a new element with a specified key and value to
a Map object.
**Kind**: instance property of [Map](#module_collections-x.MapConstructor)
**Returns**: Object - The Map object.
| Param | Type | Description |
| ----- | --------------- | -------------------------------------------------- |
| key | \* | The key of the element to add to the Map object. |
| value | \* | The value of the element to add to the Map object. |
**Example**
```js
import {MapConstructor} from 'collections-x';
const myMap = new MapConstructor();
// Add new elements to the map
myMap.set('bar', 'foo');
myMap.set(1, 'foobar');
// Update an element in the map
myMap.set('bar', 'fuuu');
```
#### `map.size` : number
The value of size is an integer representing how many entries the Map
object has.
**Kind**: instance property of [Map](#module_collections-x.MapConstructor)
**Example**
```js
import {MapConstructor} from 'collections-x';
const myMap = new MapConstructor();
myMap.set(1, true);
myMap.set(5, false);
myMap.set('some text', 1);
console.log(myMap.size); // 3
```
#### `map.values` ⇒ Object
The values() method returns a new Iterator object that contains the
values for each element in the Map object in insertion order.
**Kind**: instance property of [Map](#module_collections-x.MapConstructor)
**Returns**: Object - A new Iterator object.
**Example**
```js
import {MapConstructor} from 'collections-x';
const myMap = new MapConstructor();
myMap.set('0', 'foo');
myMap.set(1, 'bar');
myMap.set({}, 'baz');
const mapIter = myMap.values();
console.log(mapIter.next().value); // "foo"
console.log(mapIter.next().value); // "bar"
console.log(mapIter.next().value); // "baz"
```
#### `map.has(key)` ⇒ boolean
The has() method returns a boolean indicating whether an element with
the specified key exists or not.
**Kind**: instance method of [Map](#module_collections-x.MapConstructor)
**Returns**: boolean - Returns true if an element with the specified key
exists in the Map object; otherwise false.
| Param | Type | Description |
| ----- | --------------- | -------------------------------------------------------------- |
| key | \* | The key of the element to test for presence in the Map object. |
**Example**
```js
import {MapConstructor} from 'collections-x';
const myMap = new MapConstructor();
myMap.set('bar', 'foo');
myMap.has('bar'); // returns true
myMap.has('baz'); // returns false
```
#### `map.symIt()` ⇒ Object
The initial value of the @@iterator property is the same function object
as the initial value of the entries property.
**Kind**: instance method of [Map](#module_collections-x.MapConstructor)
**Returns**: Object - A new Iterator object.
**Example**
```js
import {MapConstructor, symIt} from 'collections-x';
const myMap = new MapConstructor();
myMap.set('0', 'foo');
myMap.set(1, 'bar');
myMap.set({}, 'baz');
var mapIter = myMap[symIt]();
console.log(mapIter.next().value); // ["0", "foo"]
console.log(mapIter.next().value); // [1, "bar"]
console.log(mapIter.next().value); // [Object, "baz"]
```
### `collections-x.SetConstructor`
**Kind**: static property of [collections-x](#module_collections-x)
- [`.SetConstructor`](#module_collections-x.SetConstructor)
- [`.add`](#module_collections-x.SetConstructor+add) ⇒ Object
- [`.clear`](#module_collections-x.SetConstructor+clear) ⇒ Object
- [`.delete`](#module_collections-x.SetConstructor+delete) ⇒ boolean
- [`.forEach`](#module_collections-x.SetConstructor+forEach) ⇒ Object
- [`.size`](#module_collections-x.SetConstructor+size) : number
- [`.entries()`](#module_collections-x.SetConstructor+entries) ⇒ Object
- [`.has(value)`](#module_collections-x.SetConstructor+has) ⇒ boolean
- [`.keys()`](#module_collections-x.SetConstructor+keys) ⇒ Object
- [`.values()`](#module_collections-x.SetConstructor+values) ⇒ Object
- [`.symIt()`](#module_collections-x.SetConstructor+symIt) ⇒ Object
#### `set.add` ⇒ Object
The add() method appends a new element with a specified value to the end
of a Set object.
**Kind**: instance property of [Set](#module_collections-x.SetConstructor)
**Returns**: Object - The Set object.
| Param | Type | Description |
| ----- | --------------- | ------------------------------------------------------------ |
| value | \* | Required. The value of the element to add to the Set object. |
**Example**
```js
import {SetConstructor} from 'collections-x';
const mySet = new SetConstructor();
mySet.add(1);
mySet.add(5).add('some text'); // chainable
console.log(mySet);
// Set [1, 5, "some text"]
```
#### `set.clear` ⇒ Object
The clear() method removes all elements from a Set object.
**Kind**: instance property of [Set](#module_collections-x.SetConstructor)
**Returns**: Object - The Set object.
**Example**
```js
import {SetConstructor} from 'collections-x';
const mySet = new SetConstructor();
mySet.add(1);
mySet.add('foo');
console.log(mySet.size); // 2
mySet.has('foo'); // true
mySet.clear();
console.log(mySet.size); // 0
mySet.has('bar'); // false
```
#### `set.delete` ⇒ boolean
The delete() method removes the specified element from a Set object.
**Kind**: instance property of [Set](#module_collections-x.SetConstructor)
**Returns**: boolean - Returns true if an element in the Set object has been
removed successfully; otherwise false.
| Param | Type | Description |
| ----- | --------------- | ------------------------------------------------------- |
| value | \* | The value of the element to remove from the Set object. |
**Example**
```js
import {SetConstructor} from 'collections-x';
const mySet = new SetConstructor();
mySet.add('foo');
mySet.delete('bar'); // Returns false. No "bar" element found
//to be deleted.
mySet.delete('foo'); // Returns true. Successfully removed.
mySet.has('foo'); // Returns false. The "foo" element is no
//longer present.
```
#### `set.forEach` ⇒ Object
The forEach() method executes a provided function once per each value
in the Set object, in insertion order.
**Kind**: instance property of [Set](#module_collections-x.SetConstructor)
**Returns**: Object - The Set object.
| Param | Type | Description |
| --------- | --------------------- | --------------------------------------------- |
| callback | function | Function to execute for each element. |
| [thisArg] | \* | Value to use as this when executing callback. |
**Example**
```js
function logSetElements(value1, value2, set) {
console.log('s[' + value1 + '] = ' + value2);
}
new SetConstructor(['foo', 'bar', undefined]).forEach(logSetElements);
// logs:
// "s[foo] = foo"
// "s[bar] = bar"
// "s[undefined] = undefined"
```
#### `set.size` : number
The value of size is an integer representing how many entries the Set
object has.
**Kind**: instance property of [Set](#module_collections-x.SetConstructor)
**Example**
```js
import {SetConstructor} from 'collections-x';
const mySet = new SetConstructor();
mySet.add(1);
mySet.add(5);
mySet.add('some text');
console.log(mySet.size); // 3
```
#### `set.entries()` ⇒ Object
The entries() method returns a new Iterator object that contains an
array of [value, value] for each element in the Set object, in
insertion order. For Set objects there is no key like in Map objects.
However, to keep the API similar to the Map object, each entry has the
same value for its key and value here, so that an array [value, value]
is returned.
**Kind**: instance method of [Set](#module_collections-x.SetConstructor)
**Returns**: Object - A new Iterator object.
**Example**
```js
import {SetConstructor} from 'collections-x';
const mySet = new SetConstructor();
mySet.add('foobar');
mySet.add(1);
mySet.add('baz');
const setIter = mySet.entries();
console.log(setIter.next().value); // ["foobar", "foobar"]
console.log(setIter.next().value); // [1, 1]
console.log(setIter.next().value); // ["baz", "baz"]
```
#### `set.has(value)` ⇒ boolean
The has() method returns a boolean indicating whether an element with the
specified value exists in a Set object or not.
**Kind**: instance method of [Set](#module_collections-x.SetConstructor)
**Returns**: boolean - Returns true if an element with the specified value
exists in the Set object; otherwise false.
| Param | Type | Description |
| ----- | --------------- | ------------------------------------------------- |
| value | \* | The value to test for presence in the Set object. |
**Example**
```js
import {SetConstructor} from 'collections-x';
const mySet = new SetConstructor();
mySet.add('foo');
mySet.has('foo'); // returns true
mySet.has('bar'); // returns false
```
#### `set.keys()` ⇒ Object
The keys() method is an alias for the `values` method (for similarity
with Map objects); it behaves exactly the same and returns values of
Set elements.
**Kind**: instance method of [Set](#module_collections-x.SetConstructor)
**Returns**: Object - A new Iterator object.
**Example**
```js
import {SetConstructor} from 'collections-x';
const mySet = new SetConstructor();
mySet.add('foo');
mySet.add('bar');
mySet.add('baz');
const setIter = mySet.keys();
console.log(setIter.next().value); // "foo"
console.log(setIter.next().value); // "bar"
console.log(setIter.next().value); // "baz"
```
#### `set.values()` ⇒ Object
The values() method returns a new Iterator object that contains the
values for each element in the Set object in insertion order.
**Kind**: instance method of [Set](#module_collections-x.SetConstructor)
**Returns**: Object - A new Iterator object.
**Example**
```js
import {SetConstructor} from 'collections-x';
const mySet = new SetConstructor();
mySet.add('foo');
mySet.add('bar');
mySet.add('baz');
const setIter = mySet.values();
console.log(setIter.next().value); // "foo"
console.log(setIter.next().value); // "bar"
console.log(setIter.next().value); // "baz"
```
#### `set.symIt()` ⇒ Object
The initial value of the @@iterator property is the same function object
as the initial value of the values property.
**Kind**: instance method of [Set](#module_collections-x.SetConstructor)
**Returns**: Object - A new Iterator object.
**Example**
```js
import {SetConstructor, symIt} from 'collections-x';
const mySet = new SetConstructor();
mySet.add('0');
mySet.add(1);
mySet.add({});
const setIter = mySet[symIt]();
console.log(setIter.next().value); // "0"
console.log(setIter.next().value); // 1
console.log(setIter.next().value); // Object
```
### `collections-x.symIt`
The iterator identifier that is in use.
type {Symbol|string}
**Kind**: static property of [collections-x](#module_collections-x)