@elumixor/extensions
v2.3.0
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
Accessors
const arr = [1, 2, 3, 4, 5];
arr.first; // 1
arr.second; // 2
arr.last; // 5
arr.first = 10; // [10, 2, 3, 4, 5]
arr.last = 50; // [10, 2, 3, 4, 50]
arr.isEmpty; // false
arr.nonEmpty; // true
[].isEmpty; // trueMath
[1, 2, 3, 4].sum; // 10
[1, 2, 3, 4].prod; // 24
[1, 2, 3, 4].cumsum; // [1, 3, 6, 10]
[1, 5, 3].max; // 5
[1, 5, 3].min; // 1
[1, 5, 3].argmax; // 1
[1, 5, 3].argmin; // 0
[1, 2, 3].add([4, 5, 6]); // [5, 7, 9]
[4, 5, 6].sub([1, 2, 3]); // [3, 3, 3]Slicing
[1, 2, 3, 4, 5].take(3); // [1, 2, 3]
[1, 2, 3, 4, 5].skip(2); // [3, 4, 5]
[1, 2, 3, 4, 5].takeLast(2); // [4, 5]Manipulation (mutating)
const arr = [1, 2, 3, 4, 5];
arr.remove(3); // arr is [1, 2, 4, 5]
arr.removeAt(0); // arr is [2, 4, 5]
arr.insertAt(10, 1); // arr is [2, 10, 4, 5]
arr.clear(); // arr is []
[1, 2, 3].toggle(4); // [1, 2, 3, 4]
[1, 2, 3].toggle(2); // [1, 3]
[1, 2, 3, 4].shuffle(); // e.g. [3, 1, 4, 2] (in-place)Utilities
[1, 2, 2, 3, 3, 3].count(3); // 3
[1, 1, 2, 2, 3].unique(); // [1, 2, 3]
[1, 2, 3, 4].binarySplit(x => x > 2); // [[3, 4], [1, 2]]
[1, 2, 3].pick(); // random element
[1, 2, 3].pick(5); // 5 random elements (with repetition)
[1, 2, 3].pick(2, { repeat: false }); // 2 random elements (no repetition)
[1, 2, 3].shuffled; // generator yielding elements in random order
[1, 2, 3].mapLazy(x => x * 2); // generator yielding [2, 4, 6]Static Methods
Array.range(5); // [0, 1, 2, 3, 4]
Array.range(2, 5); // [2, 3, 4]Set
const set = new Set([1, 2, 3]);
set.first; // 1
set.isEmpty; // false
set.nonEmpty; // true
set.toggle(4); // adds 4 → Set {1, 2, 3, 4}
set.toggle(4); // removes 4 → Set {1, 2, 3}String
"hello".capitalize(); // "Hello"
"hello world".capitalize(); // "Hello world"
"Hello, {name}!".format({ name: "World" }); // "Hello, World!"
"{a} + {b} = {c}".format({ a: 1, b: 2, c: 3 }); // "1 + 2 = 3"
"hello-world".toCamelCase(); // "HelloWorld"
"foo_bar_baz".toCamelCase(); // "FooBarBaz"
"some text here".toCamelCase(); // "SomeTextHere"License
ISC
