Skip to content

Array Utilities

Collection of array manipulation utilities.

chunk

Creates an array of elements split into groups the length of size.

Signature:

typescript
export declare const chunk: <T>(array: T[], size: number) => T[][];

Example:

typescript
import { chunk } from "easy-kit-utils";

chunk(["a", "b", "c", "d"], 2);
// => [['a', 'b'], ['c', 'd']]

chunk(["a", "b", "c", "d"], 3);
// => [['a', 'b', 'c'], ['d']]

uniq

Creates a duplicate-free version of an array.

Signature:

typescript
export declare const uniq: <T>(array: T[]) => T[];

Example:

typescript
import { uniq } from "easy-kit-utils";

uniq([2, 1, 2]);
// => [2, 1]

groupBy

Groups elements of an array based on a given key or criterion function.

Signature:

typescript
export declare const groupBy: <T, K extends keyof any>(
  array: T[],
  key: keyof T | ((item: T) => K)
) => Record<K, T[]>;

Example:

typescript
import { groupBy } from "easy-kit-utils";

groupBy([6.1, 4.2, 6.3], Math.floor);
// => { '4': [4.2], '6': [6.1, 6.3] }

intersection

Creates an array of unique values that are included in all given arrays.

Signature:

typescript
export declare const intersection: <T>(...arrays: T[][]) => T[];

Example:

typescript
import { intersection } from "easy-kit-utils";

intersection([2, 1], [2, 3]);
// => [2]

partition

Splits an array into two groups: one where the predicate returns true, and one where it returns false.

Signature:

typescript
export declare const partition: <T>(
  array: T[],
  predicate: (item: T) => boolean
) => [T[], T[]];

Example:

typescript
import { partition } from "easy-kit-utils";

const users = [
  { user: "barney", age: 36, active: false },
  { user: "fred", age: 40, active: true },
  { user: "pebbles", age: 1, active: false },
];

partition(users, function (o) {
  return o.active;
});
// => objects for [['fred'], ['barney', 'pebbles']]