1.0.0 • Published 3 years ago
jsollection v1.0.0
jsollection
About
The Collection class is similar to the Map class, but also includes methods that can be advantageous.
Contributors
- ceyrex (Ceyhun): Added typings.
Installation
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).
Using npm
npm install jsollection
Using Yarn
yarn install jsollection
Using Git
git clone https://github.com/MustafaTRK/jsollection.git
Manually (GitHub Archive)
MustafaTRK/jsollection - GitHub Archive
Examples
Include in the project
// 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";
static Collection#fromArray()
let myArray = [["key", "value"], ["second", "strip"]];
const myCollection = jsollection.Collection.fromArray(myArray);
static Collection#fromMap()
let myMap = new Map();
myMap.set("key", "value");
const myCollection = jsollection.Collection.fromMap(myMap);
Collection#constructor(entries)
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);
Collection#set(key, value)
myCollection.set("key", "value");
myCollection.set(1453, { id: 1453, name: "Fatih" });
Collection#get(key)
myCollection.get(1453); // { id: 1453, name: 'Fatih' }
myCollection.get("randomly"); // undefined
Collection#has(key)
myCollection.has("key"); // true
myCollection.has("randomly"); // false
Collection#size()
myCollection.size(); // 2
Collection#delete(key, timeout)
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.
Collection#clear()
myCollection.clear(); // 0
Note: In the rest of the examples, it will be treated as not deleted.
Collection#clone()
let secondCollection = myCollection.clone();
Collection#keys()
myCollection.keys(); // ['key', 1453]
Collection#values()
myCollection.keys(); // ['value', 1453]
Collection#random(amount)
myCollection.random(0); // []
myCollection.random(); // 'value'
myCollection.random(4); // [{ id: 1453, name: 'Fatih' }, 'value', undefined, undefined]
Collection#randomKey(amount)
myCollection.randomKey(0); // []
myCollection.randomKey(); // 1453
myCollection.randomKey(4); // ['key', 1453, undefined, undefined]
Collection#find(func, thisArg)
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
Collection#find(func, thisArg)
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.
Collection#filter(func, thisArg)
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' } }
Collection#partition(func, thisArg)
myCollection.filter((value, key, collection) => typeof key === "number" && value["name"] === undefined); // [ { 2021 => 'incredible year' }, { 'key' => 'value', 'different' => 'pairs', 1453 => { id: 1453, name: 'Fatih' } }]
Collection#map(func, thisArg)
myCollection.map((value, key, collection) => value = key + ": " + value); // [ 'key: value', 'different: pairs', '1453: [object Object]', '2021: incredible year' ]
Collection#apply(func, thisArg)
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 }
Collection#some(func, thisArg)
myCollection.some((value, key, collection) => value === 10); // true
myCollection.some((value, key, collection) => !collection.has(key)); // false
Collection#every(func, thisArg)
myCollection.every((value, key, collection) => value === 10); // false
myCollection.every((value, key, collection) => typeof value === "number"); // true
Collection#reduce(func, initialValue)
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
Collection#forEach(func, thisArg)
Aliases: Collection#each
myCollection.forEach((value, key) => {
console.log(key, "-", value);
});
// key - 10
// different - 15
// 1453 - 20
// 2021 - 25
Collection#concat(collection, thisArg)
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' }
Collection#equals(collection, checkValue)
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
Collection#sort(func)
myCollection.sort((x, y) => y - x); // { 2021 => 25, 1453 => 20, 'different' => 15, 'key' => 10 }
myCollection; // { 2021 => 25, 1453 => 20, 'different' => 15, 'key' => 10 }
Collection#sorted(func)
myCollection.sorted((x, y) => y - x); // { 2021 => 25, 1453 => 20, 'different' => 15, 'key' => 10 }
myCollection; // { 'key' => 10, 'different' => 15, 1453 => 20, 2021 => 25 }
Collection#intersect(collection, checkValue)
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); // {}
Collection#difference(collection, checkValue)
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' }
Collection#toArray()
myCollection.toArray(); // [ [ 'key', 10 ], [ 'different', 15 ], [ 1453, 20 ], [ 2021, 25 ] ]
Collection#toMap()
myCollection.toMap(); // { 'key' => 10, 'different' => 15, 1453 => 20, 2021 => 25 }
Collection#toString()
myCollection.toString(); // jsollection(4)
1.0.0
3 years ago