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

myers-diff

v2.1.0

Published

Implementation of the longest common subsequence (diff) algorithm.

Downloads

11,753

Readme

myers-diff

A JavaScript test differentiation implementation based on An O(ND) Difference Algorithm and Its Variations (1986). It is a lightweight, low-level, no-frills library that can be used to build bulkier viewers.

Installation

$ npm install myers-diff

Getting started

With basic usage:

const myers = require('myers-diff');

const lhs = 'the quick red fox jumped\nover the hairy dog';
const rhs = 'the quick brown fox jumped\nover the lazy dog';

const diff = myers.diff(lhs, rhs);

console.log(myers.formats.GnuNormalFormat(diff));
console.log(diff);
//
// 1,2c1,2
// < the quick red fox jumped
// < over the hairy dog
// ---
// > the quick brown fox jumped
// > over the lazy dog

With all options:

const myers = require('myers-diff');

const lhs = 'the quick red fox jumped\nover the hairy dog';
const rhs = 'the quick brown fox jumped\nover the lazy dog';

const diff = myers.diff(lhs, rhs, {
    compare: 'lines',
    ignoreWhitespace: false,
    ignoreCase: false,
    ignoreAccents: false
});

For building visual editors:

const { diff, changed } = require('myers-diff');
const lhs = 'the quick red fox jumped\nover the hairy dog';
const rhs = 'the quick brown fox jumped\nover the lazy dog';
const changes = diff(lhs, rhs);

for (const change of changes) {
    if (changed(change.lhs)) {
        // deleted
        const { pos, text, del, length } = change.lhs;
    }
    if (changed(change.rhs)) {
        // added
        const { pos, text, add, length } = change.rhs;
    }
}

API

myers.diff(lhs, rhs, [options])

Compare lhs text to rhs text. Changes are compared from left to right such that items are deleted from left or added to right.

  • lhs <string> - The left-hand side text to compare.
  • rhs <string> - The right-hand side text to compare.
  • options <object> - Diff options.
    • ignoreWhitespace <boolean> - Ignores whitespace characters.
    • ignoreCase <boolean> - Ignores whitespace characters.
    • ignoreAccents <boolean> - Ignores accent characters.
  • Returns: <Change[]> - Returns an array of Change.

Change

An object that describes a change occurrence between the left-hand text and right-hand text.

LeftPart

Describes a left-hand change occurrence.

  • at <number> - The part item identifier. When comparing lines, it is the n-th line; when comparing words, it is the n-th word; when comparing chars, it is the n-th char.
  • del <number> - The number of parts deleted from the left. When comparing lines, it is the number of lines deleted; when comparing words, it is the number of words deleted; when comparing chars, it is the number of chars deleted.
  • pos <number> - The zero-based character position of the part from the original text.
  • text <string> - The text that was changed.
  • length <number> - The number of characters.

RightPart

Describes a right-hand change occurrence.

  • at <number> - The part item identifier. When comparing lines, it is the n-th line; when comparing words, it is the n-th word; when comparing chars, it is the n-th char.
  • add <number> - The number of parts added from the right. When comparing lines, it is the number of lines added; when comparing words, it is the number of words added; when comparing chars, it is the number of chars added.
  • pos <number> - The zero-based character position of the part from the original text.
  • text <string> - The text that was changed.
  • length <number> - The number of characters.

myers.formats

Formatting functions.

GnuNormalFormat(changes)

Formats an array of Change in GNU Normal Format.

myers.changed(part)

Examines the LeftPart or RightPart part to determine if was changed. It is possible for a Change to only affect one side or the other, or both. If changed, it returns true, otherwise, it returns false.

  • part <LeftPart | RightPart>- The left-hand part or right-hand part to compare.
  • Returns: <boolean> Returns true if changed, otherwise, it returns false.