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

arr-mutations

v0.1.2

Published

Calculate all mutation operations between two arrays. Supports a generic equals predicate function.

Downloads

8

Readme

Array mutations

Build Status

Calculate all mutation operations between two arrays. Supports a generic equals predicate function.

Install

npm install arr-mutations

Usage

var mutations = require('arr-mutations');

mutations([1, 2], [2, 3]);
//=> [{type: 'add', item: 3}, {type: 'remove', item: 1}]

mutations([2, 2, 2], [2, 2]);
//=> [{type: 'remove', item: 2}]

mutations([2, 2, 3, 2], [2, 3, 3, 2]);
//=> [{type: 'add', item: 3}, {type: 'remove', item: 2}]

// Items are matched in order from the new array, consider this example:
var first = [{id: 1, name: 'a'}, {id: 1, name: 'b'}, {id: 1, name: 'c'}];
var second = [{id: 1, name: 'aa'}, {id: 1, text: 'bb'}];
mutations(first, second, {
	equals: function(a, b) { return a.id === b.id; }
});
//=> [
//     {type: 'change', old: {id: 1, name: 'a'}, item: {id: 1, name: 'aa'}},
//     {type: 'change', old: {id: 1, name: 'b'}, item: {id: 1, text: 'bb'}},
//     {type: 'remove', item: {id: 1, name: 'c'}}
//   ]

var first = [{id: 1, text: 'one'}, {id: 2, text: 'two'}];
var second = [{id: 2, text: 'another'}, {id: 3, text: 'three'}];
mutations(first, second, {
	equals: function(a, b) { return a.id === b.id; }
});
//=> [
//     {type: 'add', item: {id: 3, text: 'three'}},
//     {type: 'change', old: {id: 2, text: 'two'}, item: {id: 2, text: 'another'}},
//     {type: 'remove', item: {id: 1, text: 'one'}}
//   ]

API

mutations(old, new, [options])

Returns an array of mutation operations which are needed to convert array old to new. When detecting if an item has changed, the items from new array are scanned from left to right and the first item passing options.equals check, will be considered as the new version of the original item. If that new item also matches options.deepEquals, it will be listed as a changed item.

Returned mutation operations are sorted by type.

old

Required
Type: array

Original array of items.

new

Required
Type: array

New array of items.

options

Type: object

options.equals

Type: function Default: function equals(a, b) { return a === b; }

Equality function. Should return true if a and b items are considered as the same entity. Entity means e.g. single user with id: 1.

If you have e.g. an array of users, this function answers to: is a the same user as b?

options.deepEquals

Type: function
Default: function deepEquals(a1, a2) { return _.isEqual(a1, a2); }

Deep equality function. Should return true if a1 is not equal to a2. Used to detect if an entity has changed.

If you have e.g. an array of users, this function answers to: is a2 a changed version a1?

License

MIT