Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
collections-x
Advanced tools
ES6 collections fallback library: Map and Set.
.isMap
⇒ boolean
.isSet
⇒ boolean
.MapConstructor
.SetConstructor
.add
⇒ Object
.clear
⇒ Object
.delete
⇒ boolean
.forEach
⇒ Object
.size
: number
.entries()
⇒ Object
.has(value)
⇒ boolean
.keys()
⇒ Object
.values()
⇒ Object
.symIt()
⇒ Object
.symIt
collections-x.isMap
⇒ boolean
Determine if an object
is a Map
.
Kind: static property of collections-x
Returns: boolean
- true
if the object
is a Map
,
else false
.
Param | Type | Description |
---|---|---|
object | * | The object to test. |
Example
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
Returns: boolean
- true
if the object
is a Set
,
else false
.
Param | Type | Description |
---|---|---|
object | * | The object to test. |
Example
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
map.clear
⇒ Object
The clear() method removes all elements from a Map object.
Kind: instance property of Map
Returns: Object
- The Map object.
Example
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
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
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
Returns: Object
- A new Iterator object.
Example
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
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
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
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
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
Returns: Object
- A new Iterator object.
Example
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
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
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
Example
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
Returns: Object
- A new Iterator object.
Example
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
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
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
Returns: Object
- A new Iterator object.
Example
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
.SetConstructor
.add
⇒ Object
.clear
⇒ Object
.delete
⇒ boolean
.forEach
⇒ Object
.size
: number
.entries()
⇒ Object
.has(value)
⇒ boolean
.keys()
⇒ Object
.values()
⇒ Object
.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
Returns: Object
- The Set object.
Param | Type | Description |
---|---|---|
value | * | Required. The value of the element to add to the Set object. |
Example
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
Returns: Object
- The Set object.
Example
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
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
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
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
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
Example
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
Returns: Object
- A new Iterator object.
Example
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
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
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
Returns: Object
- A new Iterator object.
Example
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
Returns: Object
- A new Iterator object.
Example
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
Returns: Object
- A new Iterator object.
Example
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
FAQs
ES6 collections fallback library: Map and Set.
The npm package collections-x receives a total of 243 weekly downloads. As such, collections-x popularity was classified as not popular.
We found that collections-x demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.