This repository was archived by the owner on Mar 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdiff.js
More file actions
78 lines (69 loc) · 2.24 KB
/
diff.js
File metadata and controls
78 lines (69 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
'use strict'
import diff from '../lib/diff'
import assert from 'assert'
const data = [
// primitive primitive
[true, true],
// structure structure
[[], []],
[{}, {}],
// replace primitive
[true, false, 'replace', '', false],
// replace primitive with structure
[true, [], "replace", '', []],
// replace structure with primitive
[[], true, "replace", '', true],
// replace structure with structure
[[], {}, 'replace', '', {}],
[{}, [], 'replace', '', []],
// remove key
[{"foo": "bar"}, {}, 'remove', '/foo'],
// replace key
[{"foo": "bar"}, {"foo": "foo"}, 'replace', '/foo', 'foo'],
// add key
[{}, {"foo": "foo"}, 'add', '/foo', 'foo'],
// nested replace key
[{"foo": {"foo": "bar"}}, {"foo": {"foo": "foo"}}, 'replace', '/foo/foo', 'foo'],
// nested add key
[{"foo": {}}, {"foo": {"foo": "foo"}}, 'add', '/foo/foo', 'foo'],
// nested remove key
[{"foo": {"foo": "foo"}}, {"foo": {}}, 'remove', '/foo/foo'],
// nested nested
[{}, {"foo": {"foo": "foo"}}, 'add', '/foo', {"foo": "foo"}],
// same array
[["foo"], ["foo"]],
// add item end
[["foo"], ["foo", "bar"], 'add', '/-', 'bar'], // -----
[["foo", "bara"], ["foo", "bara", "bar"], 'add', '/-', 'bar'],
// replace item
[["foo"], ["bar"], 'replace', '/0', 'bar'],
// replace first item
[["foo", "bar"], ["bar", "bar"], 'replace', '/0', 'bar'],
// replace last item
[["foo", "bar"], ["foo", "foo"], 'replace', '/1', 'foo'],
// add item start
[["bar"], ["foo", "bar"], 'add', '/0', 'foo'],
// add item start + 1
[["foo", "bar"], ["hi", "foo", "bar"], 'add', '/0', 'hi'],
// add item in between
[["foo", "bar"], ["foo", "lol", "bar"], "add", '/1', 'lol'],
// remove item in between
[["foo", "foobar", "bar"], ["foo", "bar"], "remove", '/1'],
// replace item in between
[["foo", "", "bar"], ["foo", "foobar", "bar"], "replace", '/1', 'foobar'],
]
describe('diff', () => {
data.forEach(function(d, idx) {
// if (idx !== 16)
// return
const patch = diff(d[0], d[1])
it('returns a correct patch ' + idx, () => {
if (!d[2])
return assert.deepEqual(patch, [])
const op = {"op": d[2], "path": d[3]}
if (d[4] !== undefined)
op.value = d[4]
assert.deepEqual(patch, [op])
})
})
})