UNPKG

es-toolkit

Version:

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

18 lines (17 loc) 593 B
import { isArrayLike } from "../predicate/isArrayLike.mjs"; import { iteratee } from "../util/iteratee.mjs"; //#region src/compat/array/countBy.ts function countBy(collection, iteratee$1) { if (collection == null) return {}; const array = isArrayLike(collection) ? Array.from(collection) : Object.values(collection); const mapper = iteratee(iteratee$1 ?? void 0); const result = Object.create(null); for (let i = 0; i < array.length; i++) { const item = array[i]; const key = mapper(item); result[key] = (result[key] ?? 0) + 1; } return result; } //#endregion export { countBy };