UNPKG

rjweb-server

Version:

Easy and Robust Way to create a Web Server with Many Easy-to-use Features in NodeJS

209 lines (208 loc) 5.53 kB
export class BaseCollection { /** * Create a new Value Collection * @example * ``` * import { ValueCollection } from "rjweb-server" * * const collection = new ValueCollection() * * collection * .set('name', 'beta') * .set('key', 'value') * * collection.has('key') // true * collection.has('ms') // false * * collection.toJSON() // { name: 'beta', key: 'value' } * * collection.forEach((key, value) => { * console.log(key, value) * }) * * collection.clear(['key']) * * collection.toJSON() // { key: 'value' } * ``` * @since 2.5.0 */ constructor( /** JSON Data to Import */ data, /** Function to Parse Values with */ parse, /** Whether to allow modifying the Values */ allowModify = true, /** How many Elements to store at max, when hit clear */ maxElements = Infinity) { this.modifyFn = null; this.data = new Map(); data = data ?? {}; parse = parse ?? ((value) => value); this.maxElements = maxElements; this.allowModify = allowModify; for (const key in data) { this.data.set(key, parse(data[key])); } } /** * Check if a Key exists * @since 2.5.0 */ has( /** The Key to check */ key) { return this.data.has(key); } /** * Get a Key * @since 2.5.0 */ get( /** The Key to get */ key, /** The Fallback Value */ fallback) { return this.data.get(key) ?? fallback; } /** * Get all Objects as JSON * @since 2.5.0 */ toJSON( /** Excluded Keys */ excluded = []) { let keys = {}; for (const [key, value] of this.data) { if (excluded.includes(key)) continue; keys[key] = value; } return keys; } /** * Get all Values as Array * @since 2.5.0 */ toArray( /** Excluded Keys */ excluded = []) { const values = []; for (const [key, value] of this.data) { if (excluded.includes(key)) continue; values.push(value); } return values; } /** * Loop over all Keys * @since 2.5.0 */ forEach( /** Callback Function */ callback, /** Excluded Keys */ excluded = []) { callback = callback ?? (() => undefined); let index = 0; this.data.forEach((value, key) => { if (excluded.includes(key)) return; callback(key, value, index++); }); return this; } /** * Object Iterator (similar to .forEach() but can be used in for ... of loops) * @since 7.7.0 */ [Symbol.iterator]() { return this.data[Symbol.iterator](); } /** * Get the Entries of this Value Collection * @since 6.0.3 */ entries( /** Excluded Keys */ excluded = []) { const entries = []; this.data.forEach((value, key) => { if (excluded.includes(key)) return; entries.push([key, value]); }); return entries; } /** * Map the Keys to a new Array * @since 5.3.1 */ map( /** Callback Function */ callback, /** Excluded Keys */ excluded = []) { callback = callback ?? ((value) => value); const result = []; let index = 0; this.data.forEach((value, key) => { if (excluded.includes(key)) return; result.push(callback(key, value, index++, this)); }); return result; } /** * The Amount of Stored Objects * @since 2.7.2 */ get objectCount() { return this.data.size; } } /** * A Key - Value Store with easy access functions * @example * ``` * const collection = new ValueCollection(...) * ``` * @since 2.5.0 */ export default class ValueCollection extends BaseCollection { /** * Set a Key * @since 2.5.0 */ set( /** The Key to set */ key, /** The new Value */ value) { if (!this.allowModify) return this; if (this.objectCount > this.maxElements) this.clear(); if (this.modifyFn) this.modifyFn('set', key, value); else this.data.set(key, value); return this; } /** * Delete a Key * @since 8.0.0 */ delete( /** The Key to delete */ key) { if (!this.allowModify) return this; if (this.objectCount > this.maxElements) this.clear(); if (this.modifyFn) this.modifyFn('delete', key, null); else this.data.delete(key); return this; } /** * Clear the Stored Objects * @since 3.0.0 */ clear( /** Excluded Keys */ excluded = []) { if (!this.allowModify) return 0; let keys = 0; if (this.modifyFn) keys = this.modifyFn('clear', excluded, null); else for (const [key] of this.data) { if (excluded.includes(key)) continue; this.data.delete(key); keys++; } return keys; } /** * Import another Value Collection into this one * @since 9.0.0 */ import(valueCollection) { valueCollection.data.forEach((value, key) => { this.data.set(key, value); }); return this; } }