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.
filterArrayFilters an array based on a predicate function.
arr: The array to filter.predicate: The predicate function used to filter elements.A new array containing only the elements that satisfy the predicate function.
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]
mapArrayMaps each element of an array to a new value using a transformation function.
arr: The array to map.mapper: The transformation function to apply to each element.A new array containing the transformed elements.
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]
binarySearchPerforms a binary search on a sorted array to find the index of a target element.
arr: The sorted array to search.target: The target element to find.The index of the target element if found, otherwise -1.
const { binarySearch } = require('auxin');
const arr = [1, 2, 3, 4, 5];
const index = binarySearch(arr, 3);
console.log(index); // Output: 2
quickSortSorts an array using the Quick Sort algorithm.
arr: The array to sort.A new array containing the sorted elements.
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]
arrPushAppends an element to the end of an array.
arr: The array to modify.target: The element to append to the array.The modified array with the element appended.
const { arrPush } = require('auxin');
let arr = [1, 2, 3];
arr = arrPush(arr, 4);
console.log(arr); // Output: [1, 2, 3, 4]
arrUnshiftPrepends an element to the beginning of an array.
arr: The array to modify.target: The element to prepend to the array.The modified array with the element prepended.
const { arrUnshift } = require('auxin');
let arr = [2, 3, 4];
arr = arrUnshift(arr, 1);
console.log(arr); // Output: [1, 2, 3, 4]
arrPopRemoves the last element from an array.
arr: The array to modify.The modified array with the last element removed.
const { arrPop } = require('auxin');
let arr = [1, 2, 3];
arr = arrPop(arr);
console.log(arr); // Output: [1, 2]
arrShiftRemoves the first element from an array.
arr: The array to modify.The modified array with the first element removed.
const { arrShift } = require('auxin');
let arr = [1, 2, 3];
arr = arrShift(arr);
console.log(arr); // Output: [2, 3]
arrSliceExtracts a section of an array and returns a new array.
arr: The array to slice.start: The start index.end: The end index (exclusive).A new array containing the sliced elements.
const { arrSlice } = require('auxin');
const arr = [1, 2, 3, 4, 5];
const slicedArr = arrSlice(arr, 1, 3);
console.log(slicedArr); // Output: [2, 3]
arrToUpperCaseConverts all string elements of an array to uppercase.
arr: The array to modify.A new array with all string elements converted to uppercase.
const { arrToUpperCase } = require('auxin');
const arr = ['hello', 'world'];
const upperCaseArr = arrToUpperCase(arr);
console.log(upperCaseArr); // Output: ['HELLO', 'WORLD']
arrToLowerCaseConverts all string elements of an array to lowercase.
arr: The array to modify.A new array with all string elements converted to lowercase.
const { arrToLowerCase } = require('auxin');
const arr = ['HELLO', 'WORLD'];
const lowerCaseArr = arrToLowerCase(arr);
console.log(lowerCaseArr); // Output: ['hello', 'world']
arrayUtilsExports all array utility functions.
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]
quickSortDescendingSorts an array in descending order using the Quick Sort algorithm.
arr: The array to sort.A new array containing the sorted elements in descending order.
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]