arryx v1.0.0
Arryx
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.
Table of Contents
Installation
via NPM
npm install arryxvia Yarn
yarn add arryx
Usage
The package exposes the Arryx class which can be used to instantiate a new array.
import { Arryx } from 'arryx';
const array = new Arryx();API
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.
Initialization
The Arryx class can be initialised almost exactly like the regular Array class.
Empty array
const arr = new Arryx();With a single element
const arr = new Arryx('foo'); // ['foo']const arr = Arryx.from('foo'); // ['foo']With multiple elements
const arr = new Arryx([1, 2, 3]); // [1, 2, 3]const arr = Arryx.from([1, 2, 3]); // [1, 2, 3]Empty array of size
Nconst arr = Arryx.create(3); // [empty x3]const arr = Arryx.from(new Array(3)); // [empty x3]
Static methods
createCreate a new array of the specified size.
Signature:
static create<NT>(size: number): Arryx<NT>Example:
const arr = Arryx.create(3); // [empty x3]fromCreates a new array from given entry(ies).
Signature:
static from<FT>(entries: FT | FT[]): Arryx<FT>Example:
const arr1 = Arryx.from('a'); // ['a'] const arr2 = Arryx.from([1, 2, 3]); // [1, 2, 3]isChecks if the provided argument is an instance of
Arryxor notSignature:
static is(object: unknown): booleanExample:
const arr = new Arryx(); Arryx.is(arr); // true Arryx.is({}); // false
Properties
emptyReturns whether the array is empty or not.
Return type:
booleanExample:
const arr = new Arryx(); arr.empty; // truelengthReturns the number of entries in the array.
Return type:
numberExample:
const arr = new Arryx([1, 2, 3]); arr.length; // 3
Methods
clearClear all entries from the array.
Signature:
public clear(): Arryx<T>Example:
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; // 0cloneCreates a shallow copy of the array.
Signature:
public clone(): Arryx<T>Example:
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]concatConcatenates two arrays and returns a new array pre-filled with the entries from the two arrays.
Signature:
public concat<N>(array: Arryx<N>): Arryx<T | N>Example:
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]entriesReturns all the entries in the array.
Signature:
public entries(): T[]Example:
new Arryx(['a', 'b', 'c']).entries(); // ['a', 'b', 'c']everyCheck if all entries in the array match the predicate.
Signature:
public every(predicate: (entry: T) => boolean): booleanExample:
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)); // falsefillReturns the instance of the array after filling the ranges identified by start and end indices with the specified value.
Signature:
public fill<N = T>( value: N extends Function ? never : N, startIndex?: number, endIndex?: number ): Arryx<N>Example:
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]fillDynamicReturns 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.
Signature:
public fillDynamic<N = T>( filler: (index: number, entries: N[]) => N, startIndex?: number, endIndex?: number ): Arryx<N>Example:
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']filterReturns a new array with entries of the array that meet the predicate.
Signature:
public filter(predicate: (value: T, index: number, entries: T[]) => value is T): Arryx<T>Example:
const arr = new Arryx([1, 2, 3, 4, 5, 6]); arr.filter((item) => item % 2 === 0).entries(); // [2, 4, 6]findFind an entry which matches the criteria.
Signature:
public find(finder: (entry: T) => boolean): T | undefinedExample:
const arr = new Arryx([{ id: 1 }, { id: 2 }, { id: 3 }]); arr.find((item) => item.id === 3); // { id: 3 } arr.find((item) => item.id === 100); // undefinedfindIndexFind the index of the first matching entry in the array, given a predicate. If no match found, returns
-1.Signature:
public findIndex(predicate: (entry: T) => boolean): numberExample:
const arr = new Arryx([5, 12, 8, 130, 44]); arr.findIndex((item) => item > 13); // 3flatReturns a new array with all sub-array elements concatenated into it recursively up to the specified depth.
Signature:
public flat<NT = T>(depth?: number): Arryx<NT>Example:
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]]flatMapCalls a defined callback function on each entry of the array. Then, flattens the result into a new array.
Signature:
public flatMap<NT = T>( mapper: (entry: T, index: number, entries: T[]) => NT | readonly NT[] ): Arryx<NT>Example:
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]]forEachPerforms the specified action for each entry in the array.
Signature:
public forEach(iterator: (value: T, index: number, entries: T[]) => void): voidExample:
const arr = new arryx(['a', 'b', 'c']); arr.forEach((element) => console.log(element)); // a // b // cincludesCheck whether the array includes a certain entry.
Signature:
public includes(entry: T, fromIndex?: number): booleanExample:
const arr = new Arryx([1, 2, 3]); arr.includes(2); // true arr.includes(4); // trueindexOfFind the index of the first occurence of an entry. If no match found, returns
-1.Signature:
public indexOf(entry: T, fromindex?: number): numberExample:
const beasts = new Arryx(['ant', 'bison', 'camel', 'duck', 'bison']); beasts.indexOf('bison'); // 1 beasts.indexOf('bison', 2); // 4 beasts.indexOf('ox'); // -1insertAfterInsert a new entry after the specified index.
Signature:
public insertAfter(index: number, entries: T | T[]): numberExample:
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]insertBeforeInsert a new entry before the specified index.
Signature:
public insertBefore(index: number, entries: T | T[]): numberExample:
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]joinAdds all the entries of the array into a string, separated by the specified separator string.
Signature:
public join(separator?: string): stringExample:
const arr = new Arryx([1, 2, 3]); arr.join(); // 1,2,3 arr.join('..'); // 1..2..3lastIndexOfFind the index of last occurence of an entry. If no match found, returns
-1.Signature:
public lastIndexOf(entry: T, fromIndex?: number): numberExample:
const animals = new Arryx(['Dodo', 'Tiger', 'Penguin', 'Dodo']); animals.lastIndexOf('Dodo'); // 3 animals.lastIndexOf('Dodo', 1); // 0mapCalls a defined callback function on each entry of the array, and returns an new array that contains the results.
Signature:
public map<NT = T>(mapper: (entry: T, index: number, entries: T[]) => NT): Arryx<NT>Example:
const arr = new Arryx([1, 2, 3]); arr.map((item) => item * 2).entiries(); // [2, 4, 6]peekPeek at the first entry in the array.
Signature:
public peek(): TExample:
const arr = new Arryx(['foo', 'bar', 'baz']); arr.peek(); // foopeekAtPeek at the entry at the specified index. This returns the reference of the entry.
Signature:
public peekAt(index: number): TExample:
const arr = new Arryx(['foo', 'bar', 'baz']); arr.peekAt(1); // barpeekLastPeek at the last entry in the array. This returns the reference of the entry.
Signature:
public peekLast(): TExample:
const arr = new Arryx(['foo', 'bar', 'baz']); arr.peekAt(1); // bazpopReturn the last entry in the array if it exists, otherwise return
undefined.Signature:
public pop(): T | undefinedExample:
const arr = new Arryx([1, 2, 3]); arr.pop(); arr.entries(); // [1, 2]pushInsert a new entry to the end of the array.
Signature:
public push(entry: T): numberExample:
const arr = new Arryx([1, 2]); arr.push();reduceCalls the specified reducer for all entries in the array, in the left-to-right order. Returns the accumulated result.
Signature:
public reduce<NT = T>( reducer: (previous: NT, current: T, index: number, entries: T[]) => NT, initialValue: NT ): NTExample:
const arr = new Arryx([1, 2, 3]); const reducer = (accumulator, current) => accumulator + current; arr.reduce(reducer); // 6 arr.reduce(reducer, 5); // 11reduceRightCalls the specified callback function for all the entries the array, in right-to-left order. Returns the accumulated result.
Signature:
public reduceRight<NT = T>( reducer: (previous: NT, current: T, index: number, entries: T[]) => NT, initialValue: NT ): NTExample:
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]removeAtRemove the entry at the specified index.
Signature:
public removeAt(index: number): T | undefinedExample:
const arr = new Arryx([1, 3, 2]); arr.removeAt(1); arr.entries(); // [1, 2]removeRangeRemove
Nentries in the array from a starting index. Returns the removed elements.Signature:
public removeRange(start: number, count: number): T[]Example:
const arr = new Arryx([1, 3, 4, 5, 6, 7, 2]); arr.removeRange(1, 5); arr.entries(); // [1, 2]reverseReverses the entries in the array. This method mutates the entries in the array and returns a reference the instance of the array.
Signature:
public reverse(): Arryx<T>Example:
const arr = new Arryx([1, 2, 3]); arr.reverse().entries(); // [3, 2, 1]shiftReturns the first entry in the array if it exists, otherwise returns
undefined.Signature:
public shift(): T | undefinedExample:
const arr = new Arryx([1, 2, 3]); arr.shift(); arr.entries(); // [2, 3]someCheck if some entries in the array match the predicate.
Signature:
public some(predicate: (entry: T) => boolean): booleanExample:
const arr = new Arryx([1, 2, 3]); arr.some((item) => item % 2 === 0); // true arr.some((item) => item < 0); // falsesortSorts and returns the instance of the array.
Signature:
public sort(sorter?: (firstEntry: T, secondEntry: T) => number): Arryx<T>Example:
const arr = new Arryx([3, 4, 2, 1, 7, 11]); arr.sort().entries(); // [1, 2, 3, 4, 7, 11]takeReturns a new array as a subset of entries from the existing instance.
Signature:
public take(count: number, startIndex?: number): Arryx<T>Example:
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];toStringReturns a string representation of the array.
Signature:
public toString(): stringExample:
const arr = new Arryx([1, 2, 3]); arr.toString(); // 1,2,3unshiftInsert a new entry to the beginning of the array.
Signature:
public unshift(entry: T): numberExample:
const arr = new Arryx([2, 3]); arr.unshift(1); arr.entries(); // [1, 2, 3]updateUpdate the entry at a specified index.
Signature:
public update(index: number, newItem: T): Arryx<T>Example:
const arr = new Arryx([1, 2, 3]); arr.update(0, 10); arr.entries(); // [10, 2, 3]valuesReturns an iterable of entries in the array.
Signature:
public values(): Iterable<T>Example:
const arr = new Arryx([1, 2, 3]); arr.values(); // Array Iterator {}
Differences
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
entriesmethod.const array = new Arryx([1, 2, 3]); array.entries(); // [1, 2, 3]
Contributing
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.
License
MIT. Do whatever you want with it.
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago