phane-tech-array-utils
v1.0.2
Published
Pure JavaScript array utility functions
Downloads
203
Maintainers
Readme
📦 Kotipalli Phaneendra Kumar - Array Utilities
A lightweight, dependency-free JavaScript module that provides safe and predictable array utility functions.
These helpers make it easy to add, remove, search, transform, and validate arrays while handling edge cases such as null, undefined, empty arrays, and invalid inputs.
Designed to be minimal, consistent, and well-tested for both Node.js and browser environments.
✨ Highlights
- 📚 Safe array helpers
- ➕ Add / remove utilities
- 🔍 Search & match helpers
- 🔁 Transform utilities
- 🧪 Extensive Jest test coverage
- ⚡ Zero dependencies
📦 Installation
npm install phane-tech-array-utils🚀 Import
import {
getArrayLength,
addItemToAnArray,
addItemsInFrontToAnArray,
addOrRemoveItemsByIndex,
removeFirstItemOfAnArray,
removeLastItemOfAnArray,
mapAnArray,
filterAnArray,
findArrayItem,
findIndexArrayItem,
checkIsMatched,
checkIsAllMatched,
sortAnArray,
flatAnArray,
merginMultipleArray,
countItem
} from "phane-tech-array-utils";📚 API Reference
📏 getArrayLength(arr)
Returns the length of an array.
getArrayLength([1,2,3]); // 3
getArrayLength([]); // 0➕ addItemToAnArray(arr, item)
Adds an item to the end of the array.
addItemToAnArray([1,2], 3); // [1,2,3]➕ addItemsInFrontToAnArray(arr, ...items)
Adds items to the beginning of an array.
addItemsInFrontToAnArray([2,3], 1); // [1,2,3]🔄 addOrRemoveItemsByIndex(arr, startIndex, count, ...items)
Adds or removes items using index.
addOrRemoveItemsByIndex([1,2,3], 1, 1); // [1,3]➖ removeLastItemOfAnArray(arr)
Removes the last element of an array.
removeLastItemOfAnArray([1,2,3]); // [1,2]➖ removeFirstItemOfAnArray(arr)
Removes the first element of an array.
removeFirstItemOfAnArray([1,2,3]); // [2,3]🔁 mapAnArray(arr, callback)
Safely maps array values.
mapAnArray([1,2,3], n => n * 2); // [2,4,6]🔍 filterAnArray(arr, condition , callback)
Filters array values.
filterAnArray([1,2,3], 2); // [2]
filterAnArray([{a:1},{a:2}], {a:1}); // [{a:1}]🔎 findArrayItem(arr, condition , callback)
Finds the first matched item.
findArrayItem([1,2,3], 2); // 2🔢 findIndexArrayItem(arr, callback)
Returns the index of the matched item.
findIndexArrayItem([1,2,3], v => v === 2); // 1✅ checkIsMatched(arr, condition , callback)
Checks if any item matches.
checkIsMatched([1,2,3], 2); // true✅ checkIsAllMatched(arr, condition , callback)
Checks if all items match.
checkIsAllMatched([2,2,2], 2); // true🔀 sortAnArray(arr, key?)
Sorts an array or array of objects.
sortAnArray([3,1,2]); // [1,2,3]
sortAnArray([{a:2},{a:1}], "a"); // [{a:1},{a:2}]🧩 flatAnArray(arr)
Deep flattens nested arrays.
flatAnArray([1,[],[2,[3]]]); // [1,2,3]🔗 merginMultipleArray(...arrays)
Merges multiple arrays into one.
merginMultipleArray([1],[2],[3]); // [1,2,3]➕ countItem(arr, key?)
Counts or sums array values.
countItem([1,2,3]); // 6
countItem([{a:1},{a:2}], "a"); // 3📄 License
MIT
🔗 Links
- GitHub Repository: https://github.com/phane-tech/phane-tech-array-utils
- Demo / Documentation: https://phane-tech.github.io/phane-tech-array-utils/module-ArrayHelpers.html
- Unit Test Cases Reports: https://phane-tech.github.io/phane-tech-array-utils/unit-test-report.html
