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.
28 lines (25 loc) • 595 B
text/typescript
/**
* @public
* Like _Array.map but where the callback returns null it will be omitted from the result.
*
* @remarks
* See {@link arrayCompactMap}.
*/
export function arrayCompactMap<TItem, TTransformed>
(
items: ArrayLike<TItem>,
map: (item: TItem, index: number) => TTransformed | null
)
: TTransformed[]
{
const mapped: TTransformed[] = [];
for (let i = 0, iEnd = items.length; i < iEnd; ++i)
{
const transformed = map(items[i], i);
if (transformed !== null)
{
mapped.push(transformed);
}
}
return mapped;
}