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

adiff

v0.2.13

Published

diff and patch arrays.

Downloads

12,094

Readme

adiff - Array diff tools is javacript

adiff is a minimal implementation of diff tools, diff, patch, diff3 in javascript.

testling badge

build status

I initially started writing this to understand how git works. then i got totally carried away. adiff is a central component in snob a self hosting port of git to javascript.

how git works.

if you want to know what is the difference between two files, you must first know what is the same. this is called the Longest Common Subsequence problem. if you have two sequences x = "ABDCEF" and y = "ABCXYZF"thenLCS(x,y)` is clearly "ABCF".

lcs

function lcs (a,b)
  if head(a) == head(b)
    then lcs(a,b) = head(a) + lcs(tail(a), tail(b))
  else lcs(a, b) = max(lcs(tail(a),b), lcs(a, tail(b)))

(where max returns the longer list, head return the first element, and tail returns the rest of the sequence minus the head)

this is very simple, but with exponential time complexity. however, it can easily be made sufficantly performant by cacheing the return value of each call to lcs().

see js implementation, index.js#L64-94

chunking

now, we can see when the strings differ, by comparing them to the lcs. the next step is dividing them into 'stable' chunks where they match the lcs, and unstable chunks where they differ.

basically, to go from chunk("ABDCEF", "ABCXYZF") to ["AB", ["D", ""], "C", ["E", "XYZ"], "F"]

note that stable and unstable chunks always alternate.

basically, you iterate over the sequences and while the heads match the head of the lcs, shift that value to a stable chunk. then, while the heads do not match the next head of the lcs, collect add those items into an unstable chunk.

diff

once you have the chunks getting a list of changes that you can apply is easy...

making a diff from a to b we want to know what changes to make to a to get b. the way I have node this Array#splice so, for ["AB", ["D", ""], "C", ["E", "XYZ"], "F"]we want:

  var changes = [
    [4, 1, 'X', 'Y', 'Z'], //delete 1 item ("E") at index 4, then insert "X", "Y", "Z"
    [2, 1] //delete 1 item at index 2 ("D")
  ]

note, you can apply changes to the end of the array without altering the indexes in the start of the array.

this makes the function to apply the patch very simple

patch

  function patch (orig, changes) {
    var ary = orig.split('') //assuming that orig is just a string
    changes.forEach(function (ch) {
      [].splice.apply(ary, ch)
    })
    return ary.join('')
  }

diff3

if we want a distributed version management system, the we need to be able to make changes in parallel. this is only a slightly more complicated problem. given a string "ABDCEF", If I changed it to "ABCXYZF" and meanwhile you changed it to "AXBCEFG". we must compare each of our changes to the original string, the Concestor

merging rules

TODO: worked example with chunks, resolve.

license

MIT / Apache2