1.1.9 • Published 3 years ago

deep-object-diff v1.1.9

Weekly downloads
2,199,021
License
MIT
Repository
github
Last release
3 years ago

❄️

Deep diff two JavaScript Objects

Build Status Code coverage version downloads MIT License PRs Welcome

A small library that can deep diff two JavaScript Objects, including nested structures of arrays and objects.

Installation

yarn add deep-object-diff

npm i --save deep-object-diff

Functions available:

Importing

import { diff, addedDiff, deletedDiff, updatedDiff, detailedDiff } from 'deep-object-diff';

Usage:

diff:

const lhs = {
  foo: {
    bar: {
      a: ['a', 'b'],
      b: 2,
      c: ['x', 'y'],
      e: 100 // deleted
    }
  },
  buzz: 'world'
};

const rhs = {
  foo: {
    bar: {
      a: ['a'], // index 1 ('b')  deleted
      b: 2, // unchanged
      c: ['x', 'y', 'z'], // 'z' added
      d: 'Hello, world!' // added
    }
  },
  buzz: 'fizz' // updated
};

console.log(diff(lhs, rhs)); // =>
/*
{
  foo: {
    bar: {
      a: {
        '1': undefined
      },
      c: {
        '2': 'z'
      },
      d: 'Hello, world!',
      e: undefined
    }
  },
  buzz: 'fizz'
}
*/

addedDiff:

const lhs = {
  foo: {
    bar: {
      a: ['a', 'b'],
      b: 2,
      c: ['x', 'y'],
      e: 100 // deleted
    }
  },
  buzz: 'world'
};

const rhs = {
  foo: {
    bar: {
      a: ['a'], // index 1 ('b')  deleted
      b: 2, // unchanged
      c: ['x', 'y', 'z'], // 'z' added
      d: 'Hello, world!' // added
    }
  },
  buzz: 'fizz' // updated
};

console.log(addedDiff(lhs, rhs));

/*
{
  foo: {
    bar: {
      c: {
        '2': 'z'
      },
      d: 'Hello, world!'
    }
  }
}
*/

deletedDiff:

const lhs = {
  foo: {
    bar: {
      a: ['a', 'b'],
      b: 2,
      c: ['x', 'y'],
      e: 100 // deleted
    }
  },
  buzz: 'world'
};

const rhs = {
  foo: {
    bar: {
      a: ['a'], // index 1 ('b')  deleted
      b: 2, // unchanged
      c: ['x', 'y', 'z'], // 'z' added
      d: 'Hello, world!' // added
    }
  },
  buzz: 'fizz' // updated
};

console.log(deletedDiff(lhs, rhs));

/*
{
  foo: {
    bar: {
      a: {
        '1': undefined
      },
      e: undefined
    }
  }
}
*/

updatedDiff:

const lhs = {
  foo: {
    bar: {
      a: ['a', 'b'],
      b: 2,
      c: ['x', 'y'],
      e: 100 // deleted
    }
  },
  buzz: 'world'
};

const rhs = {
  foo: {
    bar: {
      a: ['a'], // index 1 ('b')  deleted
      b: 2, // unchanged
      c: ['x', 'y', 'z'], // 'z' added
      d: 'Hello, world!' // added
    }
  },
  buzz: 'fizz' // updated
};

console.log(updatedDiff(lhs, rhs));

/*
{
  buzz: 'fizz'
}
*/

detailedDiff:

const lhs = {
  foo: {
    bar: {
      a: ['a', 'b'],
      b: 2,
      c: ['x', 'y'],
      e: 100 // deleted
    }
  },
  buzz: 'world'
};

const rhs = {
  foo: {
    bar: {
      a: ['a'], // index 1 ('b')  deleted
      b: 2, // unchanged
      c: ['x', 'y', 'z'], // 'z' added
      d: 'Hello, world!' // added
    }
  },
  buzz: 'fizz' // updated
};

console.log(detailedDiff(lhs, rhs));

/*
{
  added: {
    foo: {
      bar: {
        c: {
          '2': 'z'
        },
        d: 'Hello, world!'
      }
    }
  },
  deleted: {
    foo: {
      bar: {
        a: {
          '1': undefined
        },
        e: undefined
      }
    }
  },
  updated: {
    buzz: 'fizz'
  }
}
*/

License

MIT

phaser-server@carlosk/dotenv-encryptquick-parent-uploader@abelmkr/appsync-change-record-transformerinsurer-policyapi@deity/falcon-theme-editornebula-test-fragment@zip-software/vue-virtual-streamkric-object-sharedcra-template-promess_v1@infini-soft/store@infinitebrahmanuniverse/nolb-dee@mydoma/myd-providerdemotestpublish@knapsack/app-uiesante-api@everything-registry/sub-chunk-1460vuesfcbuildertangro-react-scriptssync-dependencies-utilsynaptiksymmetric-parsersveltecmstechovarya-cbl-apptechovarya-cbl-mobile-apptestophobiasvelte-component-libthreexr-enginevsfcvnmvite-utils-helperveendor@dominikla/smart-vue-components@crawlo/entity@crawlo/graphql@concepto/interface@cute-apocalypse/react-tree@deeptrekker/react@deeptrekker/react-sdk@cadolabs/crowdin-cli@boomber/holonews@blkmarketco/components-library@payrail/ancore@powership/utils@prioritymediabeta/core@rundeck/ui-trellisbaril@ygorconfig/cssiureiurejabcorejourney-uijson-autotranslatejson-autotranslate-forkjosmjson-diff-corejson-file-autotranslate-to-manyio-ts-api-clientsilver-docker@appmint/dataformzzzxxxyyy321123@react-wasm/eguistalefishstark-orchestratorstark-clientspyglass-clityrextypeorm-simple-historyu-libraryterraform-rancher-comparetldrawlignintnext-hanta-core-componentstnext-hanta-core-components-libtranslatorchaintooljet_elab_sampleunleash-serverunleash-server-openapi@aolney/experimental-control@arwidt/fluxtools@atlaskit/navigation-next@asigloo/vue-dynamic-forms@atomly/typeorm-entity-semver-manager@aures/custom-react-table@autoteetimes/teetimedataservice@adobe/token-diff-generator@ahanapediatrics/ahana-api-client@agentin/stan@aicacia/locales-bundler@aliatech/loopback-diff-mixin@backpackjs/cms@backland/utils@beatchainmusic/ui@alwaystudios/as-utils@bodhi-project/components@brixel/brixel-components@builder.io/locale-plugin@cathaybc/bdk@chumager/itdfw-site-base@100mslive/hms-excalidraw@abstractball/journey-ui@cojad/bottender
1.1.9

3 years ago

1.1.8

3 years ago

1.1.1

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.0

7 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago

0.0.4

8 years ago

0.0.3

9 years ago

0.0.2

9 years ago

0.0.1

9 years ago