@elumixor/extensions
v2.1.3
Published
JavaScript/TypeScript extensions for Array, Set, and String prototypes
Maintainers
Readme
Extensions
JavaScript/TypeScript extensions for Array, Set, and String prototypes.
Installation
npm install @elumixor/extensionsor
bun add @elumixor/extensionsUsage
Simply import the package to add the extensions to the prototypes:
import "@elumixor/extensions";
// Array extensions
const arr = [1, 2, 3, 4, 5];
console.log(arr.first); // 1
console.log(arr.last); // 5
console.log(arr.sum); // 15
console.log(arr.take(3)); // [1, 2, 3]
// Set extensions
const set = new Set([1, 2, 3]);
console.log(set.first); // 1
console.log(set.isEmpty); // false
set.toggle(4); // adds 4
set.toggle(4); // removes 4
// String extensions
console.log("hello".capitalize()); // "Hello"Array Extensions
Accessors
first- Get/set first elementsecond- Get/set second elementlast- Get/set last elementisEmpty- Check if array is emptynonEmpty- Check if array is not emptyget(index)- Get element at index (supports negative indexes)
Math Operations
sum- Sum all elementsprod- Multiply all elementscumsum- Cumulative sumadd(other)- Element-wise additionsub(other)- Element-wise subtractionmax- Get maximum elementmin- Get minimum elementargmax- Get index of maximum elementargmin- Get index of minimum element
Manipulation
remove(...elements)- Remove elements from arrayremoveAt(index)- Remove element at indexinsertAt(element, index)- Insert element at indexset(index, value)- Set element at index (supports negative indexes)clear()- Clear arraytoggle(element)- Add if not present, remove if presentshuffle()- Shuffle array in-place
Slicing
take(n)- Take first n elementsskip(n)- Skip first n elementstakeLast(n)- Take last n elements
Utilities
count(element)- Count occurrences of elementunique(comparator?)- Get unique elementsbinarySplit(predicate)- Split into two arrays based on conditiontransposed- Transpose 2D arraypick()- Pick random elementpick(n)- Pick n random elements with repetitionpick(n, {repeat: false})- Pick n random elements without repetitionshuffled- Generator of shuffled elementsmapLazy(callback)- Lazy map using generator
Set Extensions
first- Get first elementisEmpty- Check if set is emptynonEmpty- Check if set is not emptytoggle(value)- Add if not present, remove if present
String Extensions
capitalize()- Capitalize first letter
License
ISC
