
Research
/Security News
Toptal’s GitHub Organization Hijacked: 10 Malicious Packages Published
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.
A lightweight implementation over native Javascript array for some extra sauce.
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]
N
const arr = Arryx.create(3); // [empty x3]
const arr = Arryx.from(new Array(3)); // [empty x3]
create
Create a new array of the specified size.
static create<NT>(size: number): Arryx<NT>
const arr = Arryx.create(3); // [empty x3]
from
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]
is
Checks if the provided argument is an instance of Arryx
or not
static is(object: unknown): boolean
const arr = new Arryx();
Arryx.is(arr); // true
Arryx.is({}); // false
empty
Returns whether the array is empty or not.
boolean
const arr = new Arryx();
arr.empty; // true
length
Returns the number of entries in the array.
number
const arr = new Arryx([1, 2, 3]);
arr.length; // 3
clear
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
clone
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]
concat
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]
entries
Returns all the entries in the array.
public entries(): T[]
new Arryx(['a', 'b', 'c']).entries(); // ['a', 'b', 'c']
every
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
fill
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]
fillDynamic
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']
filter
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
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
findIndex
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
flat
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]]
flatMap
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]]
forEach
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
includes
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
indexOf
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
insertAfter
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]
insertBefore
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]
join
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
lastIndexOf
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
map
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
Peek at the first entry in the array.
public peek(): T
const arr = new Arryx(['foo', 'bar', 'baz']);
arr.peek(); // foo
peekAt
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
peekLast
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
pop
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]
push
Insert a new entry to the end of the array.
public push(entry: T): number
const arr = new Arryx([1, 2]);
arr.push();
reduce
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
reduceRight
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]
removeAt
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]
removeRange
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]
reverse
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]
shift
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]
some
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
sort
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]
take
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];
toString
Returns a string representation of the array.
public toString(): string
const arr = new Arryx([1, 2, 3]);
arr.toString(); // 1,2,3
unshift
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
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]
values
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.
new Array(3); // [empty x3]
new Arryx(3); // [3]
This is done to ensure consistency between creating an array with other data types.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
yarn dev
yarn build
Once your changes are made, create a PR from your fork to this repository.
MIT. Do whatever you want with it.
FAQs
A lightweight implementation over native Javascript array for some extra sauce.
The npm package arryx receives a total of 0 weekly downloads. As such, arryx popularity was classified as not popular.
We found that arryx demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
/Security News
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.
Research
/Security News
Socket researchers investigate 4 malicious npm and PyPI packages with 56,000+ downloads that install surveillance malware.
Security News
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.