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

curlydiff

v2.0.1

Published

diff nested JavaScript objects

Downloads

8

Readme

curlydiff

diff nested JavaScript objects.

Given two JavaScript objects, return an object that represents the properties of the second that are different. This is a 1-way diff; the diff does not contain the "old" values.

This diff algorithm compares the contents of {...} objects. All other types of values (numbers, strings, etc.) are considered "primitive", and any change in a primitive value is represented in the diff as the entire new object.

Use of [...] arrays is discouraged, and will probably not do what you expect. Internally, [...] arrays are considered primitive values, and are checked for equality with the === operator, which will probably return false, even if the JSON representation of the arrays would be identical. curlydiff was not designed to diff objects that contain arrays.

The value null has special meaning in curlydiff, and so it should probably be avoided in the objects you're trying to diff. (null corresponds to the deletion of a property. See below for formal description of the patch format.)

If curlydiff is given an object that contains itself, such as var x = {}; x.self = x;, then curlydiff's behavior is undefined. (It will probably cause a "Maximum call stack size exceeded" error.)

Usage

var curlydiff = require(curlydiff);

curlydiff.diff({a:1}, {a:1});      // undefined
curlydiff.diff({a:1}, {a:2});      // {a:2}
curlydiff.diff({a:1}, {a:1, b:2}); // {b:2}
curlydiff.diff({a:1, b:2}, {a:1}); // {b:null}

curlydiff.diff({a:1, b:{}}, {a:1, b:{c:2}});    // {b:{c:2}}
curlydiff.diff({a:1, b:{c:2}}, {a:1, b:{}});    // {b:{c:null}}
curlydiff.diff({a:1, b:{c:2}}, {a:2, b:{c:2}}); // {a:2}

curlydiff.diff("hello", "hello"); // undefined
curlydiff.diff("hello", "world"); // "world"

curlydiff.diff(new Date(1234), new Date(1234)); // undefined

// this object will be modified in place
var data = {};
curlydiff.apply(data, {a:1});             // {a:1}
curlydiff.apply(data, {b:2});             // {a:1, b:2}
curlydiff.apply(data, {b:null});          // {a:1}
curlydiff.apply(data, {b:{c:3}});         // {a:1, b:{c:3}}
curlydiff.apply(data, {b:null, d:{e:4}}); // {a:1, d:{e:4}}

API

diff(from, to)

Returns a diff from from to to. Assuming from and to are {...} objects (according to isObject()), calling apply(from, diff(from, to)) will result in from being deep-equal (see below) to to. In general, from = apply(from, diff(from, to)); will always result in from being deep-equal to to.

If this function returns undefined, then from and to are already deep-equal. Otherwise, this function often returns a {...} object, which is a patch (see patch format below). If from and to are not both {...} objects (according to isObject()), then curlydiff does not perform any meaningful diff, and to is simply returned as-is to indicate that nothing is similar about the two objects. (Note that apply() will still behave meaningfully in this case.)

Iteration over object keys is performed with a native for-in loop with no regard for Object.hasOwnProperty().

apply(object, patch)

Applies patch to object in-place, and returns object. Assuming patch is a value that was returned from diff(object, to), the returned value will be deep-equal (see below) to the original to. If patch is undefined, then object is returned unaltered. If object and the original to are both {...} objects (according to isObject()), then object will be modified in-place, and will become deep-equal to the original to. Otherwise, patch is returned as-is, which corresponds to the case where object and to were not both {...} objects.

isObject(value)

Returns true iff curlydiff thinks that this object is a {...} object. The following must all be true:

  • value != null
  • typeof value === "object"
  • !Array.isArray(value)
  • !(value instanceof Date)

Whenever this function returns false, the value is considered a "primitive value".

Patch Format

A patch is obtained by calling diff(from, to).

A patch that is undefined, indicates that from and to are already deep-equal (see below). Otherwise:

  • If the patch is a primitive value (according to isObject()), then the patch is equal to the to value.
  • If the patch is a {...} object (according to isObject()), it means the to value is a {...} object, and:
    • If a key is not present in the patch, it indicates that the values match in the two objects.
    • If a key maps to null, it means the value was deleted.
    • Otherwise, the value is a patch between the properties of the objects (and will not be undefined).

Deep Equality

Deep equality is defined for a and b recursively:

  • if isObject(a) and isObject(b):
    • a and b have the same set of keys, and for all keys:
      • a[key] is deep-equal to b[key]
  • if a instanceof Date and b instanceof Date:
    • a.getTime() === b.getTime()
  • otherwise a === b

Note that arrays are not well supported by this definition.