This module is an utility for managing data in the form of Map.
Prerequisites :
Node.js 12.0.0 or newer is required.
With npm :
npm install collection-data
With yarn :
yarn add collection-data
Import the module from node_modules :
With CommonJS syntax :
const { Collection } = require("collection-data");
With module syntax :
import { Collection } = from 'collection-data';
Create a new Collection with optional data has a parameter.
Parameters :
- data : Array of data to be added to the collection.
- type : Array<Array<any, data>>
- optional : Yes
const collection = new Collection([
["key1", value1],
["key2", value2],
["key3", value3],
]);
Get a value from the collection by name of key.
Parameters :
- key : The name of the key to get value from the collection (any).
Returns :
The value of the key in the collection (type : any
| undefined
).
collection.get("key1");
Set a value in the collection.
Parameters :
- key : The name of the key to add (any).
- value : The value to set (any).
Returns :
The Collection (type : Collection
).
collection.set("key2", value2);
Check if a key exists in the collection.
Parameters :
- key : The key to check (any).
Returns :
True if the key exists in the collection (type : boolean
).
collection.has("key1"); // true
Check if all keys exists in the collection.
Parameters :
- keys : The name of the keys to check (Array<any>).
Returns :
True if the keys exists in the collection (type : boolean
).
collection.hasAllKeys(["key1", "key2"]);
Check if at least one key exists in the collection.
Parameters :
- keys : The name of the keys to check (Array<any>).
Returns :
True if one key exists in the collection (type : boolean
).
collection.hasAnyKey(["key1", "key2"]);
Delete a value from the collection by name of key.
Parameters :
- key : The name of the key to delete from the collection (any).
Returns :
True if the key was deleted (type : boolean
).
collection.delete("key1");
Clear all values from the collection.
Parameters :
None
Returns :
Void (type : void
).
collection.clear();
Update value from the collection if the filter returns true for at least one item of collection.
Parameters :
- data : Object with a key and value keys (key of item and newValue).
- filter : Function with in parameters key and value.
Returns :
The collection if it was updated and null otherwise (type : Collection
| null
).
const filter = (k, v) => k === "key1";
collection.updateIf({ key: "key1", value: newValue }, filter);
Delete value from the collection if the filter returns true for at least one item of collection.
Parameters :
- key : Object with a key and value keys (key of item and newValue) (any).
- filter : Function with in parameters key and value (Function).
Returns :
The collection if data was deleted and null otherwise (type : Collection
| null
).
const filter = (k, v) => k === "key1";
collection.deleteIf("key1", filter);
Check if in the collection an othen element have the value of item of key.
Parameters :
- key : The name of the key to check (any).
Returns :
True if value of this item is unique in the collection (type : boolean
).
collection.isUnique("key1");
Get the key of first element of the collection.
Parameters :
None
Returns :
The key of the first element in the collection (type : any
| null
).
collection.firstKey();
Get the first element of the collection.
Parameters :
None
Returns :
The value of the first element in the collection (type : any
| null
).
collection.first();
Get the key of last element of the collection.
Parameters :
None
Returns :
The key of the last element in the collection (type : any
| null
).
collection.lastKey();
Get the last element of the collection.
Parameters :
None
Returns :
The value of the last element in the collection (type : any
| null
).
collection.last();
Get a random key from the collection.
Parameters :
None
Returns :
The random key of the collection (type : any
| null
).
collection.randomKey();
Get a random value from the collection.
Parameters :
None
Returns :
The random value of the collection (type : any
| null
).
collection.random();
Search value in collection.
Parameters :
- function : The function to search with value and key as a parameter.
Returns :
The value found (type : any
| null
).
collection.find((v) => v.property == value);
Check if the collection is empty.
Parameters :
None
Returns :
True if the collection is empty (type : boolean
).
collection.isEmpty();
Get a copie of the collection.
Parameters :
None
Returns :
The collection copied (type : Collection
).
collection.copy();
Return the values where the function returns true in the collection.
Parameters :
- function : Function with in parameters the value and the key.
Returns :
A collection with the items filtered (type : Collection
).
collection.filter((v, k) => {
return v.property === other;
});
Get a list of keys in the collection.
Parameters :
None
Returns :
An arrays of keys of the collection (type : Array<K>
| null
).
collection.keysList();
Get a list of values in the collection.
Parameters :
None
Returns :
An arrays of values of the collection (type : Array<V>
| null
).
collection.valuesList();
Merge an other collection or Map with this collection.
Parameters :
- other : A collection or a Map
Returns :
The new collection merged (type : Collection
).
collection.merge(other);
Concat map or collection to this collection.
Parameters :
- other : A collection or a Map
None
Returns :
The collection (type : Collection
)
collection.concat(other);
Convert the collection to an array.
Parameters :
None
Returns :
An arrays of the collection (type : Array<{key:string, value:any}>
| null
).
collection.toArray();
Convert the collection to json.
Parameters :
None
Returns :
The collection as json (type : Object
).
collection.toJSON();
Convert the collection to json.
Parameters :
- function : Function to execute for each element (Function).
Returns :
The collection (type : Collection
).
collection.each((v) => console.log(v));