Enum

Provides a set of algorithms to work with enumerables.

chunkBy

Splits enumerable on every element for which fun returns a new value. Returns a list of lists.

const list = [1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log(
    chunkBy(list, (prev, current) => current - (prev || current) === 1)
);
// => [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

chunkEvery

Chunks an array into subarrays of a specified size.

const list = [1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log(chunkEvery(list, 3));
// => [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

chunkWhile

Chunks an enumerable into lists of elements while the given predicate returns true. Last sublist includes 10 as it breaks the sequence of odd numbers.

const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
 
const evenChunks = chunkWhile(numbers, (a, b) => a % 2 === 0 && b % 2 === 0);
console.log(evenChunks);
// => [[2], [4], [6], [8], [10]]
 
const oddChunks = chunkWhile(numbers, (a, b) => a % 2 !== 0 && b % 2 !== 0);
console.log(oddChunks);
// => [[1], [3], [5], [7], [9], [10]]

concat, concatLists

Given an enumerable of enumerables, concatenates the enumerables into a single list.

console.log(
    concat([
        [1, 2],
        [3, 4],
        [5, 6],
    ])
);
// => [1, 2, 3, 4, 5, 6]
 
console.log(concatLists([1, [2], 3], [4, 5, 6]));
// => [ 1, [ 2 ], 3, 4, 5, 6 ]

countUntil

Counts the number of elements in the enumerable until the given predicate returns false.

const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
 
const count = countUntil(numbers, (num) => num > 5);
console.log(count);
// => 5

dedup

Removes duplicate elements from an array and returns a new array with unique elements.

const inputList = [1, 2, 3, 2, 4, 1, 5];
const deduplicatedList = dedup(inputList);
console.log(deduplicatedList);
// => [1, 2, 3, 4, 5]

deleteItem

Deletes all occurrences of an item from an array and returns a new array without the item.

const numbers = [1, 2, 3, 4, 5];
const updatedNumbers = deleteItem(numbers, 3);
console.log(updatedNumbers); // Output: [1, 2, 4, 5]

dropEvery

Drops every nth element from an array and returns a new array without those elements.

const list = [1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log(dropEvery(list, 3));
// => [1, 2, 4, 5, 7, 8]

dropWhile

Drops elements from the beginning of an array until the condition is no longer satisfied and returns a new array without those elements.

const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
 
const result = dropWhile(numbers, (num) => num <= 5);
console.log(result);
// => [6, 7, 8, 9, 10]

each

Iterates over an array and applies the given function to each element.

const numbers = [1, 2, 3, 4];
each(numbers, (num) => console.log(num));
// => 1 2 3 4

findIndex

Finds the index of the first element in a collection that satisfies the provided predicate function.

const numbers = [10, 20, 30, 40];
const index = findIndex(numbers, (num) => num > 25);
console.log(index);
// => 2

findValue

Finds the first value in a map that satisfies the provided predicate function.

const fruitMap = new Map([
    ["apple", "red"],
    ["banana", "yellow"],
    ["orange", "orange"],
]);
 
const value = findValue(fruitMap, (color) => color === "yellow");
console.log(value);
// => yellow

flatMapReduce

Applies a mapper function to each element in an array, flattens the resulting arrays, and reduces them into a single value using a reducer function.

const list = [1, 2, 3];
const mapper = (value: number) => [value, value * 2];
const reducer = (acc: number[], value: number[]) => acc.concat(value);
console.log(flatMapReduce(list, mapper, reducer));
// => [1, 2, 2, 4, 3, 6]

groupBy

Groups elements of an array into an object based on a key returned by a key function.

const list = [1, 2, 3, 4, 5];
const result = groupBy(list, (x) => (x % 2 === 0 ? "even" : "odd"));
console.log(result);
// => { odd: [ 1, 3, 5 ], even: [ 2, 4 ] }

into

Adds an element to the end of an array and returns the new array.

console.log(intoCollection([1, 2, 3, 4], 5, 1));
// => [1, 5, 2, 3, 4]
 
console.log(into([1, 2, 3, 4], 5));
// => [1, 2, 3, 4, 5]

introsperse

Inserts a separator element between each element of an array and returns the new array.

const list = [1, 2, 3];
const result = introsperse(list, 0);
console.log(result);
// => [1, 0, 2, 0, 3]
 
const words = ["apple", "banana", "orange"];
const result2 = introsperse(words, "-");
console.log(result2);
// => ["apple", "-", "banana", "-", "orange"]

keysDelete

Deletes keys from an nested object.

const obj = {
    a: 1,
    b: {
        c: 2,
        d: {
            e: 3,
            f: 4,
        },
    },
    g: {
        h: 5,
    },
};
 
const result = keysDelete(obj, ["a", "b.d.f", "g"]);
console.log(result);
// => { b: { c: 2, d: { e: 3 } } }

mapEvery

Maps every nth element of an array using a callback function and returns the new array.

const list = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const result = mapEvery(list, 3, (x) => x * 2);
console.log(result);
// => [1, 2, 6, 4, 5, 12, 7, 8, 18]

mapFetch

Retrieves the value associated with the specified key from a map.

const fruitMap = new Map([
    ["apple", "red"],
    ["banana", "yellow"],
    ["orange", "orange"],
]);
 
console.log(mapFetch(fruitMap, "apple"));
// => red
console.log(mapFetch(fruitMap, "grape"));
// => undefined

enumMap

Applies the provided callback function to each element of the input array and returns a new array with the results.

const array: number[] = [1, 2, 3];
const out: number[] = enumMap(array, (x) => x * 2)
console.log(out); // [2, 4, 6]

mapReduce

Maps each element of an array using a mapper function and reduces the mapped values into a single result using a reducer function.

const array = [1, 2, 3, 4, 5];
const result = mapReduce(
    array,
    (num) => num * num,
    (acc, squared) => acc + squared,
    0
);
console.log(result);
// => 55

maxBy

Finds the maximum value in an array based on a selector function.

interface Person {
    name: string;
    age: number;
}
 
const people: Person[] = [
    { name: "Alice", age: 25 },
    { name: "Bob", age: 30 },
    { name: "Charlie", age: 28 },
];
 
const oldestPerson = maxBy(people, (person) => person.age);
console.log(oldestPerson);
// => { name: 'Bob', age: 30 }

minMaxBy

Finds the minimum and maximum values in an array based on a selector function and a custom comparer function.

const array = [5, 2, 8, 1, 4];
const [min, max] = minMaxBy(
    array,
    (num) => num,
    (a, b) => a - b
);
console.log(`Min: ${min}, Max: ${max}`);
// => Min: 1, Max: 8

reverseSlice

Creates a new array by reversing a section of an existing array.

const array = [1, 2, 3, 4, 5];
const result = reverseSlice(array, 1, 3);
console.log(result);
// => [4, 3, 2]

scan

Applies a function to each element of an array, accumulating the results into a new array.

const list = [1, 2, 3, 4, 5];
const result = scan(list, (acc, x) => acc + x, 0);
console.log(result);
// => [0, 1, 3, 6, 10, 15]

takeEvery

Takes every nth element from an array and returns a new array.

const list = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const result = takeEvery(list, 2);
console.log(result);
// => [1, 3, 5, 7, 9]

takeRandom

Takes a random sample of elements from an array and returns a new array.

const numbers = [1, 2, 3, 4, 5];
const randomNumbers = takeRandom(3, numbers);
console.log(randomNumbers);
// => [?, ?, ?]

uniq

Removes duplicate elements from an array and returns a new array with unique elements.

const numbers = [1, 2, 2, 3, 4, 4, 5];
const uniqueNumbers = uniq(numbers);
console.log(uniqueNumbers);
// => [1, 2, 3, 4, 5]

uniqBy

Removes duplicate elements from an array based on a key function and returns a new array with unique elements.

interface Person {
    name: string;
    age: number;
}
 
const people: Person[] = [
    { name: "Alice", age: 25 },
    { name: "Bob", age: 30 },
    { name: "Alice", age: 35 },
    { name: "Charlie", age: 30 },
];
 
const uniquePeople = uniqueBy(people, (person) => person.name);
console.log(uniquePeople);
// => Output: [
//    { name: 'Alice', age: 25 },
//    { name: 'Bob', age: 30 },
//    { name: 'Charlie', age: 30 },
// ]

unzip

Unzips an array of tuples into separate arrays for each element position.

const tuples: (string | number)[][] = [
    ["a", 1],
    ["b", 2],
    ["c", 3],
];
const [letters, numbers] = unzip(tuples);
console.log(letters);
// => ['a', 'b', 'c']
 
console.log(numbers);
// => [1, 2, 3]

withIndex

Applies a function to each element of an array along with its index.

const array = ["apple", "banana", "cherry"];
withIndex(array, (item, index) => {
    console.log(`Item: ${item}, Index: ${index}`);
});
// => Item: apple, Index: 0 Item: banana, Index: 1 Item: cherry, Index: 2

zipReduce

Reduces two arrays into a single value by applying a function to each pair of corresponding elements.

const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
 
const result = zipReduce(arr1, arr2, 0, (acc, a, b) => acc + a * b);
console.log(result);
// =>    32

zipWith

Applies a function to each pair of corresponding elements from two arrays and returns a new array of the results.

const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
 
const result = zipWith(arr1, arr2, (a, b) => a + b);
console.log(result);
// => [5, 7, 9]