UNPKG

4.43 kBMarkdownView Raw
1# changesets
2
3A changesets tool for javascript objects inspired by https://github.com/eugeneware/changeset.
4
5## Features
6
7### Generate diff between different versions of an object.
8
9If a key is specified for an embedded array, the diff will be generated based on the objects have same keys.
10
11#### Examples:
12
13```javascript
14
15 var changesets = require('./index');
16 var newObj, oldObj;
17
18 oldObj = {
19 name: 'joe',
20 age: 55,
21 coins: [2, 5],
22 children: [
23 {name: 'kid1', age: 1 },
24 {name: 'kid2', age: 2 }
25 ]};
26
27 newObj = {
28 name: 'smith',
29 coins: [2, 5, 1],
30 children: [
31 {name: 'kid3', age: 3 },
32 {name: 'kid1', age: 0 },
33 {name: 'kid2', age: 2 }
34 ]};
35
36
37 # Assume children is an array of child object and the child object has 'name' as its primary key
38 diffs = changesets.diff(oldObj, newObj, {children: 'name'});
39
40 expect(diffs).to.eql([
41 {type: '+-', key: [ 'name' ], value: 'smith', oldValue: 'joe'}, # modified
42 {type: '+', key: [ 'coins', '2' ], value: 1 }, # added
43 {type: '+-', key: [ 'children', '$name=kid1', 'age' ], value: 0, oldValue: 1 },
44 {type: '+', key: [ 'children', '$name=kid3' ], value: { name: 'kid3', age: 3 } },
45 {type: '-', key: [ 'age' ], value: 55 } # deleted
46 ]);
47```
48
49### Apply changeset to an object to make a new version of the object
50#### Examples:
51
52```javascript
53
54 var changesets = require('./index');
55 var oldObj = {
56 name: 'joe',
57 age: 55,
58 coins: [2, 5],
59 children: [
60 {name: 'kid1', age: 1 },
61 {name: 'kid2', age: 2 }
62 ]};
63
64
65 # Assume children is an array of child object and the child object has 'name' as its primary key
66 diffs = [
67 {type: '+-', key: [ 'name' ], value: 'smith', oldValue: 'joe'}, # modified
68 {type: '+', key: [ 'coins', '2' ], value: 1 }, # added
69 {type: '+-', key: [ 'children', '$name=kid1', 'age' ], value: 0, oldValue: 1 },
70 {type: '+', key: [ 'children', '$name=kid3' ], value: { name: 'kid3', age: 3 } },
71 {type: '-', key: [ 'age' ], value: 55 } # deleted
72 ]
73
74 expect(changesets.applyChange(oldObj, diffs)).to.eql {
75 name: 'smith',
76 coins: [2, 5, 1],
77 children: [
78 {name: 'kid3', age: 3 },
79 {name: 'kid1', age: 0 },
80 {name: 'kid2', age: 2 }
81 ]};
82
83```
84
85
86### Revert an object to previous state with a changeset
87#### Examples:
88
89```javascript
90
91 var changesets = require('./index');
92
93 var newObj = {
94 name: 'smith',
95 coins: [2, 5, 1],
96 children: [
97 {name: 'kid3', age: 3 },
98 {name: 'kid1', age: 0 },
99 {name: 'kid2', age: 2 }
100 ]};
101
102 # Assume children is an array of child object and the child object has 'name' as its primary key
103 diffs = [
104 {type: '+-', key: [ 'name' ], value: 'smith', oldValue: 'joe'}, # modified
105 {type: '+', key: [ 'coins', '2' ], value: 1 }, # added
106 {type: '+-', key: [ 'children', '$name=kid1', 'age' ], value: 0, oldValue: 1 },
107 {type: '+', key: [ 'children', '$name=kid3' ], value: { name: 'kid3', age: 3 } },
108 {type: '-', key: [ 'age' ], value: 55 } # deleted
109 ]
110
111 expect(changesets.revertChanges(newObj, diffs)).to.eql {
112 name: 'joe',
113 age: 55,
114 coins: [2, 5],
115 children: [
116 {name: 'kid1', age: 1 },
117 {name: 'kid2', age: 2 }
118 ]};
119
120```
121
122## Get started
123
124```
125npm install changesets
126```
127
128## Run the test
129```
130npm run test
131```
132
133## Licence
134
135The MIT License (MIT)
136
137Copyright (c) 2013 viruschidai@gmail.com
138
139Permission 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:
140
141The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
142
143THE 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.