rc-js-util
Version:
A collection of TS and C++ utilities to help writing performant and correct applications, achieved through strict typing and (removable) invariant checking.
32 lines • 1.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.arrayFlatMap = void 0;
/**
* @public
* Iterate over `ArrayLike` calling a callback against it, the callback returns `ArrayLike` which is concatenated into a single `Array`.
*
* @param items - The items to iterate over.
* @param mapCallback - The return from this callback is concatenated to the result, unless null is returned in which case the result is omitted.
*
* @returns The output of `mapCallback` flattened into a single array.
*
* @remarks
* See {@link arrayFlatMap}.
*/
function arrayFlatMap(items, mapCallback) {
const result = [];
for (let i = 0, iEnd = items.length; i < iEnd; ++i) {
const currentResult = mapCallback(items[i], i);
if (currentResult === null) {
continue;
}
let start = result.length;
result.length += currentResult.length;
for (let j = 0, jEnd = currentResult.length; j < jEnd; ++j) {
result[start++] = currentResult[j];
}
}
return result;
}
exports.arrayFlatMap = arrayFlatMap;
//# sourceMappingURL=array-flat-map.js.map