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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@monstermann/delta

v0.3.0

Published

Functional operational-transform.

Readme

Minified Minzipped

Functional operational-transform.

Documentation

Differences from quill-delta

This library has been largely ported from quill-delta, some differences:

  • Immutable with optional transient mutations
  • Functional data-first/data-last API acting upon plain arrays
  • Support for nested attributes has been removed
  • Cloning is only done when the data actually changes
  • Deep-cloning has been replaced with shallow-cloning
  • Around 50% smaller

Example

import { Delta } from "@monstermann/delta";

// Create a document
const doc = Delta.insert([], "Hello world");

// Create a change that makes "Hello" bold
const change = Delta.retain([], 5, { bold: true });

// Apply the change
const result = Delta.compose(doc, change);
// [{ insert: "Hello", attributes: { bold: true } },
//  { insert: " world" }]

// Compute the difference between two documents
const a = Delta.insert([], "Hello");
const b = Delta.insert([], "Hello world");

Delta.diff(a, b);
// [{ retain: 5 },
//  { insert: " world" }]

Installation

npm install @monstermann/delta
pnpm add @monstermann/delta
yarn add @monstermann/delta
bun add @monstermann/delta

Delta

batch

function Delta.batch(
  ops: Delta,
  transform: (delta: Delta) => Delta,
): Delta

Batches multiple delta operations together for improved performance.

Example

import { Delta } from "@monstermann/delta";

Delta.batch([], (delta) => {
    // First change copies:
    delta = Delta.insert(delta, "Hello", { bold: true });
    // Other changes mutate:
    delta = Delta.insert(delta, " world");
    return delta;
});
// [{ insert: "Hello", attributes: { bold: true } },
//  { insert: " world" }]
import { Delta } from "@monstermann/delta";

pipe(
    [],
    Delta.batch((delta) => {
        // First change copies:
        delta = Delta.insert(delta, "Hello", { bold: true });
        // Other changes mutate:
        delta = Delta.insert(delta, " world");
        return delta;
    }),
);
// [{ insert: "Hello", attributes: { bold: true } },
//  { insert: " world" }]

chop

function Delta.chop(ops: Delta): Delta

Removes a trailing retain operation if it has no attributes.

Example

import { Delta } from "@monstermann/delta";

Delta.chop(pipe(
    [],
    Delta.insert("Hello"),
    Delta.retain(5)
));
// [{ insert: "Hello" }]

Delta.chop(pipe(
    [],
    Delta.insert("Hello"),
    Delta.retain(5, { bold: true })
));
// [{ insert: "Hello" },
//  { retain: 5, attributes: { bold: true } }]
import { Delta } from "@monstermann/delta";

pipe(
    [],
    Delta.insert("Hello"),
    Delta.retain(5),
    Delta.chop()
);
// [{ insert: "Hello" }]

clean

function Delta.clean(ops: Delta): Delta

Normalizes the delta by merging consecutive operations of the same type and attributes.

Example

import { Delta } from "@monstermann/delta";

Delta.clean(pipe(
    [],
    Delta.insert("Hello"),
    Delta.insert(" world")
));
// [{ insert: "Hello world" }]

Delta.clean(
    pipe(
        [],
        Delta.insert("Hello", { bold: true }),
        Delta.insert(" world", { bold: true }),
    ),
);
// [{ insert: "Hello world", attributes: { bold: true } }]
import { Delta } from "@monstermann/delta";

pipe(
    [],
    Delta.insert("Hello"),
    Delta.insert(" world"),
    Delta.clean()
);
// [{ insert: "Hello world" }]

compose

function Delta.compose(a: Delta, b: Delta): Delta

Composes two deltas into a single delta that represents applying a then b.

Example

import { Delta } from "@monstermann/delta";

const a = Delta.insert([], "Hello");
const b = pipe(
    [],
    Delta.retain(5),
    Delta.insert(" world")
);

Delta.compose(a, b);
// [{ insert: "Hello world" }]

const format = Delta.retain([], 5, { bold: true });

Delta.compose(a, format);
// [{ insert: "Hello", attributes: { bold: true } }]
import { Delta } from "@monstermann/delta";

const a = Delta.insert([], "Hello");
const b = pipe(
    [],
    Delta.retain(5),
    Delta.insert(" world")
);

pipe(a, Delta.compose(b));
// [{ insert: "Hello world" }]

concat

function Delta.concat(a: Delta, b: Delta): Delta

Concatenates two deltas together, merging adjacent operations if possible.

Example

import { Delta } from "@monstermann/delta";

const a = Delta.insert([], "Hello");
const b = Delta.insert([], " world");

Delta.concat(a, b);
// [{ insert: "Hello world" }]

const bold = Delta.insert([], "Hello", { bold: true });
const italic = Delta.insert([], " world", { italic: true });

Delta.concat(bold, italic);
// [{ insert: "Hello", attributes: { bold: true } },
//  { insert: " world", attributes: { italic: true } }]
import { Delta } from "@monstermann/delta";

const a = Delta.insert([], "Hello");
const b = Delta.insert([], " world");

pipe(a, Delta.concat(b));
// [{ insert: "Hello world" }]

diff

function Delta.diff(a: Delta, b: Delta, cursor?: number): Delta

Computes the difference between two document deltas, returning a delta that transforms a into b.

The optional cursor parameter provides a hint about where the user's cursor is positioned. This helps produce more intuitive diffs when there are multiple valid ways to represent the same change.

Example

import { Delta } from "@monstermann/delta";

const a = Delta.insert([], "Hello");
const b = Delta.insert([], "Hello world");

Delta.diff(a, b);
// [{ retain: 5 },
//  { insert: " world" }]

const plain = Delta.insert([], "Hello");
const bold = Delta.insert([], "Hello", { bold: true });

Delta.diff(plain, bold);
// [{ retain: 5, attributes: { bold: true } }]
import { Delta } from "@monstermann/delta";

const a = Delta.insert([], "Hello");
const b = Delta.insert([], "Hello world");

pipe(a, Delta.diff(b));
// [{ retain: 5 },
//  { insert: " world" }]

Cursor hint

When text changes are ambiguous, the cursor position determines where the change is placed:

import { Delta } from "@monstermann/delta";

const a = Delta.insert([], "foo");
const b = Delta.insert([], "foo bar foo");

// cursor=3: user typed " bar foo" at the end
Delta.diff(a, b, 3);
// [{ retain: 3 },
//  { insert: " bar foo" }]

// cursor=0: user typed "foo bar " at the beginning
Delta.diff(a, b, 0);
// [{ insert: "foo bar " }]

equals

function Delta.equals(a: Delta, b: Delta): boolean

Checks if two deltas are equal by comparing their operations and attributes.

Example

import { Delta } from "@monstermann/delta";

const a = Delta.insert([], "Hello", { bold: true });
const b = Delta.insert([], "Hello", { bold: true });
const c = Delta.insert([], "Hello", { italic: true });

Delta.equals(a, b); // true
Delta.equals(a, c); // false
import { Delta } from "@monstermann/delta";

const a = Delta.insert([], "Hello", { bold: true });
const b = Delta.insert([], "Hello", { bold: true });

pipe(a, Delta.equals(b)); // true

insert

function Delta.insert(
  ops: Delta,
  content: string | EmbedValue,
  attributes?: OpAttributes | null,
): Delta

Adds an insert operation to the delta.

Example

import { Delta } from "@monstermann/delta";

Delta.insert([], "Hello");
// [{ insert: "Hello" }]

Delta.insert([], "Hello", { bold: true });
// [{ insert: "Hello", attributes: { bold: true } }]
import { Delta } from "@monstermann/delta";

pipe([], Delta.insert("Hello"));
// [{ insert: "Hello" }]

pipe(
    [],
    Delta.insert("Hello", { bold: true }),
    Delta.insert(" world", { italic: true }),
);
// [{ insert: "Hello", attributes: { bold: true } },
//  { insert: " world", attributes: { italic: true } }]

invert

function Delta.invert(a: Delta, b: Delta): Delta

Returns the inverse of a delta against a base document. Applying the inverted delta undoes the original change.

Example

import { Delta } from "@monstermann/delta";

const base = Delta.insert([], "Hello");
const change = Delta.retain([], 5, { bold: true });

Delta.invert(change, base);
// [{ retain: 5, attributes: { bold: null } }]

const insert = pipe(
    [],
    Delta.retain(5),
    Delta.insert(" world")
);

Delta.invert(insert, base);
// [{ retain: 5 },
//  { delete: 6 }]
import { Delta } from "@monstermann/delta";

const base = Delta.insert([], "Hello");
const change = Delta.retain([], 5, { bold: true });

pipe(change, Delta.invert(base));
// [{ retain: 5, attributes: { bold: null } }]

length

function Delta.length(ops: Delta): number

Returns the total length of the delta (sum of all operation lengths).

Example

import { Delta } from "@monstermann/delta";

Delta.length(Delta.insert([], "Hello")); // 5

Delta.length(pipe(
    [],
    Delta.insert("Hello"),
    Delta.retain(3),
    Delta.remove(2)
)); // 10
import { Delta } from "@monstermann/delta";

pipe([], Delta.insert("Hello"), Delta.length()); // 5

pipe(
    [],
    Delta.insert("Hello"),
    Delta.retain(3),
    Delta.remove(2),
    Delta.length(),
); // 10

push

function Delta.push(ops: Delta, op: Op): Delta

Pushes an operation onto the delta, merging with the previous operation if possible.

Example

import { Delta } from "@monstermann/delta";

Delta.push([], { insert: "Hello" });
// [{ insert: "Hello" }]

Delta.push(Delta.push([], { insert: "Hello" }), { insert: " world" });
// [{ insert: "Hello world" }]
import { Delta } from "@monstermann/delta";

pipe([], Delta.push({ insert: "Hello" }));
// [{ insert: "Hello" }]

pipe([], Delta.push({ insert: "Hello" }), Delta.push({ insert: " world" }));
// [{ insert: "Hello world" }]

remove

function Delta.remove(ops: Delta, length: number): Delta

Adds a remove operation to the delta.

Example

import { Delta } from "@monstermann/delta";

Delta.remove([], 5);
// [{ delete: 5 }]
import { Delta } from "@monstermann/delta";

pipe([], Delta.remove(5));
// [{ delete: 5 }]

pipe([], Delta.retain(3), Delta.remove(5));
// [{ retain: 3 },
//  { delete: 5 }]

retain

function Delta.retain(
  ops: Delta,
  length: number,
  attributes?: OpAttributes | null,
): Delta

Adds a retain operation to the delta, optionally with attributes to apply formatting.

Example

import { Delta } from "@monstermann/delta";

Delta.retain([], 5);
// [{ retain: 5 }]

Delta.retain([], 5, { bold: true });
// [{ retain: 5, attributes: { bold: true } }]
import { Delta } from "@monstermann/delta";

pipe([], Delta.retain(5));
// [{ retain: 5 }]

pipe(
    [],
    Delta.retain(3),
    Delta.retain(2, { italic: true })
);
// [{ retain: 3 },
//  { retain: 2, attributes: { italic: true } }]

Removing attributes

Use null to remove an attribute when composing deltas:

import { Delta } from "@monstermann/delta";

const doc = Delta.insert([], "Hello", { bold: true });
// [{ insert: "Hello", attributes: { bold: true } }]

const removeBold = Delta.retain([], 5, { bold: null });
// [{ retain: 5, attributes: { bold: null } }]

Delta.compose(doc, removeBold);
// [{ insert: "Hello" }]

slice

function Delta.slice(ops: Delta, start: number, end?: number): Delta

Returns a portion of the delta from start to end.

Example

import { Delta } from "@monstermann/delta";

const delta = Delta.insert([], "Hello world");

Delta.slice(delta, 0, 5);
// [{ insert: "Hello" }]

Delta.slice(delta, 6);
// [{ insert: "world" }]

const formatted = pipe(
    [],
    Delta.insert("Hello", { bold: true }),
    Delta.insert(" world", { italic: true }),
);

Delta.slice(formatted, 3, 8);
// [{ insert: "lo", attributes: { bold: true } },
//  { insert: " wo", attributes: { italic: true } }]
import { Delta } from "@monstermann/delta";

pipe(
    [],
    Delta.insert("Hello world"),
    Delta.slice(0, 5)
);
// [{ insert: "Hello" }]

pipe(
    [],
    Delta.insert("Hello world"),
    Delta.slice(6)
);
// [{ insert: "world" }]

transform

function Delta.transform(
  a: Delta,
  b: Delta,
  priority?: boolean,
): Delta

Transforms delta b to account for delta a having been applied first. When both deltas insert at the same position, priority determines which insert comes first.

Example

import { Delta } from "@monstermann/delta";

const a = Delta.insert([], "Hello");
const b = Delta.insert([], "World");

Delta.transform(a, b, true);
// [{ retain: 5 },
//  { insert: "World" }]

Delta.transform(a, b, false);
// [{ insert: "World" }]

const format = Delta.retain([], 5, { bold: true });
const insert = pipe(
    [],
    Delta.retain(2),
    Delta.insert("XXX")
);

Delta.transform(insert, format);
// [{ retain: 8, attributes: { bold: true } }]
import { Delta } from "@monstermann/delta";

const a = Delta.insert([], "Hello");
const b = Delta.insert([], "World");

pipe(a, Delta.transform(b, true));
// [{ retain: 5 },
//  { insert: "World" }]

Op

length

function Op.length(op: Op): number

Returns the length of a single operation.

For string inserts this is the number of characters. For embed inserts this is always 1. For retain and remove operations this is the numeric value.

Example

import { Op } from "@monstermann/delta";

Op.length({ insert: "Hello" }); // 5
Op.length({ insert: { image: "..." } }); // 1
Op.length({ retain: 3 }); // 3
Op.length({ delete: 2 }); // 2