Coder Social home page Coder Social logo

diff-json's People

Contributors

0x80 avatar jdus avatar renovate-bot avatar runk avatar vinteo avatar viruschidai 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

Watchers

 avatar  avatar  avatar

diff-json's Issues

Make lodash dependency more granular

Hi,

The dependency on lodash increases the bundle size of consuming applications. Could we switch to the packages for the individual lodash functions to lodash.intersection, lodash.difference etc.?

The update process is fairly trivial, would be happy to do a PR for it.

Thanks.

Tests are failing

I'm getting the following output if I run npm run test

      ✓ should return correct diff for object with embedded array object that does not have key specified
      ✓ should return correct diff for object with embedded array that has key specified
    applyChanges()
      1) should transfer oldObj to newObj with changeset
      ✓ should transfer oldObj to newObj with changesetWithoutEmbeddedKey
    revertChanges()
      ✓ should transfer newObj to oldObj with changeset
      ✓ should transfer newObj to oldObj with changesetWithoutEmbeddedKey


  5 passing (20ms)
  1 failing

  1) changesets
       applyChanges()
         should transfer oldObj to newObj with changeset:

      Error: expected { name: 'smith',
  nested: { inner: 2 },
  date: Mon, 13 Oct 2014 09:13:00 GMT,
  coins: [ 2, 5, 1 ],
  toys: [],
  pets: [],
  children:
   [ { name: 'kid1', age: 0, subset: [Object] },
     { name: 'kid2', age: 2 },
     { name: 'kid3', age: 3 } ],
  mixed: '10' } to sort of equal { name: 'smith',
  mixed: '10',
  nested: { inner: 2 },
  date: Mon, 13 Oct 2014 09:13:00 GMT,
  coins: [ 2, 5, 1 ],
  toys: [],
  pets: [],
  children:
   [ { name: 'kid3', age: 3 },
     { name: 'kid1', age: 0, subset: [Object] },
     { name: 'kid2', age: 2 } ] }
      + expected - actual

       {
         "children": [
           {
      +      "age": 3
      +      "name": "kid3"
      +    }
      +    {
             "age": 0
             "name": "kid1"
             "subset": [
               {
--
           {
             "age": 2
             "name": "kid2"
           }
      -    {
      -      "age": 3
      -      "name": "kid3"
      -    }
         ]
         "coins": [
           2
           5

      at Assertion.assert (node_modules/expect.js/index.js:96:13)
      at Assertion.eql (node_modules/expect.js/index.js:230:10)
      at Context.<anonymous> (test/changesets.coffee:143:25)
      at processImmediate (internal/timers.js:464:21)


How to get an array of objects with key to show diff?

I have the following code:

var diffJson = require("diff-json")

oldArray = [
  {
    "id": "a",
    "count": 1
  },
  {
    "id": "b",
    "count": 0
  }
];

newArray = [
  {
    "id": "b",
    "count": 1
  },
  {
    "id": "a",
    "count": 3
  }
];

var diffs = diffJson.diff(oldArray, newArray);

And the result is showing as:

[Object {changes: [, ], embededKey: "$index", key: "$root", type: "update"}]

What should the call to .diff be?
What do the other params (path, embededObjKeys, keyPath) mean?

Logic around array removeKey seems wrong

It seems that the logic around removing items from an array is seriously dodgy.

For example, in the test you go from toys ["car", "doll", "car"] to [] by doing 3 remove actions on the array. But the actions are not looking at the items in the array, they will try to remove an item from the index where the original item was positioned, and if that index doesn't have an element, it will just remove the last element from the array, whatever it is.

Here's what happens if you log the applyLeafChange arguments. The first is the array contents it operates on, and the second is the change to be applied.

  1. applyLeafChange [ 'car', 'doll', 'car' ] { type: 'remove', key: '0', value: 'car' }
  2. applyLeafChange [ 'doll', 'car' ] { type: 'remove', key: '1', value: 'doll' }
  3. applyLeafChange [ 'doll' ] { type: 'remove', key: '2', value: 'car' }

As you can see the first mutation is valid, because there is a "car" at index 0, but 2 and 3 are not. Step two should remove the doll, but instead removes the car at the end. Then step 3 should remove the car at index 2 but there is no car and not index 2 anymore, and instead it just removes the last item.

The end result is still [], and that's why the test passes, but this all seems wrong to me.

I think what you intended to do with the indexOfItemInArray function is to look for the actual value in the array. But for that to work you need to pass the value to removeKey which is currently not happening.

This is what I ended up with:

function removeKey(obj: JsonObject | JsonArray, key: string, value: JsonValue) {
  if (Array.isArray(obj)) {
    if (!obj[Number(key)]) {
      const index = obj.findIndex((x) => x === value);
      return obj.splice(index, 1);
    } else {
      return obj.splice(Number(key), 1);
    }
  } else {
    return delete obj[key];
  }
}

In this implementation, if the item can not be found, it will still remove the last item from the array whatever it is, so it's probably better to catch the -1 returned from findIndex and throw an error.

This code is the result of converting your library to Typescript and in the process, I have also stripped the logic around embeddedKeys because I didn't need that complexity and it was difficult enough trying to port this.

In the future, I would probably move to a JSON Patch compatible implementation, but my production code was already dependent on this library so I decided to convert it to something I can understand. If enough people are interested I might release it in the future.

Fails for case of newly added property with a value of null

A simple test case:

    it 'should properly handle new null property', ->
      before = {}
      after = { newProp: null }
      changesets.applyChanges before, changesets.diff before, after
      expect(after).to.eql before

Fails with "TypeError: Cannot read property 'length' of undefined"

if change.value? or change.type is changeset.op.REMOVE

should probably be:

      if !change.changes

In its current form applyChanges calls applyBranchChanges for the add of a new property with a value of null. applyBranchChanges expects a changes array to exist on the change and calls length on undefined.

Doesn't produce diff if array elements order is changed

I would expect that a different order of items in an array would also produce a diff. It doesn't.

Here's an example:

const changesets = require('diff-json')

const newObj = {
  children: [
    // kid2 is first here
    {name: 'kid2', age: 2},
    {name: 'kid1', age: 1}
  ]}

const oldObj = {
  // kid1 is first here
  children: [
    {name: 'kid1', age: 1},
    {name: 'kid2', age: 2}
  ]}

const diffs = changesets.diff(oldObj, newObj, {children: 'name'})
console.log(JSON.stringify(diffs, null, 2))

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.