@minikit/array
TypeScript icon, indicating that this package has built-in type declarations

0.2.0 • Public • Published

@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"
  • 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...
  • Each method depended on other method

License

MIT © azu & billgo

Readme

Keywords

Package Sidebar

Install

npm i @minikit/array

Weekly Downloads

1

Version

0.2.0

License

MIT

Unpacked Size

9.21 kB

Total Files

5

Last publish

Collaborators

  • billgo