The Collection class is similar to the Map class, but also includes methods that can be advantageous.
- ceyrex (Ceyhun): Added typings.
This is a node.js module available through the npm registry. Before installing, download and install Node.js. Node.js v12.4.0 or later is required (tested and recommended v14.16.0 or later).
npm install jsollection
yarn install jsollection
git clone https://github.com/MustafaTRK/jsollection.git
MustafaTRK/jsollection - GitHub Archive
// ES5
const jsollection = require("jsollection");
// Direct import collection class
const { Collection } = require("jsollection");
// ES6
import jsollection from "jsollection";
// Direct import collection class
import { Collection } from "jsollection";
let myArray = [["key", "value"], ["second", "strip"]];
const myCollection = jsollection.Collection.fromArray(myArray);
let myMap = new Map();
myMap.set("key", "value");
const myCollection = jsollection.Collection.fromMap(myMap);
const myCollection = new jsollection.Collection();
// or if the class was imported
const myCollection = new Collection();
// Array entries
let myArray = [["key", "value"], ["second", "strip"]];
const myCollection = new jsollection.Collection(myArray);
// Map entries
let myMap = new Map();
myMap.set("key", "value");
const myCollection = new jsollection.Collection(map);
myCollection.set("key", "value");
myCollection.set(1453, { id: 1453, name: "Fatih" });
myCollection.get(1453); // { id: 1453, name: 'Fatih' }
myCollection.get("randomly"); // undefined
myCollection.has("key"); // true
myCollection.has("randomly"); // false
myCollection.size(); // 2
myCollection.delete("chicken"); // false
myCollection.delete("key"); // true
myCollection.delete("bird", 2000)
.then(collection => console.log(collection.size()))
.catch(error => console.error(error)); // Key (bird) not exist in the collection!
myCollection.delete(1453, 2000)
.then(collection => console.log(collection.size())) // 0
.catch(error => console.error(error));
Note: In the rest of the examples, it will be treated as not deleted.
myCollection.clear(); // 0
Note: In the rest of the examples, it will be treated as not deleted.
let secondCollection = myCollection.clone();
myCollection.keys(); // ['key', 1453]
myCollection.keys(); // ['value', 1453]
myCollection.random(0); // []
myCollection.random(); // 'value'
myCollection.random(4); // [{ id: 1453, name: 'Fatih' }, 'value', undefined, undefined]
myCollection.randomKey(0); // []
myCollection.randomKey(); // 1453
myCollection.randomKey(4); // ['key', 1453, undefined, undefined]
myCollection.find((value, key, collection) => typeof value === "object"); // { id: 1453, name: 'Fatih' }
myCollection.find((value, key, collection) => typeof key === "boolean"); // undefined
myCollection.find(function(value) {
return value === this.xyz();
}, { xyz: () => "awesomeUsername" }); // undefined
myCollection.findKey((value, key, collection) => collection.get(key) !== value); // undefined
myCollection.findKey(function(value) {
return typeof value === "object" && value["name"] === this.getGrand();
}, { getGrand: () => "Fatih" }); // 1453
Note: It will be considered that there are several unspecified set processes.
myCollection.filter((value, key, collection) => typeof key === "number"); // { 1453 => { id: 1453, name: 'Fatih' }, 2021 => 'incredible year' }
myCollection.filter(function(value, key) { return String(key).includes(this.size()); }); // { 1453 => { id: 1453, name: 'Fatih' } }
myCollection.filter((value, key, collection) => typeof key === "number" && value["name"] === undefined); // [ { 2021 => 'incredible year' }, { 'key' => 'value', 'different' => 'pairs', 1453 => { id: 1453, name: 'Fatih' } }]
myCollection.map((value, key, collection) => value = key + ": " + value); // [ 'key: value', 'different: pairs', '1453: [object Object]', '2021: incredible year' ]
myCollection.apply((value, key, collection) => { i++; return i * 5; }); // { 'key' => 10, 'different' => 15, 1453 => 20, 2021 => 25 }
myCollection; // { 'key' => 10, 'different' => 15, 1453 => 20, 2021 => 25 }
myCollection.some((value, key, collection) => value === 10); // true
myCollection.some((value, key, collection) => !collection.has(key)); // false
myCollection.every((value, key, collection) => value === 10); // false
myCollection.every((value, key, collection) => typeof value === "number"); // true
let i = 1;
myCollection.reduce((accumulator, value, key, collection) => accumulator + value, 50); // 120
myCollection.reduce((accumulator, value, key, collection) => { accumulator = accumulator * value; return accumulator + value; }); // 83025
Aliases: Collection#each
myCollection.forEach((value, key) => {
console.log(key, "-", value);
});
// key - 10
// different - 15
// 1453 - 20
// 2021 - 25
let secondCollection = new jsollection.Collection();
secondCollection.set("set", "the key");
secondCollection.set("key", "value2");
myCollection.concat(secondCollection); // { 'key' => 'value2', 'different' => 15, 1453 => 20, 2021 => 25, 'set' => 'the key' }
let secondCollection = new jsollection.Collection();
secondCollection.set("set", "the key");
secondCollection.set("key", "value2");
myCollection.equals(secondCollection); // false
secondCollection = myCollection.clone();
myCollection.equals(secondCollection, true); // true
secondCollection.set("key", "different value");
myCollection.equals(secondCollection); // true
myCollection.equals(secondCollection, true); // false
myCollection.sort((x, y) => y - x); // { 2021 => 25, 1453 => 20, 'different' => 15, 'key' => 10 }
myCollection; // { 2021 => 25, 1453 => 20, 'different' => 15, 'key' => 10 }
myCollection.sorted((x, y) => y - x); // { 2021 => 25, 1453 => 20, 'different' => 15, 'key' => 10 }
myCollection; // { 'key' => 10, 'different' => 15, 1453 => 20, 2021 => 25 }
let secondCollection = new jsollection.Collection();
secondCollection.set("set", "the key");
secondCollection.set("key", "different value");
myCollection.intersect(secondCollection); // { 'key' => 'different value' }
myCollection.intersect(secondCollection, true); // {}
Aliases: Collection#diff
let secondCollection = new jsollection.Collection();
secondCollection.set("set", "the key");
secondCollection.set("key", "different value");
myCollection.difference(secondCollection); // { 'set' => 'the key' }
myCollection.difference(secondCollection, true); // { 'key' => 'different value', 'set' => 'the key' }
myCollection.toArray(); // [ [ 'key', 10 ], [ 'different', 15 ], [ 1453, 20 ], [ 2021, 25 ] ]
myCollection.toMap(); // { 'key' => 10, 'different' => 15, 1453 => 20, 2021 => 25 }
myCollection.toString(); // jsollection(4)