Skip to content

Commit 492dea0

Browse files
committed
Splice and Replace can encode and decode internally
1 parent 5c1ccd4 commit 492dea0

File tree

5 files changed

+58
-34
lines changed

5 files changed

+58
-34
lines changed

src/types/Replace.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@ Replace.patch = function ({ patch, target, prop, old_value }) {
1616
return old_value
1717
}
1818

19-
Replace.encode = function ({ value }) {
19+
Replace.encode = function ({ value, encode }) {
2020
if (value instanceof Replace) {
21-
return { [REPLACE_KEY]: value.value }
21+
return { [REPLACE_KEY]: encode(value.value) }
2222
} else if (isValidToDecode({ value, key: REPLACE_KEY })) {
2323
return { [ESCAPE_KEY]: value }
2424
}
2525
return value
2626
}
2727

28-
Replace.decode = function ({ value }) {
28+
Replace.decode = function ({ value, decode }) {
2929
if (isValidToDecode({ value, key: REPLACE_KEY })) {
30-
return new Replace(value[REPLACE_KEY])
30+
return new Replace(decode(value[REPLACE_KEY]))
3131
} else if (
3232
isValidToEscape({ value }) &&
3333
isValidToDecode({ value: value[ESCAPE_KEY], key: REPLACE_KEY })

src/types/Splice.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@ Splice.patch = function ({ patch, target, prop, old_value }) {
2727
return old_value
2828
}
2929

30-
Splice.encode = function ({ value }) {
30+
Splice.encode = function ({ value, encode }) {
3131
if (value instanceof Splice) {
32-
return { [SPLICE_KEY]: value.args }
32+
return { [SPLICE_KEY]: encode(value.args) }
3333
} else if (isValidToDecode({ value, key: SPLICE_KEY })) {
3434
return { [ESCAPE_KEY]: value }
3535
}
3636
return value
3737
}
3838

39-
Splice.decode = function ({ value }) {
39+
Splice.decode = function ({ value, decode }) {
4040
if (isValidToDecode({ value, key: SPLICE_KEY })) {
41-
return Splice.apply(null, value[SPLICE_KEY])
41+
return Splice.apply(null, decode(value[SPLICE_KEY]))
4242
} else if (
4343
isValidToEscape({ value }) &&
4444
isValidToDecode({ value: value[ESCAPE_KEY], key: SPLICE_KEY })

test/type_multi.js

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,41 @@ test('Inner encode', function (t) {
1515
const { encoded, decoded } = testEncodeDecode(t, patch, expected)
1616
})
1717

18-
// test('Escape', function (t) {
19-
// const patch = { escape: { $w: [0, 1] } }
20-
// const expected = { escape: { $escape: { $w: [0, 1] } } }
21-
// testEncodeDecode(t, patch, expected)
22-
// })
18+
test('Escape', function (t) {
19+
const patch = { escape: { $m: [0, 1] } }
20+
const expected = { escape: { $escape: { $m: [0, 1] } } }
21+
testEncodeDecode(t, patch, expected)
22+
})
2323

24-
// test('Ignore', function (t) {
25-
// const patch = { ignore: { $w: { a: 2 } } }
26-
// const expected = { ignore: { $w: { a: 2 } } }
27-
// testEncodeDecode(t, patch, expected)
28-
// })
24+
test('Ignore', function (t) {
25+
const patch = { ignore: { $m: { a: 2 } } }
26+
const expected = { ignore: { $m: { a: 2 } } }
27+
testEncodeDecode(t, patch, expected)
28+
})
2929

30-
// test('API', function (t) {
31-
// const target = { array: ['a', 'b'] }
32-
// const patch = { array: TYPE.Swap(0, 1) }
33-
// const expected = { array: ['b', 'a'] }
34-
// const { unpatch, result, mutations } = testPatchUnpatch(
35-
// t,
36-
// target,
37-
// patch,
38-
// expected
39-
// )
40-
// t.is(mutations.length, 1)
41-
// t.is(result.array, target.array)
42-
// t.true(unpatch.array instanceof TYPE.Swap)
43-
// t.deepEqual(unpatch.array.args, [1, 0])
44-
// })
30+
test('API', function (t) {
31+
const target = { array: ['a', 'b'] }
32+
const array = target.array
33+
const patch = {
34+
array: TYPE.Multi(
35+
TYPE.Splice(0, 0, 'c'),
36+
TYPE.Swap(0, 2),
37+
TYPE.Swap(1, 2)
38+
),
39+
}
40+
const expected = { array: ['b', 'c', 'a'] }
41+
const { unpatch, result, mutations } = testPatchUnpatch(
42+
t,
43+
target,
44+
patch,
45+
expected
46+
)
47+
t.is(mutations.length, 1)
48+
t.is(result.array, target.array)
49+
t.is(array, target.array)
50+
t.true(unpatch.array instanceof TYPE.Multi)
51+
t.is(unpatch.array.values.length, 3)
52+
})
4553

4654
test('Deleting', function (t) {
4755
const target = { array: false }
@@ -57,7 +65,7 @@ test('Adding array and swaping', function (t) {
5765
testPatchUnpatch(t, target, patch, expected)
5866
})
5967

60-
test('Undefined before applying Multi', function (t) {
68+
test('Undefined before applying Multi', function (t) {
6169
const target = {}
6270
const patch = { array: TYPE.Multi(true) }
6371
const expected = { array: true }

test/type_replace.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,11 @@ test('same behavior as replace array', function (t) {
133133

134134
testPatchUnpatch(t, target, patch, expected)
135135
})
136+
137+
test('inner types', function (t) {
138+
const target = { value: { a: 1, b: 2 } }
139+
const patch = { value: TYPE.Replace({ a: 1, b: TYPE.Swap(1, 2) }) }
140+
const expected = { value: { a: 1, b: TYPE.Swap(1, 2) } }
141+
142+
testPatchUnpatch(t, target, patch, expected, false)
143+
})

test/type_splice.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ function testAgainstNative(t, original, params) {
9696
// console.log({ result: target })
9797
}
9898

99+
test('inner types', function (t) {
100+
const target = { value: [0, 1] }
101+
const patch = { value: TYPE.Splice(2, 0, TYPE.Swap(1, 2)) }
102+
const expected = { value: [0, 1, TYPE.Swap(1, 2)] }
103+
104+
testPatchUnpatch(t, target, patch, expected, false)
105+
})
106+
99107
test('1/ vs Array.splice', function (t) {
100108
const original = ['angel', 'clown', 'mandarin', 'sturgeon']
101109
const params = [0, 0]

0 commit comments

Comments
 (0)