Package Exports
- @jlhv/array-helper
- @jlhv/array-helper/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@jlhv/array-helper) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
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-helperUsage
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]); // falsefirst<T>(arr: T[]): T | undefined
Gets the first element of an array.
first([10, 20, 30]); // 10
first([]); // undefinedlast<T>(arr: T[]): T | undefined
Gets the last element of an array.
last([10, 20, 30]); // 30
last([]); // undefinedunique<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); // -1contains<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); // falsecountOccurrences<T>(arr: T[], value: T): number
Counts the occurrences of a value in an array.
countOccurrences([1, 2, 2, 3, 3, 3], 3); // 3Merging & 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 varyreverse<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]); // 1max(arr: number[]): number | undefined
Finds the maximum value in an array.
max([3, 1, 4, 1, 5]); // 5Grouping & 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 varynth<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); // 30uniqueMultiple<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