@@ -13,6 +13,74 @@ npm install unist-diff
13
13
## Usage
14
14
15
15
``` js
16
+ var h = require (' hastscript' );
17
+ var diff = require (' unist-diff' );
18
+
19
+ var left = h (' div' , [
20
+ h (' p' , [
21
+ ' Some ' ,
22
+ h (' b' , ' importance' ),
23
+ ' and ' ,
24
+ h (' i' , ' emphasis' ),
25
+ ' .'
26
+ ]),
27
+ h (' pre' , h (' code' , ' foo()' ))
28
+ ]);
29
+
30
+ var right = h (' div' , [
31
+ h (' p' , [
32
+ ' Some ' ,
33
+ h (' strong' , ' importance' ),
34
+ ' and ' ,
35
+ h (' em' , ' emphasis' ),
36
+ ' .'
37
+ ]),
38
+ h (' pre' , h (' code' , ' bar()' ))
39
+ ]);
40
+
41
+ console .dir (diff (left, right), {depth: null });
42
+ ```
43
+
44
+ Yields:
45
+
46
+ ``` js
47
+ { ' 1' :
48
+ [ { type: ' insert' ,
49
+ left: null ,
50
+ right:
51
+ { type: ' element' ,
52
+ tagName: ' strong' ,
53
+ properties: {},
54
+ children: [ { type: ' text' , value: ' importance' } ] } },
55
+ { type: ' insert' ,
56
+ left: null ,
57
+ right:
58
+ { type: ' element' ,
59
+ tagName: ' em' ,
60
+ properties: {},
61
+ children: [ { type: ' text' , value: ' emphasis' } ] } } ],
62
+ ' 3' :
63
+ { type: ' remove' ,
64
+ left:
65
+ { type: ' element' ,
66
+ tagName: ' b' ,
67
+ properties: {},
68
+ children: [ { type: ' text' , value: ' importance' } ] },
69
+ right: null },
70
+ ' 6' :
71
+ { type: ' remove' ,
72
+ left:
73
+ { type: ' element' ,
74
+ tagName: ' i' ,
75
+ properties: {},
76
+ children: [ { type: ' text' , value: ' emphasis' } ] },
77
+ right: null },
78
+ ' 11' :
79
+ { type: ' text' ,
80
+ left: { type: ' text' , value: ' foo()' },
81
+ right: { type: ' text' , value: ' bar()' } },
82
+ left: left
83
+ }
16
84
```
17
85
18
86
## API
@@ -28,9 +96,73 @@ npm install unist-diff
28
96
29
97
` Array.<Patch> ` — List of one or [ ` patch ` es] [ patch ] .
30
98
31
- ### Patch
99
+ ### ` Patch `
100
+
101
+ Patches represent changes. They come with three properties:
102
+
103
+ * ` type ` (` string ` ) — Type of change
104
+ * ` left ` ([ ` Node ` ] [ node ] , optional) — Left node
105
+ * ` right ` ([ ` Node ` ] [ node ] , [ ` PropsDiff ` ] [ propsdiff ] , [ ` MoveDiff ` ] [ movediff ] ,
106
+ optional) — New thing
107
+
108
+ #### ` remove `
109
+
110
+ * ` type ` : ` 'remove' `
111
+ * ` left ` ([ ` Node ` ] [ node ] ) — Left node
112
+ * ` right ` : ` null `
113
+
114
+ #### ` insert `
115
+
116
+ * ` type ` : ` 'insert' `
117
+ * ` left ` : ` null `
118
+ * ` right ` ([ ` Node ` ] [ node ] ) — Right node
119
+
120
+ #### ` replace `
121
+
122
+ * ` type ` : ` 'node' `
123
+ * ` left ` ([ ` Node ` ] [ node ] ) — Left node
124
+ * ` right ` ([ ` Node ` ] [ node ] ) — Right node
125
+
126
+ #### ` props `
32
127
33
- A patch.
128
+ * ` type ` : ` 'props' `
129
+ * ` left ` ([ ` Node ` ] [ node ] ) — Left node
130
+ * ` right ` : [ ` PropsDiff ` ] [ propsdiff ]
131
+
132
+ #### ` text `
133
+
134
+ * ` type ` : ` 'text' `
135
+ * ` left ` ([ ` Node ` ] [ node ] ) — Left node
136
+ * ` right ` ([ ` Node ` ] [ node ] ) — Right node
137
+
138
+ #### ` order `
139
+
140
+ * ` type ` : ` 'order' `
141
+ * ` left ` ([ ` Node ` ] [ node ] ) — Parent node
142
+ * ` right ` ([ ` MoveDiff ` ] [ movediff ] ) — Reorder
143
+
144
+ ## ` PropsDiff `
145
+
146
+ ` PropsDiff ` is an object mapping keys to new values.
147
+
148
+ In the diff:
149
+
150
+ * If a key is removed, the key’s value is set to ` undefined ` .
151
+ * If the new value is of the same non-primitive type as the new value
152
+ (their prototype’s are equal, thus both array or both object), the key’s
153
+ value is set to a ` PropsDiff ` of both values.
154
+ * In all other cases, the key’s value is set to the new value.
155
+
156
+ ## ` MoveDiff `
157
+
158
+ ` MoveDiff ` is an object with two arrays: ` removes ` and ` inserts ` .
159
+ They are never both empty. Objects in ` inserts ` and ` removes ` have the
160
+ following properties:
161
+
162
+ * ` from ` (` number ` ), if in ` removes ` — Current index of the child in ` parent `
163
+ * ` left ` ([ ` Node ` ] [ node ] ) — Left node
164
+ * ` right ` ([ ` Node ` ] [ node ] ) — Right node
165
+ * ` to ` (` number ` ), if in ` inserts ` — Next index of the child in ` parent `
34
166
35
167
## License
36
168
@@ -57,3 +189,7 @@ A patch.
57
189
[ node ] : https://github.com/syntax-tree/unist#node
58
190
59
191
[ patch ] : #patch
192
+
193
+ [ propsdiff ] : #propsdiff
194
+
195
+ [ movediff ] : #movediff
0 commit comments