@kakasoo/proto-typescript
Version:
Utility types and implementations based on JavaScript prototypes.
107 lines • 3.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ArrayPrototype = void 0;
var ArrayPrototype;
(function (ArrayPrototype) {
/**
* @param container
* @param predicate
* @example
* ```ts
* const answer = ArrayPrototype.filter<[1, 2, 3, 4, 5], 2>([1, 2, 3, 4, 5] as const, (el: any): el is 2 => el === 2);
* const answer = ArrayPrototype.filter([1, 2, 3, 4, 5] as const, (el: any): el is 3 => el === 3);
* ```
* @returns
*/
function filter(container, predicate) {
return container.filter(predicate);
}
ArrayPrototype.filter = filter;
/**
* Filter `null` or `undefined` element of Array Container.
* @param container
* @param predicate filtering options
* @returns
*/
function filterNullish(container, predicate) {
return container.filter((element) => {
if (predicate.filterNull === true) {
return element !== null;
}
if (predicate.filterUndefined === true) {
return element !== undefined;
}
return true;
});
}
ArrayPrototype.filterNullish = filterNullish;
/**
* It only returns the 0th index without subtracting the elements inside the actual container.
* @param conatiner
* @returns
*/
function shift(conatiner) {
return conatiner.at(0);
}
ArrayPrototype.shift = shift;
/**
* Only return the last index without subtracting the elements inside the actual container.
* @param conatiner
* @returns
*/
function pop(conatiner) {
return conatiner.at(conatiner.length - 1);
}
ArrayPrototype.pop = pop;
/**
* @param container
* @param items
* @returns
*/
function unshift(container, ...items) {
return [...items, ...container];
}
ArrayPrototype.unshift = unshift;
/**
* @todo add paramter of this method, named `thisArg`.
*
* @param container
* @param predicate
* @returns
*/
function some(container, predicate, thisArg) {
return container.some(predicate);
}
ArrayPrototype.some = some;
/**
* @param container
* @param items
* @returns
*/
function push(container, ...items) {
return [...container, ...items];
}
ArrayPrototype.push = push;
/**
* @param container
*/
function at(container, index) {
return container.at(index);
}
ArrayPrototype.at = at;
/**
* type-safe join.
* @example ArrayPrototype.join(["a", "b"]);
* @example ArrayPrototype.join(["a", "b"] as const);
*
* @param container
* @param separator A string used to separate one element of the array from the next in the resulting string. If omitted, the array elements are separated with a comma.
*
* @todo support bigint type (es2020) as element of Array.
*/
function join(container, separator = ',') {
return container.join(separator);
}
ArrayPrototype.join = join;
})(ArrayPrototype || (exports.ArrayPrototype = ArrayPrototype = {}));
//# sourceMappingURL=array.prototype.js.map