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

simple-diff

v1.7.2

Published

simple diff for object and arrays with options

Downloads

91,523

Readme

simple-diff

Lib for simple diff with detection of moved items in array

Build Status

var oldObject = {
    prop1: 'value1',
    prop2: [
        {
            id: 1,
            name: 'name1'
        },
        {
            id: 2,
            name: 'name2'
        },
        {
            id: 3,
            name: 'name3'
        },
        {
            id: 4,
            name: 'name4'
        }
    ]
};

var newObject = {
    prop1: 'value01',
    prop2: [
        {
            id: 2
        },
        {
            id: 1,
            name: 'name01'
        },
        {
            id: 4,
            name: 'name4'
        },
        {
            id: 5,
            name: 'name5'
        }
    ],
    prop3: 'value3'
};

var changes = diff(oldObject, newObject, {
    idProp: 'id'
});

console.log(changes);

Output

[
    {
        oldPath: ['prop1'],
        newPath: ['prop1'],
        type: 'change',
        oldValue: 'value1',
        newValue: 'value01'
    },
    {
        oldPath: ['prop2'],
        newPath: ['prop2'],
        type: 'remove-item',
        oldIndex: 2,
        curIndex: 2,
        newIndex: -1,
        oldValue: {
            id: 3,
            name: 'name3'
        }
    },
    {
        oldPath: ['prop2'],
        newPath: ['prop2'],
        type: 'add-item',
        oldIndex: -1,
        curIndex: -1,
        newIndex: 3,
        newValue: {
            id: 5, 
            name: 'name5'
        }
    },
    {
        oldPath: ['prop2'],
        newPath: ['prop2'],
        type: 'move-item',
        oldIndex: 1,
        curIndex: 1,
        newIndex: 0
    },
    {
        oldPath: ['prop2', 1, 'name'],
        newPath: ['prop2', 0, 'name'],
        type: 'remove',
        oldValue: 'name2',
        newValue: undefined
    },
    {
        oldPath: ['prop2', 0, 'name'],
        newPath: ['prop2', 1, 'name'],
        type: 'change',
        oldValue: 'name1',
        newValue: 'name01'
    },
    {
        oldPath: ['prop3'],
        newPath: ['prop3'],
        type: 'add',
        oldValue: undefined,
        newValue: 'value3'
    }
]

Installation

npm i simple-diff

bower i simple-diff

Lib will look for CommonJS or AMD or will be added global function called simpleDiff().

Options

idProp: 'id' || '*'

Property name with unique value in it array. Default value is id. If your arrays do not have unique properties then you should set this option as * and lib will compare items one by one without trying to find moved items.

idProps: {'path': 'propName' || '*'}

Hash where key is path to array and value is id property. Path examples: users.list, user.list.1.friends, users.list.*.friends

comparators: [[Class, function (oldValue, newValue, options)], ...]

Array of comparators to handle comparison of objects which do not have own properties. For example instead of user.name you have user.getName() or you need to compare Date objects or you just know quick way to compare large objects like by id property and you do not need full list of changes of those objects. First value of comparator should be some class and second is function which should return true if objects are equal and false if they not. In options will be oldPath and newPath.

var now = new Date();
var prevHour = new Date();
prevHour.setHours(-1);

var changes = diff(
	{
		createdAt: now
	},
	{
		createdAt: prevHour
	},
	{
		comparators: [
			[Date, function(oldValue, newValue, options) {
			    options.oldPath; // ['createdAt']
			    options.newPath; // ['createdAt']
			    
			    return oldValue.toString() === newValue.toString();
			}]
		]
	}
);

ignore: function (oldValue, newValue, options)

Function which will be called before each object comparison and if returning value will be true then objects will be ignored. In options will be oldPath and newPath. Useful to prevent from traversing through circular references and when you don't want to compare some kind of objects at all.

callback: function (event)

Function which will be called for each event. If callback is passed then lib will not create array of all changes.

addEvent: 'add'

Name of event when new property added to object. Default value is add

changeEvent: 'change'

Name of event when property value changed. Default value is change

removeEvent: 'remove'

Name of event when property removed from object. Default value is remove

addItemEvent: 'add-item'

Name of event when new item added to array. Default value is add-item

removeItemEvent: 'remove-item'

Name of event when item removed from array. Default value is remove-item

moveItemEvent: 'move-item'

Name of event when item changed it index in array. Default value is move-item