UNPKG

es-toolkit

Version:

A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.

20 lines (19 loc) 726 B
import { identity } from "../../function/identity.mjs"; import { range } from "../../math/range.mjs"; import { isArrayLike } from "../predicate/isArrayLike.mjs"; import { iteratee } from "../util/iteratee.mjs"; //#region src/compat/array/map.ts function map(collection, _iteratee) { if (!collection) return []; const keys = isArrayLike(collection) || Array.isArray(collection) ? range(0, collection.length) : Object.keys(collection); const iteratee$1 = iteratee(_iteratee ?? identity); const result = new Array(keys.length); for (let i = 0; i < keys.length; i++) { const key = keys[i]; const value = collection[key]; result[i] = iteratee$1(value, key, collection); } return result; } //#endregion export { map };