arr-helpers
v1.0.0
Published
Zero-dependency array helpers for frontend and backend — parse, group, chunk, sort, dedupe, and more
Maintainers
Readme
arr-helpers
Zero-dependency array helpers for frontend and backend — parse, group, chunk, sort, dedupe, and more.
Works in the browser (React, Vue, Next.js, Vite) and in Node (Express, Nest, APIs). Pure TypeScript, no Node-only APIs.
npm install arr-helpersQuick start
import ArrayList, { chunk, uniqueBy, get, groupByField, paginate } from "arr-helpers";
// Parse query / form / URL values into arrays
get("1,2,3"); // ["1", "2", "3"]
get("[1,2,3]"); // [1, 2, 3]
ArrayList.getIntArray("1,2,3"); // [1, 2, 3]
// Chunk for UI lists or batch jobs
chunk([1, 2, 3, 4, 5], 2); // [[1,2],[3,4],[5]]
// Dedupe objects by key
uniqueBy(
[{ id: 1, name: "a" }, { id: 1, name: "b" }],
"id"
); // [{ id: 1, name: "a" }]
// Group rows (tables, charts, API payloads)
groupByField(
[{ type: "a", v: 1 }, { type: "a", v: 2 }, { type: "b", v: 3 }],
"type"
);
// Paginate for tables or API responses
paginate(items, 1, 20);ESM and CommonJS:
import { chunk, isEmpty } from "arr-helpers";
// or
const { chunk, isEmpty } = require("arr-helpers");Where it fits
| Frontend | Backend | |---|---| | Filter / sort / paginate UI lists | Normalize query params & body fields | | Group chart or table data | Chunk IDs for batch DB/API calls | | Dedupe options / tags | Aggregate and sum row fields | | Move items in drag-and-drop lists | Parse comma-separated IDs |
API
Core
| Function | Description |
|---|---|
| isEmpty / isNotEmpty | Empty-array checks |
| isArray | Non-empty array check |
| getLength | Safe length (0 for invalid) |
| removeDuplicate / unique | Dedupe primitives |
| uniqueBy | Dedupe by key or selector |
| sort(data, key, "ASC"\|"DESC") | Sort objects by field |
| stringIntoArray | "1,2,3" → [1,2,3] |
| groupBy / groupByName | Sum quantities by date/name |
| groupByField | Generic Record<key, T[]> |
| groupByKey | Group by one or two keys |
| splitArray / chunk | Split into sized batches |
| mapByKey | Index objects by key |
| mapByFilter | Filter by field, then pluck |
| toArray | Wrap single value as array |
| get | Normalize string/JSON/array input |
| getIntArray | get + parse integers |
| sumByKeys | Sum numeric fields across rows |
Extra helpers
| Function | Description |
|---|---|
| compact | Drop null / undefined / "" |
| first / last | Safe ends with defaults |
| pluck | Map a single field |
| partition | Split by predicate |
| difference / intersection / union | Set ops |
| countBy | Count by key/selector |
| maxBy / minBy / averageBy | Aggregates by field |
| paginate(list, page, pageSize) | 1-based paging |
| flatten | Flatten nested arrays |
| moveItem | Reorder immutably |
| shuffle | Fisher–Yates shuffle copy |
| filterBy / findBy | Match by field value |
Why this package?
The same array patterns show up in UI lists and API services. arr-helpers packages them with zero dependencies, TypeScript types, tree-shakeable ESM, and CJS for older Node setups.
License
MIT
