rjweb-server
Version:
Easy and Robust Way to create a Web Server with Many Easy-to-use Features in NodeJS
190 lines (189 loc) • 4 kB
JavaScript
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;
}
}
export {
BaseCollection,
ValueCollection as default
};