Code Monkey home page Code Monkey logo

changeset's Introduction

changeset

Generate diff changesets for javascript objects, decomposing diffs into a series of puts and delete operations. The format is similar to the levelup batch operation list for bulk operations.

Handles circular references of Objects and Arrays.

build status

Example

Take a diff of two objects and produce a list of transformation operations:

var diff = require('changeset');
var a = {
  name: 'Eugene',
  number: 42,
  tags: ['tag1', 'tag2', 'tag3'],
  scores: {
    tetris: 1000,
    carmageddon: 3
  }
};

var b = {
  name: 'Susan',
  number: 43,
  tags: ['tag1', 'tag4'],
  scores: {
    carmageddon: 3,
    zelda: 3000
  },
  age: 37
};

var changes = diff(a, b);
expect(changes).to.deep.equal([
  { type: 'put', key: ['name'], value: 'Susan' },
  { type: 'put', key: ['number'], value: 43 },
  { type: 'put', key: ['tags', '1'], value: 'tag4' },
  { type: 'del', key: ['tags', '2'] },
  { type: 'del', key: ['scores', 'tetris'] },
  { type: 'put', key: ['scores', 'zelda'], value: 3000 },
  { type: 'put', key: ['age'], value: 37 }
]);

Apply an operational changeset and apply it to an object to get a transformed object:

var diff = require('changeset');

var changes = [
  { type: 'put', key: ['name'], value: 'Susan' },
  { type: 'put', key: ['number'], value: 43 },
  { type: 'put', key: ['tags', '1'], value: 'tag4' },
  { type: 'del', key: ['tags', '2'] },
  { type: 'del', key: ['scores', 'tetris'] },
  { type: 'put', key: ['scores', 'zelda'], value: 3000 },
  { type: 'put', key: ['age'], value: 37 }
];

var a = {
  name: 'Eugene',
  number: 42,
  tags: ['tag1', 'tag2', 'tag3'],
  scores: {
    tetris: 1000,
    carmageddon: 3
  }
};

// apply the changes to a
var b_ = diff.apply(changes, a);

var b = {
  name: 'Susan',
  number: 43,
  tags: ['tag1', 'tag4'],
  scores: {
    carmageddon: 3,
    zelda: 3000
  },
  age: 37
};

// the transformed object should now equal b
expect(b_).to.deep.equals(b);

By default apply will return a new modified object after applying the changeset. If you want to modify the destination, pass true as the third parameter:

// apply the changes to a and modify a
var b_ = diff.apply(changes, a, true);
// a is now modified, and b_ is the same as a
expect(b_).to.equal(a);

changeset's People

Contributors

eugeneware avatar mightyiam avatar zisiszikos avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

changeset's Issues

LICENSE file?

Hi, would you mind adding a LICENSE file to the repo? I see BSD listed in the package.json but I'm in the process of open sourcing some stuff out of my company that uses your module and my legal department will have issue with the license text not actually being included.

Thanks!

Error when changing object field to string

Currently, there is an issue when diffing and patching an object where the types of a field changes from an object to a string.

That is, I would expect the following to work:

    var a = {hello: {nested: 2}};
    var b = {hello: 3};
    var changeset = diff(a, b);
    var b_ = diff.apply(changeset, a);
    expect(b).to.deep.equals(b_);

However, b_ is set to {hello: {}} following the above operations.

sort "del" changes to array in reverse order

It would be great if deletion operations for arrays were presented in reverse order to ensure that they are deleted from the end rather than from the start. For regular JSON, the current approach is fine but the current approach is problematic when using this to edit a more complex object which does not provide the same low-level access since it might automatically shift the indices down for us.

A concrete example where this is problematic can be found here.

Diff/patching `minidom`s

Do you think that this could be used, potentially, for diffing/patching minidoms?

Trying, first thing I ran into was infinite recursion in compare, due to a _documentOwner key pointing to the parent (or grandparent, is it?).

If this is fixed, how much more issues do you assume will come up?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.