Array Helper Library
Overview
The Array Helper Library provides a collection of utility functions for working with arrays, including searching, transformation, sorting, and validation.
Installation
To install the package, use:
npm install @jlhv/array-helper
Usage
Import the required functions from the library:
import { unique, flatten, shuffle, min, max } from "@jlhv/array-helper";
Functions and Examples
Basic Operations
isEmpty<T>(arr: T[]): boolean
Checks if an array is empty.
isEmpty([]); // true
isEmpty([1, 2, 3]); // false
first<T>(arr: T[]): T | undefined
Gets the first element of an array.
first([10, 20, 30]); // 10
first([]); // undefined
last<T>(arr: T[]): T | undefined
Gets the last element of an array.
last([10, 20, 30]); // 30
last([]); // undefined
unique<T>(arr: T[]): T[]
Removes duplicate values from an array.
unique([1, 2, 2, 3, 3, 4]); // [1, 2, 3, 4]
Searching & Counting
indexOf<T>(arr: T[], value: T): number
Finds the index of an element in an array without using indexOf
.
indexOf([5, 10, 15], 10); // 1
indexOf([5, 10, 15], 20); // -1
contains<T>(arr: T[], value: T): boolean
Checks if an array contains a specific value.
contains([1, 2, 3], 2); // true
contains([1, 2, 3], 5); // false
countOccurrences<T>(arr: T[], value: T): number
Counts the occurrences of a value in an array.
countOccurrences([1, 2, 2, 3, 3, 3], 3); // 3
Merging & Transforming
mergeUnique<T>(arr1: T[], arr2: T[]): T[]
Merges two arrays without duplicates.
mergeUnique([1, 2, 3], [3, 4, 5]); // [1, 2, 3, 4, 5]
flatten<T>(arr: (T | T[])[]): T[]
Flattens a nested array (single level).
flatten([1, [2, 3], 4]); // [1, 2, 3, 4]
Set Operations
intersection<T>(arr1: T[], arr2: T[]): T[]
Finds common elements between two arrays.
intersection([1, 2, 3], [2, 3, 4]); // [2, 3]
difference<T>(arr1: T[], arr2: T[]): T[]
Finds elements that are in the first array but not in the second.
difference([1, 2, 3], [2, 3, 4]); // [1]
Sorting & Shuffling
shuffle<T>(arr: T[]): T[]
Shuffles an array without using built-in functions.
shuffle([1, 2, 3, 4]); // Output may vary
reverse<T>(arr: T[]): T[]
Reverses an array without using reverse()
.
reverse([1, 2, 3]); // [3, 2, 1]
bubbleSort(arr: number[]): number[]
Sorts an array using bubble sort.
bubbleSort([5, 3, 8, 1]); // [1, 3, 5, 8]
Min & Max Functions
min(arr: number[]): number | undefined
Finds the minimum value in an array.
min([3, 1, 4, 1, 5]); // 1
max(arr: number[]): number | undefined
Finds the maximum value in an array.
max([3, 1, 4, 1, 5]); // 5
Grouping & Partitioning
groupBy<T, K>(arr: T[], callback: (item: T) => K): Record<K, T[]>
Groups elements based on a callback.
groupBy(["apple", "banana", "avocado"], (fruit) => fruit[0]);
// { a: ["apple", "avocado"], b: ["banana"] }
partition<T>(arr: T[], predicate: (item: T) => boolean): [T[], T[]]
Splits an array into two groups based on a condition.
partition([1, 2, 3, 4], (num) => num % 2 === 0);
// [[2, 4], [1, 3]]
Other Helpers
chunk<T>(arr: T[], size: number): T[][]
Breaks an array into chunks of a given size.
chunk([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]]
compact<T>(arr: T[]): T[]
Removes falsy values (false, null, 0, "", undefined, NaN).
compact([0, 1, false, 2, "", 3]); // [1, 2, 3]
rotate<T>(arr: T[], n: number): T[]
Rotates an array by n
positions.
rotate([1, 2, 3, 4, 5], 2); // [3, 4, 5, 1, 2]
rotate([1, 2, 3, 4, 5], -2); // [4, 5, 1, 2, 3]
randomElement<T>(arr: T[]): T | undefined
Returns a random element from an array.
randomElement([10, 20, 30]); // Output may vary
nth<T>(arr: T[], index: number): T | undefined
Gets the nth element of an array (supports negative indexing).
nth([10, 20, 30], 1); // 20
nth([10, 20, 30], -1); // 30
uniqueMultiple<T>(...arrays: T[][]): T[]
Finds unique elements across multiple arrays.
uniqueMultiple([1, 2, 3], [2, 3, 4], [3, 4, 5]); // [1, 5]
License
ISC License.
Author
Vijayavel R