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

commonform-diff

v1.0.2

Published

compare Common Forms

Downloads

6

Readme

Compare two Common Forms.

For example:

var diff = require('commonform-diff')
var assert = require('assert')

assert.deepEqual(
  diff(
    {content: ['a b c']},
    {content: ['a d']}
  )
  .content,
  [
    {word: 'a'},
    {word: ' '},
    {word: 'b', deleted: true},
    {word: ' ', deleted: true},
    {word: 'c', deleted: true},
    {word: 'd', inserted: true}
  ]
)

The function returns data that's "shaped" just like a Common Form, with two differences:

  1. Strings in content and heading properties are broken into objects like {word: 'hello'}.

  2. Word and other content objects have deleted and inserted properties to show changes.

  3. Every child has a heading array. Children without headings have empty heading arrays.

  4. Every form has a conspicuous array. Inconspicuous forms have empty conspicuous arrays.

Replacements are shown deletion-first:

The BuyerSeller shall ...

instead of:

The SellerBuyer shall ...

A few simple examples:

assert.deepEqual(
  diff(
    {content: ['A B C']},
    {content: ['A C']}
  )
  .content,
  [
    {word: 'A'},
    {word: ' '},
    {word: 'B', deleted: true},
    {word: ' ', deleted: true},
    {word: 'C'}
  ]
)

assert.deepEqual(
  diff(
    {content: ['A B C']},
    {content: ['A B C D']}
  )
  .content,
  [
    {word: 'A'},
    {word: ' '},
    {word: 'B'},
    {word: ' '},
    {word: 'C'},
    {word: ' ', inserted: true},
    {word: 'D', inserted: true}
  ]
)

assert.deepEqual(
  diff(
    {content: [{use: 'Seller'}, ' pays.']},
    {content: [{use: 'Buyer'}, ' pays.']}
  )
  .content,
  [
    {use: 'Seller', deleted: true},
    {use: 'Buyer', inserted: true},
    {word: ' '},
    {word: 'pays'},
    {word: '.'}
  ]
)

assert.deepEqual(
  diff(
    {content: [{use: 'Seller'}, ' shall pay all tax.']},
    {content: ['The ', {use: 'Buyer'}, ' shall pay no tax.']}
  )
  .content,
  [
    {use: 'Seller', deleted: true},
    {word: 'The', inserted: true},
    {word: ' ', inserted: true},
    {use: 'Buyer', inserted: true},
    {word: ' '},
    {word: 'shall'},
    {word: ' '},
    {word: 'pay'},
    {word: ' '},
    {word: 'all', deleted: true},
    {word: 'no', inserted: true},
    {word: ' '},
    {word: 'tax'},
    {word: '.'}
  ]
)

assert.deepEqual(
  diff(
    {content: ['The ', {use: 'Buyer'}, ' shall pay no tax.']},
    {content: [{use: 'Seller'}, ' shall pay all tax.']}
  )
  .content,
  [
    {word: 'The', deleted: true},
    {word: ' ', deleted: true},
    {use: 'Buyer', deleted: true},
    {use: 'Seller', inserted: true},
    {word: ' '},
    {word: 'shall'},
    {word: ' '},
    {word: 'pay'},
    {word: ' '},
    {word: 'no', deleted: true},
    {word: 'all', inserted: true},
    {word: ' '},
    {word: 'tax'},
    {word: '.'}
  ]
)

assert.deepEqual(
  diff(
    {content: [{form: {content: ['a b c']}}]},
    {content: [{form: {content: ['a d']}}]}
  )
  .content,
  [
    {
      heading: [],
      form: {
        conspicuous: [],
        content: [
          {word: 'a'},
          {word: ' '},
          {word: 'b', deleted: true},
          {word: ' ', deleted: true},
          {word: 'c', deleted: true},
          {word: 'd', inserted: true}
        ]
      }
    }
  ]
)

assert.deepEqual(
  diff(
    {content: ['a b c']},
    {content: ['a x c']}
  )
  .content,
  [
    {word: 'a'},
    {word: ' '},
    {word: 'b', deleted: true},
    {word: 'x', inserted: true},
    {word: ' '},
    {word: 'c'}
  ]
)

assert.deepEqual(
  diff(
    {content: ['Hello ', {form: {content: ['a b c']}}]},
    {content: [{form: {content: ['a d']}}]}
  )
  .content,
  [
    {word: 'Hello', deleted: true},
    {word: ' ', deleted: true},
    {
      heading: [],
      form: {
        conspicuous: [],
        content: [
          {word: 'a'},
          {word: ' '},
          {word: 'b', deleted: true},
          {word: ' ', deleted: true},
          {word: 'c', deleted: true},
          {word: 'd', inserted: true}
        ]
      }
    }
  ]
)

A few edit stacking examples:

assert.deepEqual(
  diff(
    {content: ['This is just a test.']},
    {content: ['This is an important provision.']}
  )
  .content,
  [
    {word: 'This'},
    {word: ' '},
    {word: 'is'},
    {word: ' '},
    {word: 'just', deleted: true},
    {word: ' ', deleted: true},
    {word: 'a', deleted: true},
    {word: ' ', deleted: true},
    {word: 'test', deleted: true},
    {word: 'an', inserted: true},
    {word: ' ', inserted: true},
    {word: 'important', inserted: true},
    {word: ' ', inserted: true},
    {word: 'provision', inserted: true},
    {word: '.'}
  ]
)

assert.deepEqual(
  diff(
    {content: ['The ', {use: 'Buyer'}, ' pays all tax.']},
    {content: ['The ', {use: 'Seller'}, ' withholds all tax.']}
  )
  .content,
  [
    {word: 'The'},
    {word: ' '},
    {use: 'Buyer', deleted: true},
    {word: ' ', deleted: true},
    {word: 'pays', deleted: true},
    {use: 'Seller', inserted: true},
    {word: ' ', inserted: true},
    {word: 'withholds', inserted: true},
    {word: ' '},
    {word: 'all'},
    {word: ' '},
    {word: 'tax'},
    {word: '.'}
  ]
)

assert.deepEqual(
  diff(
    {content: ['a b c d']},
    {content: ['a x y z']}
  )
  .content,
  [
    {word: 'a'},
    {word: ' '},
    {word: 'b', deleted: true},
    {word: ' ', deleted: true},
    {word: 'c', deleted: true},
    {word: ' ', deleted: true},
    {word: 'd', deleted: true},
    {word: 'x', inserted: true},
    {word: ' ', inserted: true},
    {word: 'y', inserted: true},
    {word: ' ', inserted: true},
    {word: 'z', inserted: true}
  ]
)

Changes to child form headings:

assert.deepEqual(
  diff(
    {content: [{heading: 'List of Words', form: {content: ['a b c']}}]},
    {content: [{heading: 'List of Items', form: {content: ['a b c']}}]}
  )
  .content,
  [
    {
      heading: [
        {word: 'List'},
        {word: ' '},
        {word: 'of'},
        {word: ' '},
        {word: 'Words', deleted: true},
        {word: 'Items', inserted: true}
      ],
      form: {
        conspicuous: [],
        content: [
          {word: 'a'},
          {word: ' '},
          {word: 'b'},
          {word: ' '},
          {word: 'c'}
        ]
      }
    }
  ]
)

assert.deepEqual(
  diff(
    {content: [{heading: 'x', form: {content: ['a']}}]},
    {content: [{form: {content: ['a']}}]}
  )
  .content,
  [
    {
      heading: [{word: 'x', deleted: true}],
      form: {
        conspicuous: [],
        content: [{word: 'a'}]
      }
    }
  ]
)

assert.deepEqual(
  diff(
    {content: [{form: {content: ['a']}}]},
    {content: [{heading: 'x', form: {content: ['a']}}]}
  )
  .content,
  [
    {
      heading: [{word: 'x', inserted: true}],
      form: {
        conspicuous: [],
        content: [{word: 'a'}]
      }
    }
  ]
)

Changes to form conspicuous presentation flags:

assert.deepEqual(
  diff(
    {content: [{form: {content: ['a']}}]},
    {content: [{form: {conspicuous: 'yes', content: ['a']}}]}
  )
  .content,
  [
    {
      heading: [],
      form: {
        conspicuous: [{word: 'yes', inserted: true}],
        content: [{word: 'a'}]
      }
    }
  ]
)

assert.deepEqual(
  diff(
    {content: [{form: {conspicuous: 'yes', content: ['a']}}]},
    {content: [{form: {content: ['a']}}]}
  )
  .content,
  [
    {
      heading: [],
      form: {
        conspicuous: [{word: 'yes', deleted: true}],
        content: [{word: 'a'}]
      }
    }
  ]
)

Addition of a child:

assert.deepEqual(
  diff(
    {content: [{form: {content: ['a']}}]},
    {content: [{form: {content: ['a']}}, {form: {content: ['b']}}]}
  )
  .content,
  [
    {
      heading: [],
      form: {conspicuous: [], content: [{word: 'a'}]}
    },
    {
      heading: [],
      form: {conspicuous: [], content: [{word: 'b'}]},
      inserted: true
    }
  ]
)