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

fast-array-diff

v1.1.0

Published

Implementation of paper 'An O(ND) Difference Algorithm and Its Variations' on array

Downloads

70,931

Readme

fast-array-diff

Build Status Coverage Status npm version MIT Licence TypeScript

fast-array-diff is a npm module to find the common or different parts of two array, it based on the solution of LCS (Longest common subsequence) problems, widely used in diff/patch of two arrays (like diff/patch feature in git).

The algorithm of this module is implemented based on the paper "An O(ND) Difference Algorithm and its Variations" by Eugene Myers, Algorithm Vol. 1 No. 2, 1986, pp. 251-266. The difference of this implementation to the implementation of npm module diff is: the space complexity of this implementation is O(N), while the implementation of diff is O(ND), so this implementation will cost less memory on large data set. Note: although the time complexity of the implementations are both O(ND), this implementation run slower than the diff.

Installation

You can install the module via npm:

npm install fast-array-diff

API

  • same(arrayOld, arrayNew, compareFunc?) - Get the LCS of the two arrays.

    Return a list of the common subsequence. Like: [1,2,3]

    Note: The parameter compareFunc is optional, === will be used if no compare function supplied.

  • diff(arrayOld, arrayNew, compareFunc?) - Get the difference the two array.

    Return an object of the difference. Like this:

{
    removed: [1,2,3],
    added: [2,3,4]
}
  • getPatch(arrayOld, arrayNew, compareFunc?) - Get the patch array which transform from old array to the new.

    Return an array of edit action. Like this:

[
  { type: "remove", oldPos: 0, newPos: 0, items: [1] },
  { type: "add", oldPos: 3, newPos: 2, items: [4] },
];
  • applyPatch(arrayOld, patchArray) - Thansform the old array to the new from the input patch array

    Return the new Array. The input value format can be same of return value of getPatch, and for the remove type, the items can be replaced to length value which is number.

[
  { type: "remove", oldPos: 0, newPos: 0, items: [1] },
  { type: "add", oldPos: 3, newPos: 2, items: [4] },
  { type: "remove", oldPos: 5, newPos: 3, length: 3 },
];

Examples

Example for same on array of number:

var diff = require("fast-array-diff");

console.log(diff.same([1, 2, 3, 4], [2, 1, 4]));
// Output: [2, 4]

Example for diff on array of Object with a compare function

function compare(personA, personB) {
  return (
    personA.firstName === personB.firstName &&
    personA.lastName === personB.lastName
  );
}

var result = diff.diff(
  [
    { firstName: "Foo", lastName: "Bar" },
    { firstName: "Apple", lastName: "Banana" },
    { firstName: "Foo", lastName: "Bar" },
  ],
  [
    { firstName: "Apple", lastName: "Banana" },
    { firstName: "Square", lastName: "Triangle" },
  ],
  compare
);

// Result is :
// {
//    removed:[
//        { firstName: 'Foo', lastName: 'Bar' },
//        { firstName: 'Foo', lastName: 'Bar' }
//    ],
//    added: [ { firstName: 'Square', lastName: 'Triangle' } ]
// }

Example for getPatch on array of number:

var es = diff.getPatch([1, 2, 3], [2, 3, 4]);

// Result is:
// [
//     { type: "remove", oldPos: 0, newPos: 0, items: [1] },
//     { type: "add", oldPos: 3, newPos: 2, items: [4] },
// ]

Example for applyPatch:

var arr = diff.applyPatch(
  [1, 2, 3],
  [
    { type: "remove", oldPos: 0, newPos: 0, length: 1 },
    { type: "add", oldPos: 3, newPos: 2, items: [4] },
  ]
);

// Result is:
// [2, 3, 4]

TypeScript

This module is written in TypeScript, you can import it directly in TypeScript and get the benefit of static type checking and auto-complete of IDE.

import * as diff from "fast-array-diff";

console.log(diff.same([1, 2, 3], [2, 3, 4]));

let result: diff.DiffData<number> = diff.diff([1, 2], [2, 3]);
// Note: DiffData is the interface of the difference result.

License

This module is licensed under MIT.

Changelog