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

generic-diff

v1.0.1

Published

Diff any array-like object, including strings

Downloads

616

Readme

generic-diff

Diff arrays or array-like objects, such as strings. Based on "An O(ND) Difference Algorithm and its Variations" (Myers, 1986).

diff( a, b [, eql] )

Diffs the array-like objects a and b, returning a summary of edits required to turn a into b. Defaults to using strict equality (===) to compare items in a and b. A comparison function, eql, can optionally be used for more nuanced comparisons; the signature of this function is (item from a, item from b) => Boolean.

The “summary of changes” is an array of objects with three properties: items, an array of one or more items from a or b, and boolean properties added and removed, indicating whether the item(s) should be added or removed from a, respectively. For example, if we’re diffing the strings abc and abd, the summary of changes would look like:

[{
  items: ['a', 'b'],
  added: false,
  removed: false
}, {
  items: ['c'],
  added: false,
  removed: true
}, {
  items: ['d'],
  added: true,
  removed: false
}]

Example

Diff two strings, creating an HTML representation of their differences:

var diff = require('generic-diff')

var changes = diff('falafel', 'fallacy')
changes = changes.map(function (edit) {
  if (edit.added) {
    return '<ins>' + edit.items.join('') + '</ins>'
  } else if (edit.removed) {
    return '<del>' + edit.items.join('') + '</del>'
  } else {
    return edit.items.join('')
  }
}).join('')

console.log(changes)
// 'fal<del>afe</del>l<ins>acy</ins>'

For a slightly more involved example, this gist demonstrates how to diff two files and produce an output similar to the UNIX diff command.

LICENSE

MIT