A lightweight implementation over native Javascript array for some extra sauce.
Ever felt like arrays in Javascript are tedious to work with? Then this is the extension to arrays that you deserve. Arryx is a comprehensive library which provides extra firepower to make working with arrays a breeze. All this in less than 2kb.
-
npm install arryx
-
yarn add arryx
The package exposes the Arryx
class which can be used to instantiate a new array.
import { Arryx } from 'arryx';
const array = new Arryx();
The Arryx
class follows the native Javascript arrays very closely and tries to mimic and provide as many inbuilt methods as possible so as to not cause a drastic change in the API.
The Arryx
class can be initialised almost exactly like the regular Array
class.
-
const arr = new Arryx();
-
const arr = new Arryx('foo'); // ['foo']
const arr = Arryx.from('foo'); // ['foo']
-
const arr = new Arryx([1, 2, 3]); // [1, 2, 3]
const arr = Arryx.from([1, 2, 3]); // [1, 2, 3]
-
const arr = Arryx.create(3); // [empty x3]
const arr = Arryx.from(new Array(3)); // [empty x3]
-
Create a new array of the specified size.
static create<NT>(size: number): Arryx<NT>
const arr = Arryx.create(3); // [empty x3]
-
Creates a new array from given entry(ies).
static from<FT>(entries: FT | FT[]): Arryx<FT>
const arr1 = Arryx.from('a'); // ['a'] const arr2 = Arryx.from([1, 2, 3]); // [1, 2, 3]
-
Checks if the provided argument is an instance of
Arryx
or notstatic is(object: unknown): boolean
const arr = new Arryx(); Arryx.is(arr); // true Arryx.is({}); // false
-
Returns whether the array is empty or not.
const arr = new Arryx(); arr.empty; // true
-
Returns the number of entries in the array.
const arr = new Arryx([1, 2, 3]); arr.length; // 3
-
Clear all entries from the array.
public clear(): Arryx<T>
const arr = new Arryx([1, 2, 3, 4, 5]); arr.entries(); // [1, 2, 3, 4, 5] arr.length; // 5 arr.clear(); arr.entries(); // [] arr.length; // 0
-
Creates a shallow copy of the array.
public clone(): Arryx<T>
const arr1 = new Arryx([1, 2, 3]); const arr2 = arr1.clone(); arr2.update(0, 10); arr1.entries(); // [1, 2, 3] arr2.entries(); // [10, 2, 3]
-
Concatenates two arrays and returns a new array pre-filled with the entries from the two arrays.
public concat<N>(array: Arryx<N>): Arryx<T | N>
const a1 = new Arryx([1, 2, 3]); const a2 = new Arryx([4, 5, 6]); const a3 = a1.concat(a2); // [1, 2, 3, 4, 5, 6]
-
Returns all the entries in the array.
public entries(): T[]
new Arryx(['a', 'b', 'c']).entries(); // ['a', 'b', 'c']
-
Check if all entries in the array match the predicate.
public every(predicate: (entry: T) => boolean): boolean
const arr1 = new Arryx([1, 2, 3]); const arr2 = new Arryx(['a', 'b', 'c']); arr1.every((item) => Number.isFinite(item)); // true arr2.every((item) => Number.isFinite(item)); // false
-
Returns the instance of the array after filling the ranges identified by start and end indices with the specified value.
public fill<N = T>( value: N extends Function ? never : N, startIndex?: number, endIndex?: number ): Arryx<N>
Arryx.create(5).fill('a').entries(); // ['a', 'a', 'a', 'a', 'a'] Arryx.create(5).fill('b', 0, 1).entries(); // ['b', 'b', empty x3] Arryx.create(5).fill('c', 1, 2).entries(); // [empty, 'c', 'c', empty x2]
-
Returns the instance of the array after filling the ranges identified by start and end indices, while running the callback for each index in the array.
public fillDynamic<N = T>( filler: (index: number, entries: N[]) => N, startIndex?: number, endIndex?: number ): Arryx<N>
Arryx.create(5) .fillDynamic((index) => `a${index + 1}`) .entries(); // ['a1', 'a2', 'a3', 'a4', 'a5'] Arryx.create(5) .fillDynamic((index) => `b${index + 1}`, 0, 1) .entries(); // ['b1', 'b2', empty x3] Arryx.create(5) .fillDynamic((index) => `c${index + 1}`, 2) .entries(); // [empty x2, 'c3', 'c4, 'c5']
-
Returns a new array with entries of the array that meet the predicate.
public filter(predicate: (value: T, index: number, entries: T[]) => value is T): Arryx<T>
const arr = new Arryx([1, 2, 3, 4, 5, 6]); arr.filter((item) => item % 2 === 0).entries(); // [2, 4, 6]
-
Find an entry which matches the criteria.
public find(finder: (entry: T) => boolean): T | undefined
const arr = new Arryx([{ id: 1 }, { id: 2 }, { id: 3 }]); arr.find((item) => item.id === 3); // { id: 3 } arr.find((item) => item.id === 100); // undefined
-
Find the index of the first matching entry in the array, given a predicate. If no match found, returns
-1
.public findIndex(predicate: (entry: T) => boolean): number
const arr = new Arryx([5, 12, 8, 130, 44]); arr.findIndex((item) => item > 13); // 3
-
Returns a new array with all sub-array elements concatenated into it recursively up to the specified depth.
public flat<NT = T>(depth?: number): Arryx<NT>
const arr1 = new Arryx([0, 1, 2, [3, 4]]); const arr2 = new Arryx([0, 1, 2, [[[3, 4]]]]); arr1.flat(); // [0, 1, 2, 3, 4] arr2.flat(2); // [0, 1, 2, [3, 4]]
-
Calls a defined callback function on each entry of the array. Then, flattens the result into a new array.
public flatMap<NT = T>( mapper: (entry: T, index: number, entries: T[]) => NT | readonly NT[] ): Arryx<NT>
const arr1 = new Arryx([1, 2, 3, 4]); arr1.flatMap((item) => [item * 2]); // [2, 4, 6, 8] arr1.flatMap((item) => [[item * 2]]); // [[2], [4], [6], [8]]
-
Performs the specified action for each entry in the array.
public forEach(iterator: (value: T, index: number, entries: T[]) => void): void
const arr = new arryx(['a', 'b', 'c']); arr.forEach((element) => console.log(element)); // a // b // c
-
Check whether the array includes a certain entry.
public includes(entry: T, fromIndex?: number): boolean
const arr = new Arryx([1, 2, 3]); arr.includes(2); // true arr.includes(4); // true
-
Find the index of the first occurence of an entry. If no match found, returns
-1
.public indexOf(entry: T, fromindex?: number): number
const beasts = new Arryx(['ant', 'bison', 'camel', 'duck', 'bison']); beasts.indexOf('bison'); // 1 beasts.indexOf('bison', 2); // 4 beasts.indexOf('ox'); // -1
-
Insert a new entry after the specified index.
public insertAfter(index: number, entries: T | T[]): number
const arr1 = new Arryx([1, 3]); arr1.insert(0, 2); arr1.entries(); // [1, 2, 3] const arr2 = new Arryx([1, 5]); arr2.insert(0, [2, 3, 4]); arr2.entries(); // [1, 2, 3, 4, 5]
-
Insert a new entry before the specified index.
public insertBefore(index: number, entries: T | T[]): number
const arr1 = new Arryx([1, 3]); arr1.insert(1, 2); arr1.entries(); // [1, 2, 3] const arr2 = new Arryx([1, 5]); arr2.insert(1, [2, 3, 4]); arr2.entries(); // [1, 2, 3, 4, 5]
-
Adds all the entries of the array into a string, separated by the specified separator string.
public join(separator?: string): string
const arr = new Arryx([1, 2, 3]); arr.join(); // 1,2,3 arr.join('..'); // 1..2..3
-
Find the index of last occurence of an entry. If no match found, returns
-1
.public lastIndexOf(entry: T, fromIndex?: number): number
const animals = new Arryx(['Dodo', 'Tiger', 'Penguin', 'Dodo']); animals.lastIndexOf('Dodo'); // 3 animals.lastIndexOf('Dodo', 1); // 0
-
Calls a defined callback function on each entry of the array, and returns an new array that contains the results.
public map<NT = T>(mapper: (entry: T, index: number, entries: T[]) => NT): Arryx<NT>
const arr = new Arryx([1, 2, 3]); arr.map((item) => item * 2).entiries(); // [2, 4, 6]
-
Peek at the first entry in the array.
public peek(): T
const arr = new Arryx(['foo', 'bar', 'baz']); arr.peek(); // foo
-
Peek at the entry at the specified index. This returns the reference of the entry.
public peekAt(index: number): T
const arr = new Arryx(['foo', 'bar', 'baz']); arr.peekAt(1); // bar
-
Peek at the last entry in the array. This returns the reference of the entry.
public peekLast(): T
const arr = new Arryx(['foo', 'bar', 'baz']); arr.peekAt(1); // baz
-
Return the last entry in the array if it exists, otherwise return
undefined
.public pop(): T | undefined
const arr = new Arryx([1, 2, 3]); arr.pop(); arr.entries(); // [1, 2]
-
Insert a new entry to the end of the array.
public push(entry: T): number
const arr = new Arryx([1, 2]); arr.push();
-
Calls the specified reducer for all entries in the array, in the left-to-right order. Returns the accumulated result.
public reduce<NT = T>( reducer: (previous: NT, current: T, index: number, entries: T[]) => NT, initialValue: NT ): NT
const arr = new Arryx([1, 2, 3]); const reducer = (accumulator, current) => accumulator + current; arr.reduce(reducer); // 6 arr.reduce(reducer, 5); // 11
-
Calls the specified callback function for all the entries the array, in right-to-left order. Returns the accumulated result.
public reduceRight<NT = T>( reducer: (previous: NT, current: T, index: number, entries: T[]) => NT, initialValue: NT ): NT
const arr = new Arryx([ [0, 1], [2, 3], [4, 5], ]); const reducer = (accumulator, currentValue) => accumulator.concat(currentValue); arr.reduceRight(reducer); // [4, 5, 2, 3, 0, 1]
-
Remove the entry at the specified index.
public removeAt(index: number): T | undefined
const arr = new Arryx([1, 3, 2]); arr.removeAt(1); arr.entries(); // [1, 2]
-
Remove
N
entries in the array from a starting index. Returns the removed elements.public removeRange(start: number, count: number): T[]
const arr = new Arryx([1, 3, 4, 5, 6, 7, 2]); arr.removeRange(1, 5); arr.entries(); // [1, 2]
-
Reverses the entries in the array. This method mutates the entries in the array and returns a reference the instance of the array.
public reverse(): Arryx<T>
const arr = new Arryx([1, 2, 3]); arr.reverse().entries(); // [3, 2, 1]
-
Returns the first entry in the array if it exists, otherwise returns
undefined
.public shift(): T | undefined
const arr = new Arryx([1, 2, 3]); arr.shift(); arr.entries(); // [2, 3]
-
Check if some entries in the array match the predicate.
public some(predicate: (entry: T) => boolean): boolean
const arr = new Arryx([1, 2, 3]); arr.some((item) => item % 2 === 0); // true arr.some((item) => item < 0); // false
-
Sorts and returns the instance of the array.
public sort(sorter?: (firstEntry: T, secondEntry: T) => number): Arryx<T>
const arr = new Arryx([3, 4, 2, 1, 7, 11]); arr.sort().entries(); // [1, 2, 3, 4, 7, 11]
-
Returns a new array as a subset of entries from the existing instance.
public take(count: number, startIndex?: number): Arryx<T>
const arr = new Arryx([1, 2, 3, 4, 5, 6]); const arr2 = arr.take(3); arr2.entries(); // [1, 2, 3] const arr3 = arr.take(3, 3); arr3.entries(); // [4, 5, 6];
-
Returns a string representation of the array.
public toString(): string
const arr = new Arryx([1, 2, 3]); arr.toString(); // 1,2,3
-
Insert a new entry to the beginning of the array.
public unshift(entry: T): number
const arr = new Arryx([2, 3]); arr.unshift(1); arr.entries(); // [1, 2, 3]
-
Update the entry at a specified index.
public update(index: number, newItem: T): Arryx<T>
const arr = new Arryx([1, 2, 3]); arr.update(0, 10); arr.entries(); // [10, 2, 3]
-
Returns an iterable of entries in the array.
public values(): Iterable<T>
const arr = new Arryx([1, 2, 3]); arr.values(); // Array Iterator {}
Naturally, being an abstraction over the native array there are quite a couple of distinct differences.
- Passing a number to the constructor will create a new array with EXACTLY one item instead of the number of items
This is done to ensure consistency between creating an array with other data types.
new Array(3); // [empty x3] new Arryx(3); // [3]
- To access the values you need to call
entries
method.const array = new Arryx([1, 2, 3]); array.entries(); // [1, 2, 3]
All PRs are welcome. To setup the project you can run the following commands once you fork and clone the repository:
yarn install
- To develop locally:
yarn dev
- To build from source:
yarn build
Once your changes are made, create a PR from your fork to this repository.
MIT. Do whatever you want with it.