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

chardiff

v0.1.2

Published

Character-level text diffs respecting line boundaries

Downloads

3

Readme

CharDiff

Character-level text diffs respecting line boundaries.

This module first computes diffs on the line-level, matches similar lines, and then proceeds to computing char-level diffs for changed lines. It's based on diff, which uses the algorithm proposed in "An O(ND) Difference Algorithm and its Variations" (Myers, 1986).

Computing diffs in this two-step manner has the following advantages:

  1. It can generate better diffs for inherently line-based text.
  2. Its generated diffs are often easier for humans to read and comprehend because humans are accustomed to reading text (and diffs) line by line.

Install

npm install chardiff

Example

    var diff = require('chardiff');

    var text1 = 'The quick brown\nfox jumps over\nthe lazy dog\n';
    var text2 = 'The fast, brown,\nclever, lovely animal\n' +
                'people say is a fox\njumps over something,\nwhat is it?\n';

    // Diff two blocks of text, get an array of line changes.
    var changeset = diff(text1, text2);
    // Count the number of lines in each block of text.
    var lineCount1 = 0,
        lineCount2 = 0;
    // ANSI color codes used to colorize the diff for printing.
    var red = '\x1B[31m',
        green = '\x1B[32m',
        cyan = '\x1B[36m',
        clear = '\x1B[0m';
    // Holds the colorized diff.
    var result = '';

    // Each change in the set contains one line, either an added line, a removed
    // line, a changed line, or an unchanged line.
    for (var i = 0; i < changeset.length; i++) {
        var change = changeset[i];

        if (change.type === '=') {
            // This is an unchanged line.
            result += change.value;
            lineCount1++;
            lineCount2++;
        } else if (change.type === '-') {
            // This is a removed line.
            result += red + change.left + clear;
            lineCount1++;
        } else if (change.type === '+') {
            // This is an added line.
            result += green + change.right + clear;
            lineCount2++;
        } else {    // change.type === '*'
            // This is a changed line.
            // This entry has a property `diff` that holds char-level diffs in
            // this line.
            for (var j = 0; j < change.diff.length; j++) {
                var chg = change.diff[j];

                if (chg.type === '=') {
                    // These are unchanged chars.
                    result += chg.value;
                } else if (chg.type === '-') {
                    // These are removed chars.
                    result += red + chg.left + clear;
                } else if (chg.type === '+') {
                    // These are added chars.
                    result += green + chg.right + clear;
                } else {
                    // These are changed chars.
                    result += red + chg.left + clear;
                    result += green + chg.right + clear;
                }
            }
            lineCount1++;
            lineCount2++;
        }
    }

    // Print text1, text2 and their respective line counts.
    console.log(cyan + 'text1 (%s lines):' + clear, lineCount1);
    console.log(text1);
    console.log();

    console.log(cyan + 'text2 (%s lines):' + clear, lineCount2);
    console.log(text2);
    console.log();

    // Print the colorized diff.
    console.log(cyan + 'Diff genereated by chardiff:' + clear);
    console.log(result);
    console.log();

Change Object

This module provides a function that calculates string diffs. It takes two strings as arguments, which we call the "left" string and the "right" string, respectively. They are also called "old" and "new" strings in other contexts.

The result returned by this function is an array of change objects. A change object has the following properties:

  1. type. The type of this change. It can be one of four values:

    • "=". The value of this change object exists in both strings.
    • "-". The left of this change object was removed from the left string.
    • "+". The right of this change object was added to the right string.
    • "*". The left of this change object was removed from the left string and the right was added to the right string.
  2. value. Only used if type is "=".

  3. left. Only used if type is "-" or "*".

  4. right. Only used if type is "+" or "*".

  5. diff. Each line-level change object of type "*" also contains this property, which holds an array of char-level change objects describing diffs between the left and right of this change. Character-level change objects have the same format as line-level change objects, except that those of type "*" don't have a diff property.

License

MIT License