UNPKG

rjweb-server

Version:

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

214 lines (213 loc) 5.05 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var valueCollection_exports = {}; __export(valueCollection_exports, { BaseCollection: () => BaseCollection, default: () => ValueCollection }); module.exports = __toCommonJS(valueCollection_exports); class BaseCollection { /** * Create a New Value Collection * @example * ``` * 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(data, parse, allowModify = true, maxElements = Infinity) { this.modifyFn = null; this.data = /* @__PURE__ */ 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(key) { return this.data.has(key); } /** * Get a Key * @since 2.5.0 */ get(key, fallback) { return this.data.get(key) ?? fallback; } /** * Get all Objects as JSON * @since 2.5.0 */ toJSON(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 = []) { 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, excluded = []) { callback = callback ?? (() => void 0); 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.entries(); } /** * Get the Entries of this Value Collection * @since 6.0.3 */ entries(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, 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; } } class ValueCollection extends BaseCollection { /** * Set a Key * @since 2.5.0 */ set(key, 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(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 = []) { 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; } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { BaseCollection });