@minikit/array
v0.2.0
Published
Immutable Array prototype methods.
Downloads
1
Readme
@minikit/array
Immutable Array prototype methods.
- TypeScript
- Small and Thin
@minikit/array
that includes all methods: ~500bytes(gzip+minify)
- Same usage with native
Array.prototype
methods
Getting Started
# install dependencies
bun add @minikit/array
Usage
@minikit/array
is a collection of immutable Array.prototype
methods.
Basically, the usage of these method is same with mutable version.
import { sort, unshift, push, fill, splice, pop, reverse, copyWithin, shift } from "@minikit/array";
describe("prototype", () => {
it("shift", () => {
assert.deepStrictEqual(shift(["a", "b", "c", "d", "e"]), ["b", "c", "d", "e"]);
});
it("unshift", () => {
assert.deepStrictEqual(unshift(["a", "b", "c", "d", "e"], "x"), ["x", "a", "b", "c", "d", "e"]);
});
it("pop", () => {
assert.deepStrictEqual(pop(["a", "b", "c", "d", "e"]), ["a", "b", "c", "d"]);
});
it("push", () => {
assert.deepStrictEqual(push(["a", "b", "c", "d", "e"], "x"), ["a", "b", "c", "d", "e", "x"]);
});
it("splice", () => {
assert.deepStrictEqual(splice(["a", "b", "c", "d", "e"], 0, 1, "x"), ["x", "b", "c", "d", "e"]);
});
it("sort", () => {
assert.deepStrictEqual(sort(["e", "a", "c", "b", "d"]), ["a", "b", "c", "d", "e"]);
});
it("reverse", () => {
assert.deepStrictEqual(reverse(["a", "b", "c", "d", "e"]), ["e", "d", "c", "b", "a"]);
});
it("fill", () => {
assert.deepStrictEqual(fill(new Array(5), "x"), ["x", "x", "x", "x", "x"]);
});
it("copyWithin", () => {
assert.deepStrictEqual(copyWithin(["a", "b", "c", "d", "e"], 0, 3, 4), ["d", "b", "c", "d", "e"]);
});
});
Support Policy
Do
- Provide immutable version of
Array.prototype
method - Provide each method as an module
- All prototype method:
import { push } from "@minikit/array"
- All prototype method:
- ECMAScript compatible API
For example, @minikit/array
method should return same result with native API.
import { splice } from "@minikit/array";
var array = [1, 2, 3];
// immutable
var resultArray = splice(array, -1, 1, "x");
// native
array.splice(-1, 1, "x");
assert.deepStrictEqual(array, resultArray);
Do not
- Add non-standard method in ECMAScript
- e.g.)
update
,delete
,merge
...
- e.g.)
- Each method depended on other method
Related
- Pure javascript immutable arrays
- georapbox/immutable-arrays: Immutable versions of normally mutable array methods
- micnews/immutable-array-methods
- kolodny/immutability-helper: mutate a copy of data without changing the original source
License
MIT © azu & billgo