Skip to content

Commit

Permalink
Update readme with instructions for new functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mattphillips committed Jan 26, 2017
1 parent fca43fc commit 83bea8b
Showing 1 changed file with 226 additions and 8 deletions.
234 changes: 226 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,57 @@
[![Build Status](https://travis-ci.org/mattphillips/deep-object-diff.svg?branch=master)](https://travis-ci.org/mattphillips/deep-object-diff)
[![Coverage Status](https://coveralls.io/repos/github/mattphillips/deep-object-diff/badge.svg?branch=master)](https://coveralls.io/github/mattphillips/deep-object-diff?branch=master)

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

### Useage:
## Installation
```
npm i --save deep-object-diff
```

## Functions available:
- ### [`diff(originalObj, updatedObj)`](#diff)
returns the difference of the original and updated objects

- ### [`addedDiff(original, updatedObj)`](#addeddiff)
returns only the values added to the updated object

- ### [`deletedDiff(original, updatedObj)`](#deleteddiff)
returns only the values deleted in the updated object

- ### [`updatedDiff(original, updatedObj)`](#updateddiff)
returns only the values that have been changed in the updated object

- ### [`detailedDiff(original, updatedObj)`](#detaileddiff)
returns an object with the added, deleted and updated differences

## Importing

ES6 / Babel:
``` js
import { diff, addedDiff, deletedDiff, updatedDiff, detailedDiff } from 'deep-object-diff';
```

ES5:
``` js
const { diff, addedDiff, deletedDiff, detailedDiff, updatedDiff } = require("deep-object-diff");

// OR

const diff = require("deep-object-diff").diff;
const addedDiff = require("deep-object-diff").addedDiff;
const deletedDiff = require("deep-object-diff").deletedDiff;
const detailedDiff = require("deep-object-diff").detailedDiff;
const updatedDiff = require("deep-object-diff").updatedDiff;
```

## Usage:

### `diff`:
```js
const lhs = {
foo: {
bar: {
a: [1, 2],
a: ['a', 'b'],
b: 2,
c: ['x', 'y'],
e: 100 // deleted
Expand All @@ -22,18 +65,17 @@ const lhs = {
const rhs = {
foo: {
bar: {
a: [1], // updated (value deleted)
a: ['a'], // index 1 ('b') deleted
b: 2, // unchanged
c: ['x', 'y', 'z'], // updated (value added)
c: ['x', 'y', 'z'], // 'z' added
d: 'Hello, world!' // added
}
},
buzz: 'fizz' // updated
};

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

/* logs:
console.log(diff(lhs, rhs)); // =>
/*
{
foo: {
bar: {
Expand All @@ -51,3 +93,179 @@ console.log(diff(lhs, rhs));
}
*/
```

### `addedDiff`:
```js
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`:
```js
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`:
```js
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`:
```js
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'
}
}
*/
```

0 comments on commit 83bea8b

Please sign in to comment.