Skip to main content
Home
Works with
This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score100%
Downloads1/wk
Published3 months ago (0.0.3)

@differ/core provides foundational algorithms and utilities for calculating differences between sequences and objects. It is designed to be a low-level library for use in higher-level diffing and formatting tools.

@differ/core (WIP)

Work in Progress:
This library is under active development. Expect breaking changes and improvements. Contributions and feedback are welcome!

Part of the differ Suite

This package is part of the differ suite — a collection of tools and libraries for calculating and visualizing differences between code, sequences, and objects. It is under active development, with more features planned.

Overview

@differ/core provides foundational algorithms and utilities for calculating differences between sequences and objects. It is designed to be a low-level library for use in higher-level diffing and formatting tools.

Features

  • Longest Common Subsequence (LCS) calculation for arrays
  • Array diffing with detailed change tracking
  • Object diffing with nested property support
  • Map diffing with added, removed, modified, and unchanged entries
  • Set diffing with added, removed, and unchanged elements
  • Character-level diffing for strings
  • Customizable comparison functions

Usage Examples

LCS (Longest Common Subsequence)

import { lcs } from "@differ/core";

const a = ["A", "B", "C", "D"];
const b = ["A", "X", "C", "D"];
const dp = lcs(a, b);
// dp is a 2D array representing the LCS lengths

Array Diff

import { sequentialDiff } from "@differ/core";

const before = ["A", "B", "C"];
const after = ["A", "X", "C"];
const diff = sequentialDiff(before, after);
//Outputs:
//[
//  { type: "same", content: "A" },
//  { type: "delete", content: "B" },
//  { type: "add", content: "X" },
//  { type: "same", content: "C" }
//]

Object Diff

import { diffObjects, formatObjectDiff } from "@differ/core";

const before = { a: 1, b: 2 };
const after = { a: 1, b: 3, c: 4 };
const diffs = diffObjects(before, after);
console.log(formatObjectDiff(diffs));
//Outputs:
//~ b: 2 → 3
//+ c: 4

Map Diff

import { diffMaps } from "@differ/core";

const before = new Map([
    ["x", 1],
    ["y", 2],
]);
const after = new Map([
    ["y", 2],
    ["z", 3],
]);
const mapDiff = diffMaps(before, after);
// Outputs:
//[
//  { type: "removed", key: "x", value: 1 },
//  { type: "added", key: "z", value: 3 }
//]

Set Diff

import { diffSets } from "@differ/core";

const before = new Set([1, 2, 3]);
const after = new Set([2, 3, 4]);
const setDiff = diffSets(before, after);
//Outputs:
//[
//  { type: "removed", value: 1 },
//  { type: "added", value: 4 }
//]

Character Diff

import { characterDiff } from "@differ/core";

const before = "kitten";
const after = "sitting";
const charDiff = characterDiff(before, after);
//Outputs:
//[
//  { type: "delete", content: "k" },
//  { type: "add", content: "s" },
//  { type: "same", content: "itt" },
//  { type: "delete", content: "e" },
//  { type: "add", content: "i" },
//  { type: "same", content: "n" },
//  { type: "add", content: "g" }
//]

API

  • lcs(a, b, compareFn?): Returns a 2D array for LCS calculation.
  • sequentialDiff(before, after, options?): Returns an array of diff entries for two arrays.
  • characterDiff(before, after): Returns an array of character-level diff entries for two strings.
  • diffObjects(before, after, options?): Returns an array of object diff entries.
  • formatObjectDiff(diffs): Formats object diffs as a string.
  • diffMaps(before, after, showUnchanged?, compareFn?): Returns an array of map diff entries.
  • diffSets(before, after, showUnchanged?): Returns an array of set diff entries.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@differ/core

Import symbol

import * as core from "@differ/core";
or

Import directly with a jsr specifier

import * as core from "jsr:@differ/core";

Add Package

pnpm i jsr:@differ/core
or (using pnpm 10.8 or older)
pnpm dlx jsr add @differ/core

Import symbol

import * as core from "@differ/core";

Add Package

yarn add jsr:@differ/core
or (using Yarn 4.8 or older)
yarn dlx jsr add @differ/core

Import symbol

import * as core from "@differ/core";

Add Package

vlt install jsr:@differ/core

Import symbol

import * as core from "@differ/core";

Add Package

npx jsr add @differ/core

Import symbol

import * as core from "@differ/core";

Add Package

bunx jsr add @differ/core

Import symbol

import * as core from "@differ/core";