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

diff-leven

v1.0.0

Published

Git like diff between two strings, using the Levenshtein distance algorithm

Readme

diff-leven

Git-like diff between two strings or objects, powered by the Levenshtein distance algorithm

npm version npm jsdelivr License

Try now: link


✨ Features

  • Advanced Diff Generation: Uses the Levenshtein distance algorithm for meaningful diffs

  • Multiple Data Type Support:

    • Objects (including nested structures)
    • Arrays (positional comparison; no reordering/LCS)
    • Strings (character-level differences)
    • Numbers, Booleans, and any serializable value
  • Rich Output Options:

    • Git-style colorized output diff format with clear additions/removals
  • Flexible Configuration:

    • color: Toggle color output (default: true)
    • keysOnly: Compare only object structure/keys (default: false)
    • full: Output the entire object tree, not just differences (default: false)
    • outputKeys: Always include specified keys in output for objects with differences
    • ignoreKeys: Skip specified keys when comparing objects
    • ignoreValues: Ignore value differences, focus on structure

🚀 Quick Start

1. Install

npm install diff-leven

2. Usage

const { diff } = require('diff-leven');

console.log(diff({ foo: 'bar' }, { foo: 'baz' }));
// Output:
//  {
// -  foo: "bar"
// +  foo: "baz"
//  }

🛠️ API Reference

diff(a, b, options?)

Compare two values (strings, objects, arrays, etc.) and return a formatted diff string.

Note on arrays: comparison is positional only (index-by-index). Reordered elements are treated as removals/additions rather than matched by similarity.

Parameters

  • a, b: Anything serializable (object, array, string, number, etc.)
  • options (optional object):
    • color (boolean): Use colors in output (default: true)
    • keysOnly (boolean): Only compare object keys (default: false)
    • full (boolean): Output the entire JSON tree (default: false)
    • outputKeys (string[]): Always include these keys in output (default: [])
    • ignoreKeys (string[]): Ignore these keys when comparing (default: [])
    • ignoreValues (boolean): Ignore value differences (default: false)

Returns

  • A string representing the diff between a and b.

diffRaw(a, b, options?)

Compare two values (strings, objects, arrays, etc.) and return a structured diff result object.

Parameters

  • a, b: Anything serializable (object, array, string, number, etc.)
  • options (optional object):
    • color (boolean): Use colors in output (default: true)
    • keysOnly (boolean): Only compare object keys (default: false)
    • full (boolean): Output the entire JSON tree (default: false)
    • outputKeys (string[]): Always include these keys in output (default: [])
    • ignoreKeys (string[]): Ignore these keys when comparing (default: [])
    • ignoreValues (boolean): Ignore value differences (default: false)

Returns

  • A structured object representing the diff between a and b.

isDiff(a, b, options?)

Check if two values (strings, objects, arrays, etc.) are different and return a boolean result.

Parameters

  • a, b: Anything serializable (object, array, string, number, etc.)
  • options (optional object):
    • keysOnly (boolean): Only compare object keys (default: false)
    • ignoreKeys (string[]): Ignore these keys when comparing (default: [])
    • ignoreValues (boolean): Ignore value differences (default: false)

Returns

  • A boolean indicating if the values are different (true = different, false = identical).

Examples

const { diff, diffRaw, isDiff } = require('diff-leven');

// Basic diff (string output)
console.log(diff({ foo: 'bar' }, { foo: 'baz' }));
// Output:
//  {
// -  foo: "bar"
// +  foo: "baz"
//  }

// Raw diff object
const rawDiff = diffRaw({ foo: 'bar' }, { foo: 'baz' });
console.log(JSON.stringify(rawDiff, null, 2));
// Output:
// {
//   "type": "changed",
//   "path": [],
//   "oldValue": { "foo": "bar" },
//   "newValue": { "foo": "baz" },
//   "children": [
//     {
//       "type": "changed",
//       "path": ["foo"],
//       "oldValue": "bar",
//       "newValue": "baz"
//     }
//   ]
// }

// Boolean diff check
console.log(isDiff({ foo: 'bar' }, { foo: 'baz' }));
// Output: true

console.log(isDiff({ foo: 'bar' }, { foo: 'bar' }));
// Output: false

// With options
console.log(
  isDiff(
    { foo: 'bar', timestamp: 123 },
    { foo: 'bar', timestamp: 456 },
    { ignoreKeys: ['timestamp'] },
  ),
);
// Output: false (identical when ignoring timestamp)

// No colors
console.log(diff({ foo: 'bar' }, { foo: 'baz' }, { color: false }));
// Output:
//  {
// -  foo: "bar"
// +  foo: "baz"
//  }

// Full output
console.log(diff({ foo: 'bar', b: 3 }, { foo: 'baz', b: 3 }, { full: true }));
// Output:
//  {
// -  foo: "bar"
// +  foo: "baz"
//    b: 3
//  }

// Ignore keys
console.log(
  diff({ foo: 'bar', b: 3 }, { foo: 'baz', b: 3 }, { ignoreKeys: ['b'] }),
);
// Output:
//  {
// -  foo: "bar"
// +  foo: "baz"
//  }

// Ignore values
console.log(
  diff({ foo: 'bar', b: 3 }, { foo: 'baz', b: 3 }, { ignoreValues: true }),
);
// Output showing structural differences only

// Show similarity info for string changes
console.log(
  diff('hello world', 'hello there', { color: true, withSimilarity: true }),
);
// Output:
// - 'hello world'
// + 'hello there' (73% similar)

// Output specific keys
console.log(
  diff({ foo: 'bar', b: 3 }, { foo: 'baz', b: 3 }, { outputKeys: ['foo'] }),
);
// Output:
//  {
// -  foo: "bar"
// +  foo: "baz"
//  }

// Combine options
console.log(
  diff(
    { foo: 'bar', b: 3 },
    { foo: 'baz', b: 3 },
    {
      keysOnly: true,
      ignoreKeys: ['b'],
      ignoreValues: true,
      outputKeys: ['foo'],
      full: true,
      color: false,
    },
  ),
);

⚙️ Options Matrix

| Option | Type | Default | Description | | ---------------- | -------- | ------- | -------------------------------------------- | | color | boolean | true | Use colorized output | | keysOnly | boolean | false | Only compare object keys | | full | boolean | false | Output the entire object tree | | outputKeys | string[] | [] | Always include these keys in output | | ignoreKeys | string[] | [] | Ignore these keys when comparing | | ignoreValues | boolean | false | Ignore value differences, focus on structure | | withSimilarity | boolean | false | Show similarity info for string changes |


📦 Examples

See examples/basic.js for more usage patterns.


🤝 Contributing

  1. Fork the repo
  2. Create your feature branch (git checkout -b feature/YourFeature)
  3. Commit your changes (git commit -am 'Add new feature')
  4. Push to the branch (git push origin feature/YourFeature)
  5. Open a pull request

📄 License

MIT © kushalshit27