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

mdiff

v1.4.2

Published

An LCS Implementaion for Arrays and Strings

Downloads

3,929

Readme

mdiff

npm version

A Minimalistic Diff Implementation

Based on the algorithm proposed in "An O(ND) Difference Algorithm and its Variations" (Myers, 1986). Works with Arrays and Strings.

Usage

$ npm install mdiff
import mdiff from 'mdiff';

let a = 'ABCABBA';
let b = 'CBABAC';
let diff = mdiff(a, b);

console.log("lcs='%s'", diff.getLcs());

console.log("Common:");
let d = diff.scanCommon((aS, aE, bS, bE) => {
  console.log("  '%s' == '%s'", a.slice(aS, aE), b.slice(bS, bE));
}); 
console.log("edit-distance=%s", d);

console.log("Diff:");
d = diff.scanDiff((aS, aE, bS, bE) => {
  console.log("  '%s' -> '%s'", a.slice(aS, aE), b.slice(bS, bE));
});
console.log("edit-distance=%s", d);

API

var diff = mdiff(a, b, options)

Creates a diff-object.

  • a, b (strings or arrays): the items to be compared.
  • options
    • equal (function(aValue, bValue) ): a comparator that determines if two entries are supposed to be equal (default is just ===).

    • indexEqual (function(aIdx, bIdx) ): a comparator that determines if entries for two indices are supposed to be equal (default is to compare a[aIdx] with b[bIdx] by equal).

diff.scanCommon(cb, dMax)

Calls cb for each common slice and returns the edit-distance d.

  • cb (function(aS, aE, bS, bE)): reports the corresponding slices with these guarantees:
    • non-emptiness: aE - aS == bE - bS > 0
    • monotony: aS is not less than aE from the previous call. bS is not less than bE from the previous call.
    • equality: a.slice(aS, aE) is equal b.slice(bS, bE) with respect to equal.
    • minimality: The sum of all aS - aE is equal to (a.length + b.length - d) / 2 and there is no lesser d so that 'equality' holds.
  • dMax (optional): the maximum edit-distance to be handled. If the items' edit-distance happens to exceed this value, the function never calls cb and returns null.

diff.scanDiff(cb, dMax)

Calls cb for each difference and returns the edit-distance d. This is just the complement of scanCommon.

  • cb (function(aS, aE, bS, bE)): reports differences with these guarantees:
    • non-emptiness: aE - aS > 0 or bE - bS > 0
    • monotony: aS is not less than aE from the previous call. bS is not less than bE from the previous call.
    • equality: If in a all slices a.slice(aS, aE) are replaced by their corresponding b.slice(bS, bE), the result is equal to b with respect to equal.
    • minimality: The sum of all aS - aE plus the sum of all bS - bE is equal to d and there is no lesser d so that 'equality' holds.
  • dMax (optional): the maximum edit-distance to be handled. If the items' edit-distance happens to exceed this value, the function never calls cb and returns null.

diff.getLcs(dMax)

Returns the longest common subsequence of a and b (as a's type) or null, if the items' edit-distance exceeds dMax.