Skip to content
This repository has been archived by the owner on Jan 12, 2023. It is now read-only.

A set of useful utilities that are not provided in the standard JavaScript Array

License

Notifications You must be signed in to change notification settings

kevinhermawan/enhanced-array

Repository files navigation

Enhanced Array License Test coverage

A collection of essential utilities that do not exist in the original JavaScript array

NOTE: This library copies utilities of an array that already exists in other languages. For advanced utilities, please use a library like Lodash instead.

Installation

npm i enhanced-array

Only need a few? Just copy the source code :)

API

Accessing Elements

Returns the first element of the array.

import { getFirst } from 'enhanced-array';

const result = getFirst([1, 2, 3, 4, 5]);
console.log(result); // 1

Time complexity:

O(1)

Returns the last element of the array.

import { getLast } from 'enhanced-array';

const result = getLast([1, 2, 3, 4, 5]);
console.log(result); // 5

Time complexity:

O(1)


Adding Elements

Inserts a new element at the specified position.

import { insertAt } from 'enhanced-array';

const result = insertAt([1, 2, 3, 4, 5], { index: 3, element: 9 });
console.log(result); // [1, 2, 3, 9, 4, 5]

Time complexity:

O(n), where n is the remainder of skipped elements.

O(1), if index is the last index of the array.


Inspecting Elements

Returns a boolean that indicates whether the array contains undefined or null.

import { isContainNil } from 'enhanced-array';

const result1 = isContainNil([1, 2, 3, 4, 5]);
console.log(result1); // false

const result2 = isContainNil([1, 2, undefined, 4, 5]);
console.log(result2); // true

const result3 = isContainNil([1, 2, null, 4, 5]);
console.log(result3); // true

Time complexity:

O(n), where n is the length of the array.

O(1), if nil appears in the first or last index.

Returns a boolean that indicates whether the array is empty.

import { isEmpty } from 'enhanced-array';

const result1 = isEmpty([1, 2, 3, 4, 5]);
console.log(result1); // false

const result2 = isEmpty([]);
console.log(result2); // true

Time complexity:

O(1)


Removing Elements

Removes the element at the specified position.

import { removeAt } from 'enhanced-array';

const result = removeAt([1, 2, 3, 4, 5], { index: 3 });
console.log(result); // [1, 2, 3, 5]

Time complexity:

O(n), where n is the remainder of skipped elements.

O(1), if index is the last index of the array.


Reordering Elements

Moves the element at the specified position to the specified position.

import { move } from 'enhanced-array';

const result = move([1, 2, 3, 4, 5], { index: 0, toIndex: 4 });
console.log(result); // [2, 3, 4, 5, 1]

Time complexity:

O(n), where n is the length of the array.

Shuffles the element of the array. Implements Sattolo cycle.

import { shuffle } from 'enhanced-array';

const result = shuffle([1, 2, 3, 4, 5]);
console.log(result); // [5, 3, 2, 4, 1] (shuffled)

Time complexity:

O(n), where n is the length of the array.

Exchanges the element at the specified indices of the array.

import { swap } from 'enhanced-array';

const result = swap([1, 2, 3, 4, 5], { index: 0, withIndex: 4 });
console.log(result); // [5, 2, 3, 4, 1]

Time complexity:

O(1)

Stolen From