0.1.2 • Published 1 year ago

enhanced-array v0.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

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

getFirst

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)

getLast

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

insertAt

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

isContainNil

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.

isEmpty

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

removeAt

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

move

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.

shuffle

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.

swap

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

0.1.2

1 year ago

0.1.1

2 years ago

0.1.0

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago