feat(array): add new array helpers and array-like detection#525
Conversation
nev21
commented
Feb 26, 2026
- add arrUnique, arrCompact, arrFlatten, arrGroupBy, arrChunk with ArrayLike support
- add isArrayLike helper and export in index
- add tests for new array helpers and isArrayLike
There was a problem hiding this comment.
Pull request overview
This pull request adds five new array helper functions with ArrayLike support and introduces the isArrayLike type guard helper. The changes enhance the library's array manipulation capabilities with common utility functions like deduplication, compaction, flattening, grouping, and chunking, all following the established arr* naming convention and supporting both arrays and array-like objects.
Changes:
- Added
isArrayLikehelper function to detect array-like objects (objects with numeric length property >= 0) - Added five new array utility functions:
arrUnique,arrCompact,arrFlatten,arrGroupBy, andarrChunk - Updated bundle size limits to accommodate the new functionality (~1KB increase)
- Removed redundant
/*#__PURE__*/markers from simple function assignments in unwrapFunction.ts - Updated version documentation in customError.ts
Reviewed changes
Copilot reviewed 14 out of 14 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| lib/src/helpers/base.ts | Adds isArrayLike type guard function with proper null/undefined/function checks and exports it |
| lib/src/array/unique.ts | New function to remove duplicate values from arrays using object-based tracking |
| lib/src/array/compact.ts | New function to filter out all falsy values from arrays |
| lib/src/array/flatten.ts | New function to flatten nested arrays to specified depth |
| lib/src/array/groupBy.ts | New function to group array elements by callback result |
| lib/src/array/chunk.ts | New function to split arrays into fixed-size chunks |
| lib/src/index.ts | Exports all new functions and types including isArrayLike |
| lib/test/src/common/helpers/base.test.ts | Comprehensive tests for isArrayLike covering various input types |
| lib/test/src/common/helpers/array.test.ts | Comprehensive tests for all five new array functions |
| lib/src/internal/unwrapFunction.ts | Removes redundant /*#__PURE__*/ markers from simple assignments |
| lib/src/helpers/customError.ts | Updates version comment from v0.12.7 to v0.13.0 for superArgsFn parameter |
| .size-limit.json | Updates bundle size limits to reflect new functionality |
| lib/test/bundle-size-check.js | Updates bundle size limits for ES5 and ES6 builds |
| README.md | Updates documentation to include new array functions in the function list |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #525 +/- ##
==========================================
+ Coverage 98.78% 98.82% +0.03%
==========================================
Files 111 134 +23
Lines 3384 3905 +521
Branches 716 852 +136
==========================================
+ Hits 3343 3859 +516
- Misses 41 46 +5
🚀 New features to boost your workflow:
|
- add new array helpers: arrAt, arrChunk, arrCompact, arrDifference, arrDrop, arrDropWhile, arrFill, arrFlatten, arrGroupBy, arrIntersection, arrPartition, arrReverse, arrRotate, arrSample, arrShuffle, arrTake, arrTakeWhile, arrUnion, arrUnique, arrUnzip, arrWith, arrZip - add polyfills: polyArrAt, polyArrFill, polyArrWith - add WritableArrayLike type and export it publicly - add isArrayLike type guard in helpers/base, export it, and add tests - add comprehensive common tests for new array APIs and edge cases - update README utility matrix to include newly added functions - adjust bundle size limits for expanded API surface - include minor docs/typo cleanup updates
| * Checks if the type of value is array-like (has a numeric length property and numeric indices). | ||
| * @since 0.14.0 | ||
| * @function | ||
| * @group Type Identity | ||
| * @group Array |
| */ | ||
|
|
||
| import { isArrayLike } from "../helpers/base"; | ||
| import { normalizeJsName } from "../helpers/encode"; |
| arrForEach(theArray, (item) => { | ||
| const key = ((typeof item) + "_" + item); | ||
|
|
||
| if (!objHasOwn(seen, key)) { | ||
| seen[key] = 1; |
| * @param theArray - The array or array-like object to copy from | ||
| * @param index - The index at which to change the value. Negative index counts from the end | ||
| * @param value - The new value to set at the index | ||
| * @returns A new array with the element at index replaced, or the original array if index is out of range |
| if (!isArrayLike(theArray)) { | ||
| throw new RangeError("Invalid array"); | ||
| } |
|
|
||
| import { fnCall } from "../funcs/funcs"; | ||
| import { isArrayLike } from "../helpers/base"; | ||
| import { getLength } from "../helpers/length"; |
| */ | ||
|
|
||
| import { arrSlice } from "./slice"; | ||
| import { getLength } from "../helpers/length"; |
| * @group ArrayLike | ||
| * @typeParam T - Identifies the type of array elements | ||
| * @param theArray - The array or array-like object to rotate | ||
| * @param count - Number of positions to rotate. Positive = right, negative = left |
| import { ArrProto, UNDEF_VALUE, UNDEFINED } from "../internal/constants"; | ||
| import { _unwrapFunctionWithPoly } from "../internal/unwrapFunction"; | ||
| import { isArrayLike } from "../helpers/base"; | ||
| import { getLength } from "../helpers/length"; | ||
| import { WritableArrayLike } from "../helpers/arrayLike"; | ||
| import { mathMax, mathMin } from "../math/min_max"; |
| it("should throw for null and undefined", () => { | ||
| // arrSample expects a valid array | ||
| assert.isUndefined(arrSample(null)); | ||
| assert.isUndefined(arrSample(undefined)); | ||
| }); |
nevware21-bot
left a comment
There was a problem hiding this comment.
Approved by nevware21-bot