@billdaddy/arraykit
v0.1.1
Published
Tiny, type-safe array utilities — chunk, groupBy, keyBy, countBy, uniqueBy, partition, sortBy, zip, range, and numeric helpers. Zero dependencies.
Downloads
193
Maintainers
Readme
arraykit
Tiny, type-safe array utilities —
chunk,groupBy,keyBy,countBy,uniqueBy,partition,sortBy,zip,range, and numeric helpers. Zero dependencies.
JavaScript got .flat(), .at(), and Object.groupBy — but you still reach for
lodash the moment you need chunk, a stable multi-key sortBy, or keyBy.
arraykit is that handful of everyday array helpers, tree-shakeable, fully
typed, and zero-dependency — import only what you use.
import { chunk, groupBy, sortBy } from "@billdaddy/arraykit";
chunk([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]]
groupBy(users, (u) => u.role); // { admin: [...], user: [...] }
sortBy(users, (u) => u.lastName, (u) => u.age); // stable, multi-keyWhy arraykit?
- The missing helpers.
chunk,groupBy,keyBy,countBy,uniqueBy,partition,sortBy,zip,range, plussum/mean/minBy/maxBy. - Pure & predictable. Nothing mutates its input; selectors get the index too.
- Stable, multi-key
sortBy. Pass several selectors; ties fall through in order. - Strong types.
zipinfers a tuple type,groupBy/keyByreturn records,minBy/maxByreturnT | undefined. - Tree-shakeable.
sideEffects: false, ESM + CJS, zero dependencies.
Install
npm install @billdaddy/arraykit
# or: pnpm add @billdaddy/arraykit / yarn add @billdaddy/arraykit / bun add @billdaddy/arraykitGrouping & dedupe
import { chunk, groupBy, keyBy, countBy, partition, uniqueBy } from "@billdaddy/arraykit";
chunk([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]]
groupBy([1, 2, 3, 4], (n) => n % 2 ? "odd" : "even"); // { odd: [1,3], even: [2,4] }
keyBy([{ id: "a" }, { id: "b" }], (x) => x.id); // { a: {...}, b: {...} }
countBy(["a", "b", "a"], (x) => x); // { a: 2, b: 1 }
partition([1, 2, 3, 4], (n) => n % 2 === 0); // [[2, 4], [1, 3]]
uniqueBy([{ id: 1 }, { id: 1 }, { id: 2 }], (x) => x.id); // [{id:1}, {id:2}]
uniqueBy([1, 1, 2, 3]); // [1, 2, 3] (identity)Ordering
import { sortBy, zip, range } from "@billdaddy/arraykit";
sortBy(people, (p) => p.age); // ascending, original untouched
sortBy(people, (p) => p.last, (p) => p.age); // last name, then age
zip([1, 2, 3], ["a", "b", "c"]); // [[1,"a"], [2,"b"], [3,"c"]]
range(4); // [0, 1, 2, 3]
range(2, 6); // [2, 3, 4, 5]
range(0, 10, 2); // [0, 2, 4, 6, 8]
range(3, 0); // [3, 2, 1]Numerics
import { sum, sumBy, mean, minBy, maxBy } from "@billdaddy/arraykit";
sum([1, 2, 3]); // 6
sumBy(carts, (c) => c.total); // total of totals
mean([2, 4, 6]); // 4
minBy(users, (u) => u.age); // youngest (or undefined)
maxBy(users, (u) => u.age); // oldestmean([]) is NaN; minBy/maxBy of an empty array are undefined; ties keep
the first occurrence.
Pairs well with
| Need | Use |
| --- | --- |
| Deep equal / clone of elements | equalkit |
| Read/write nested fields by path | dotpathkit |
Contributors ✨
This project follows the all-contributors specification. Contributions of any kind are welcome — code, docs, bug reports, ideas, reviews! See the emoji key for how each contribution is recognized, and open a PR or issue to get involved.
Thanks goes to these wonderful people:
License
MIT © Tung Tran
