sussy-util
Version:
Util package made by me
143 lines (142 loc) • 5.29 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const Optional_1 = __importDefault(require("./Optional"));
class Collection extends Map {
/**
* It removes the key and value from the map.
* @param {K} key - The key of the element to remove from the Map object.
* @returns The object that was removed from the map.
*/
remove(key) {
const object = this.get(key);
if (!object) {
return Optional_1.default.empty();
}
this.delete(key);
return Optional_1.default.of({ key: key, value: object });
}
toString() {
return `Collection: ${this.toString()}`;
}
/**
* It converts the map to an array of objects.
* @returns An array of objects with key and value properties.
*/
toArray() {
return Array.from(this.entries()).map(e => ({ key: e[0], value: e[1] }));
}
/**
* It returns the size of the stack.
* @returns The size of the list.
*/
count() {
return this.size;
}
/**
* returns true if the key is not in the map
* @param {K} key - K
* @returns A boolean value.
*/
missing(key) {
return !this.has(key);
}
/**
* It converts the object to a JSON string.
* @returns The JSON string representation of the array.
*/
toJSONString() {
return JSON.stringify(this.toArray());
}
/**
* It checks if the map contains a specific value.
* @param {V} value - The value to search for in the map.
* @returns A boolean value indicating whether the value is found in the map.
*/
contains(value) {
for (const entry of this.entries()) {
if (entry[1] === value) {
return true;
}
}
return false;
}
/**
* It merges the entries from another map into the current map.
* @param {Collection<K, V>} map - The map to merge with the current map.
*/
merge(map) {
for (const [key, value] of map.entries()) {
this.set(key, value);
}
}
/**
* It returns a new Collection containing entries that satisfy the provided predicate function.
* @param {Function} predicate - A predicate function that is called for each entry with arguments (value, key, collection).
* @returns A new Collection containing entries that satisfy the predicate function.
*/
filter(predicate) {
const filteredCollection = new Collection();
for (const [key, value] of this.entries()) {
if (predicate(value, key, this)) {
filteredCollection.set(key, value);
}
}
return filteredCollection;
}
/**
* It performs a transformation on each entry in the Collection and returns a new Collection with the transformed entries.
* @param {Function} transformFn - A function that is called for each entry with arguments (value, key, collection).
* @returns A new Collection with the transformed entries.
*/
map(transformFn) {
const transformedCollection = new Collection();
for (const [key, value] of this.entries()) {
const transformedValue = transformFn(value, key, this);
transformedCollection.set(key, transformedValue);
}
return transformedCollection;
}
/**
* It performs a reduce operation on the Collection, accumulating a single value based on the entries.
* @param {Function} reducer - A reducer function that is called for each entry with arguments (accumulator, value, key, collection).
* @param {any} initialValue - An initial value for the accumulator.
* @returns The accumulated value after applying the reducer function to each entry.
*/
reduce(reducer, initialValue) {
let accumulator = initialValue;
for (const [key, value] of this.entries()) {
accumulator = reducer(accumulator, value, key, this);
}
return accumulator;
}
/**
* It checks if all entries in the Collection satisfy the provided predicate function.
* @param {Function} predicate - A predicate function that is called for each entry with arguments (value, key, collection).
* @returns A boolean value indicating whether all entries satisfy the predicate function.
*/
every(predicate) {
for (const [key, value] of this.entries()) {
if (!predicate(value, key, this)) {
return false;
}
}
return true;
}
/**
* It checks if any entry in the Collection satisfies the provided predicate function.
* @param {Function} predicate - A predicate function that is called for each entry with arguments (value, key, collection).
* @returns A boolean value indicating whether any entry satisfies the predicate function.
*/
some(predicate) {
for (const [key, value] of this.entries()) {
if (predicate(value, key, this)) {
return true;
}
}
return false;
}
}
exports.default = Collection;