sussy-util
Version:
Util package made by me
168 lines (167 loc) • 5.67 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 {
constructor() {
super(...arguments);
/**
* Removes the key and value from the map.
* @param key - The key of the element to remove.
* @returns An Optional containing the removed {key, value}, or empty if not found.
*/
this.remove = (key) => {
const object = this.get(key);
if (object === void 0)
return Optional_1.default.empty();
this.delete(key);
return Optional_1.default.of({ key, value: object });
};
/**
* Provides a human-readable string representation.
*/
this.toString = () => {
return `Collection(${this.toJSONString()})`;
};
/**
* Converts the map to an array of {key, value} objects.
*/
this.toArray = () => {
return Array.from(this.entries()).map(([key, value]) => ({ key, value }));
};
/**
* Returns the number of entries in the Collection.
*/
this.count = () => {
return this.size;
};
/**
* Returns true if the key does not exist in the map.
*/
this.missing = (key) => {
return !this.has(key);
};
/**
* Converts the Collection to a JSON string.
*/
this.toJSONString = () => {
return JSON.stringify(this.toArray());
};
/**
* Checks if the map contains a specific value.
*/
this.contains = (value) => {
for (const val of this.values()) {
if (val === value)
return true;
}
return false;
};
/**
* Merges entries from another map into this one.
*/
this.merge = (map) => {
for (const [key, value] of map.entries()) {
this.set(key, value);
}
};
/**
* Filters the collection based on a predicate.
* Returns a new Collection with entries that satisfy the predicate.
*/
this.filter = (predicate) => {
const result = new Collection();
for (const [key, value] of this.entries()) {
if (predicate(value, key, this)) {
result.set(key, value);
}
}
return result;
};
/**
* Transforms the collection values and returns a new Collection with the transformed values.
* Keys remain the same.
*/
this.mapValues = (transformFn) => {
const transformed = new Collection();
for (const [key, value] of this.entries()) {
const newValue = transformFn(value, key, this);
transformed.set(key, newValue);
}
return transformed;
};
/**
* Maps the collection to an array of values based on a callback function.
* Returns an array of transformed values.
*/
this.mapArray = (callback) => {
const result = [];
for (const [key, value] of this.entries()) {
result.push(callback(value, key, this));
}
return result;
};
/**
* Reduces the collection into a single value.
*/
this.reduce = (reducer, initialValue) => {
let accumulator = initialValue;
for (const [key, value] of this.entries()) {
accumulator = reducer(accumulator, value, key, this);
}
return accumulator;
};
/**
* Checks if all entries satisfy the predicate.
*/
this.every = (predicate) => {
for (const [key, value] of this.entries()) {
if (!predicate(value, key, this))
return false;
}
return true;
};
/**
* Checks if any entry satisfies the predicate.
*/
this.some = (predicate) => {
for (const [key, value] of this.entries()) {
if (predicate(value, key, this))
return true;
}
return false;
};
/**
* Finds the first entry that satisfies the predicate.
* Returns an Optional containing [key, value] if found, otherwise empty.
*/
this.find = (predicate) => {
for (const [key, value] of this.entries()) {
if (predicate(value, key, this)) {
return Optional_1.default.of([key, value]);
}
}
return Optional_1.default.empty();
};
/**
* Returns the value as an Optional.
*/
this.getOptional = (key) => {
return Optional_1.default.ofNullable(this.get(key));
};
/**
* Finds the key associated with the given value.
* Returns an Optional containing the key if found, otherwise empty.
*/
this.keyOf = (value) => {
for (const [key, val] of this.entries()) {
if (val === value)
return Optional_1.default.of(key);
}
return Optional_1.default.empty();
};
}
}
exports.default = Collection;