Array Utility Functions Documentation

This npm package provides several utility functions for working with arrays in JavaScript. Below are the details of each function along with code examples for usage.

filterArray

Filters an array based on a predicate function.

Parameters

Returns

A new array containing only the elements that satisfy the predicate function.

Example

const { filterArray } = require('auxin');

                const arr = [1, 2, 3, 4, 5];
                const filteredArr = filterArray(arr, num => num % 2 === 0);
                console.log(filteredArr); // Output: [2, 4]
                

mapArray

Maps each element of an array to a new value using a transformation function.

Parameters

Returns

A new array containing the transformed elements.

Example

const { mapArray } = require('auxin');

                const arr = [1, 2, 3, 4, 5];
                const squaredArr = mapArray(arr, num => num * num);
                console.log(squaredArr); // Output: [1, 4, 9, 16, 25]
                

binarySearch

Performs a binary search on a sorted array to find the index of a target element.

Parameters

Returns

The index of the target element if found, otherwise -1.

Example

const { binarySearch } = require('auxin');

                const arr = [1, 2, 3, 4, 5];
                const index = binarySearch(arr, 3);
                console.log(index); // Output: 2
                

quickSort

Sorts an array using the Quick Sort algorithm.

Parameters

Returns

A new array containing the sorted elements.

Example

const { quickSort } = require('auxin');

                const arr = [3, 1, 4, 1, 5, 9, 2, 6, 5];
                const sortedArr = quickSort(arr);
                console.log(sortedArr); // Output: [1, 1, 2, 3, 4, 5, 5, 6, 9]
                

arrPush

Appends an element to the end of an array.

Parameters

Returns

The modified array with the element appended.

Example

const { arrPush } = require('auxin');

                let arr = [1, 2, 3];
                arr = arrPush(arr, 4);
                console.log(arr); // Output: [1, 2, 3, 4]
                

arrUnshift

Prepends an element to the beginning of an array.

Parameters

Returns

The modified array with the element prepended.

Example

const { arrUnshift } = require('auxin');

                let arr = [2, 3, 4];
                arr = arrUnshift(arr, 1);
                console.log(arr); // Output: [1, 2, 3, 4]
                

arrPop

Removes the last element from an array.

Parameters

Returns

The modified array with the last element removed.

Example

const { arrPop } = require('auxin');

                let arr = [1, 2, 3];
                arr = arrPop(arr);
                console.log(arr); // Output: [1, 2]
                

arrShift

Removes the first element from an array.

Parameters

Returns

The modified array with the first element removed.

Example

const { arrShift } = require('auxin');

                let arr = [1, 2, 3];
                arr = arrShift(arr);
                console.log(arr); // Output: [2, 3]
                

arrSlice

Extracts a section of an array and returns a new array.

Parameters

Returns

A new array containing the sliced elements.

Example

const { arrSlice } = require('auxin');

                const arr = [1, 2, 3, 4, 5];
                const slicedArr = arrSlice(arr, 1, 3);
                console.log(slicedArr); // Output: [2, 3]
                

arrToUpperCase

Converts all string elements of an array to uppercase.

Parameters

Returns

A new array with all string elements converted to uppercase.

Example

const { arrToUpperCase } = require('auxin');

                const arr = ['hello', 'world'];
                const upperCaseArr = arrToUpperCase(arr);
                console.log(upperCaseArr); // Output: ['HELLO', 'WORLD']
                

arrToLowerCase

Converts all string elements of an array to lowercase.

Parameters

Returns

A new array with all string elements converted to lowercase.

Example

const { arrToLowerCase } = require('auxin');

                const arr = ['HELLO', 'WORLD'];
                const lowerCaseArr = arrToLowerCase(arr);
                console.log(lowerCaseArr); // Output: ['hello', 'world']
                

arrayUtils

Exports all array utility functions.

Example

const arrayUtils = require('auxin');

                const arr = [1, 2, 3, 4, 5];
                const filteredArr = arrayUtils.filterArray(arr, num => num % 2 === 0);
                console.log(filteredArr); // Output: [2, 4]
                

quickSortDescending

Sorts an array in descending order using the Quick Sort algorithm.

Parameters

Returns

A new array containing the sorted elements in descending order.

Example

const { quickSortDescending } = require('auxin');

                const arr = [3, 1, 4, 1, 5, 9, 2, 6, 5];
                const sortedArr = quickSortDescending(arr);
                console.log(sortedArr); // Output: [9, 6, 5, 5, 4, 3, 2, 1, 1]