npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@onsetsoftware/mutable-js

v0.6.0

Published

MutableJS is a zero-dependency library for mutating Javascript objects and arrays _in place_, where typically a new object or array would be created.

Downloads

9

Readme

MutableJS

MutableJS is a zero-dependency library for mutating Javascript objects and arrays in place, where typically a new object or array would be created.

This is especially useful when working with Automerge documents, where all changes are required to mutate the document in place.

Installation

npm install @onsetsoftware/mutable-js

Functions

Deduplicate

Removes duplicate values from an array.

import { deduplicate } from "@onsetsoftware/mutable-js";

const arr = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5];

deduplicate(arr); // => arr = [1, 2, 3, 4, 5]

Filter

Removes values from an array that do not match a predicate.

import { filter } from "@onsetsoftware/mutable-js";

const arr = [1, 2, 3, 4, 5];
filter(arr, (x) => x % 2 === 0); // => arr = [2, 4]

Map

Applies a function to each value in an array.

import { map } from "@onsetsoftware/mutable-js";

const arr = [1, 2, 3, 4, 5];
map(arr, (x) => x * 2); // => arr = [2, 4, 6, 8, 10]

Move Within

There are 2 functions for moving values within an array.

moveWithin(items: T[], insertIndex: number, indices: number[])

Moves items within an array to a new position, in the order defined by the indices.

import { moveWithin } from "@onsetsoftware/mutable-js";

const arr = ["a", "b", "c", "d", "e"];
const indices = [2, 4, 1];
moveWithin(arr, 0, indices); // => arr = ["c", "e", "b", "a", "d"]

moveWithinOrdered(items: T[], insertIndex: number, indices: number[])

Moves items within an array to a new position, keeping the order of the moved items in original array order, regardless of the order of the indices provided.

This is useful when (for example) a number of selections have been made in a list and the user wants to move them all to a new position, keeping them in the order of the original array

import { moveWithinOrdered } from "@onsetsoftware/mutable-js";

const arr = ["a", "b", "c", "d", "e"];
const indices = [2, 4, 1];
moveWithinOrdered(arr, 0, indices); // => arr = ["b", "c", "e", "a", "d"]

Entities

These are a set of mutable utilities for working with entities in an entity state:

import { EntityState } from "@onsetsoftware/mutable-js";

const people: EntityState<{ id: string }> = {
  ids: ["id-1"],
  entities: {
    "id-1": {
      name: "John",
      age: 20,
      id: "id-1",
    },
  },
};

The EntityState<T> type is included for your convenience.

export type EntityState<T extends { id: string }> = {
  ids: string[];
  entities: Record<string, T>;
};

addEntity

Adds an entity to an entity state.

import { EntityState, addEntity } from "@onsetsoftware/mutable-js";

const person: Person = {
  name: "Jane",
  id: "id-2",
  age: 21,
};

addEntity(people, person);

addEntities

Adds multiple entities to an entity state.

import { addEntities } from "@onsetsoftware/mutable-js";

const morePeople: Person[] = [
  {
    name: "Jane",
    id: "id-2",
    age: 21,
  },
  {
    name: "Jack",
    id: "id-3",
    age: 22,
  },
];

addEntities(people, morePeople);

updateEntity

Updates an entity in an entity state. This takes a partial entity (which must include an id).

const person = {
  id: "id-1",
  age: 21,
};

updateEntity(people, person); // => john's age === 21

deleteEntity

Deletes an entity from an entity state.

deleteEntity(people, "id-1"); // => people = { ids: [], entities: {} }

Update Array

Updates a source array in place, based on the values in a target array.

updateArray

const source = ["a", "b", "c"];
const target = ["b", "c", "a", "d"];
updateArray(source, target);

// source => ['b', 'c', 'a', 'd']