UNPKG

5.38 kBMarkdownView Raw
1# diff-json
2[![Build Status](https://travis-ci.org/viruschidai/diff-json.png?branch=master)](https://travis-ci.org/viruschidai/diff-json)
3[![Downloads](https://img.shields.io/npm/dm/diff-json.svg)](https://www.npmjs.com/package/diff-json)
4
5A diff tool for javascript objects inspired by https://github.com/eugeneware/changeset.
6
7## Features
8
9### diff
10
11If a key is specified for an embedded array, the diff will be generated based on the objects have same keys.
12
13#### Examples:
14
15```javascript
16
17 var changesets = require('diff-json');
18 var newObj, oldObj;
19
20 oldObj = {
21 name: 'joe',
22 age: 55,
23 coins: [2, 5],
24 children: [
25 {name: 'kid1', age: 1},
26 {name: 'kid2', age: 2}
27 ]};
28
29 newObj = {
30 name: 'smith',
31 coins: [2, 5, 1],
32 children: [
33 {name: 'kid3', age: 3},
34 {name: 'kid1', age: 0},
35 {name: 'kid2', age: 2}
36 ]};
37
38
39 # Assume children is an array of child object and the child object has 'name' as its primary key
40 diffs = changesets.diff(oldObj, newObj, {children: 'name'});
41
42 expect(diffs).to.eql([
43 {
44 type: 'update', key: 'name', value: 'smith', oldValue: 'joe'
45 },
46 {
47 type: 'update', key: 'coins', embededKey: '$index', changes: [
48 {type: 'add', key: '2', value: 1 }
49 ]
50 },
51 {
52 type: 'update',
53 key: 'children',
54 embededKey: 'name',
55 changes: [
56 {
57 type: 'update', key: 'kid1', changes: [
58 {type: 'update', key: 'age', value: 0, oldValue: 1 }
59 ]
60 },
61 {
62 type: 'add', key: 'kid3', value: {name: 'kid3', age: 3 }
63 }
64 ]
65 },
66 {
67 type: 'remove', key: 'age', value: 55
68 }
69 ]);
70```
71
72### applyChange
73#### Examples:
74
75```javascript
76
77 var changesets = require('diff-json');
78 var oldObj = {
79 name: 'joe',
80 age: 55,
81 coins: [2, 5],
82 children: [
83 {name: 'kid1', age: 1},
84 {name: 'kid2', age: 2}
85 ]};
86
87
88 # Assume children is an array of child object and the child object has 'name' as its primary key
89 diffs = [
90 {
91 type: 'update', key: 'name', value: 'smith', oldValue: 'joe'
92 },
93 {
94 type: 'update', key: 'coins', embededKey: '$index', changes: [
95 {type: 'add', key: '2', value: 1 }
96 ]
97 },
98 {
99 type: 'update',
100 key: 'children',
101 embededKey: 'name', // The key property name of the elements in an array
102 changes: [
103 {
104 type: 'update', key: 'kid1', changes: [
105 {type: 'update', key: 'age', value: 0, oldValue: 1 }
106 ]
107 },
108 {
109 type: 'add', key: 'kid3', value: {name: 'kid3', age: 3 }
110 }
111 ]
112 },
113 {
114 type: 'remove', key: 'age', value: 55
115 }
116 ]
117
118 changesets.applyChanges(oldObj, diffs)
119 expect(oldObj).to.eql({
120 name: 'smith',
121 coins: [2, 5, 1],
122 children: [
123 {name: 'kid3', age: 3},
124 {name: 'kid1', age: 0},
125 {name: 'kid2', age: 2}
126 ]});
127
128```
129
130### revertChange
131#### Examples:
132
133```javascript
134
135 var changesets = require('diff-json');
136
137 var newObj = {
138 name: 'smith',
139 coins: [2, 5, 1],
140 children: [
141 {name: 'kid3', age: 3},
142 {name: 'kid1', age: 0},
143 {name: 'kid2', age: 2}
144 ]};
145
146 # Assume children is an array of child object and the child object has 'name' as its primary key
147 diffs = [
148 {
149 type: 'update', key: 'name', value: 'smith', oldValue: 'joe'
150 },
151 {
152 type: 'update', key: 'coins', embededKey: '$index', changes: [
153 {type: 'add', key: '2', value: 1 }
154 ]
155 },
156 {
157 type: 'update',
158 key: 'children',
159 embededKey: 'name', // The key property name of the elements in an array
160 changes: [
161 {
162 type: 'update', key: 'kid1', changes: [
163 {type: 'update', key: 'age', value: 0, oldValue: 1 }
164 ]
165 },
166 {
167 type: 'add', key: 'kid3', value: {name: 'kid3', age: 3 }
168 }
169 ]
170 },
171 {
172 type: 'remove', key: 'age', value: 55
173 }
174 ]
175
176 changesets.revertChanges(newObj, diffs)
177 expect(newObj).to.eql {
178 name: 'joe',
179 age: 55,
180 coins: [2, 5],
181 children: [
182 {name: 'kid1', age: 1},
183 {name: 'kid2', age: 2}
184 ]};
185
186```
187
188## Get started
189
190```
191npm install diff-json
192```
193
194## Run the test
195```
196npm run test
197```
198
199## Licence
200
201The MIT License (MIT)
202
203Copyright (c) 2013 viruschidai@gmail.com
204
205Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
206
207The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
208
209THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.