@akala/core
Version:
103 lines • 3.31 kB
JavaScript
/**
* Iterates over array-like structures using forEach
* @template T - Element type
* @param {T[] | ArrayLike<T>} array - Array or array-like object to iterate
* @param {function(element: T, i: number): void} body - Callback executed for each element
* @returns {void} - No return value
*/
export function array(array, body) {
Array.prototype.forEach.call(array, body);
}
/**
* Iterates over object properties
* @template TIn - Object type
* @param {TIn} o - Target object to iterate
* @param {function(element: TIn[keyof TIn], i: keyof TIn): void} body - Callback executed for each property
* @returns {void} - No return value
*/
export function object(o, body) {
array(Object.keys(o), function (key) {
body(o[key], key);
});
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function each(it, body) {
if (isArrayLike(it))
return array(it, body);
return object(it, body);
}
/**
* Filters array-like structures
* @template T - Element type
* @param {T[] | ArrayLike<T>} it - Input collection
* @param {function(element: T, i: number): boolean} body - Filter predicate
* @returns {T[]} - Array of matching elements
*/
export function grepArray(it, body) {
const result = [];
array(it, function (el, i) {
if (body(el, i))
result.push(el);
});
return result;
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export function grepObject(o, body, asArray) {
const result = {};
const resultArray = [];
object(o, function (el, i) {
if (body(el, i))
if (asArray)
resultArray.push(el);
else
result[i] = el;
});
if (asArray)
return resultArray;
return result;
}
export function isArrayLike(it) {
return Array.isArray(it) || typeof (it) != 'undefined' && typeof (it['length']) == 'number';
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
export function grep(it, body, asArray) {
if (isArrayLike(it))
return grepArray(it, body);
return grepObject(it, body, asArray);
}
/**
* Transforms array elements
* @template T - Input element type
* @template U - Output element type
* @param {T[] | ArrayLike<T>} it - Input collection
* @param {function(element: T, i: number): U} body - Transformation function
* @returns {U[]} - Transformed elements
*/
export function mapArray(it, body) {
const result = [];
array(it, function (el, i) {
result.push(body(el, i));
});
return result;
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export function mapObject(o, body, asArray) {
const result = {};
const resultArray = [];
object(o, function (el, i) {
if (asArray)
resultArray.push(body(el, i));
else
result[i] = body(el, i);
});
if (asArray)
return resultArray;
return result;
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
export function map(it, body, asArray) {
if (isArrayLike(it))
return mapArray(it, body);
return mapObject(it, body, asArray);
}
//# sourceMappingURL=each.js.map