Skip to content

Commit a795099

Browse files
committed
Small changes applyPatch
1 parent 4d3591f commit a795099

File tree

4 files changed

+26
-17
lines changed

4 files changed

+26
-17
lines changed

src/api/applyPatchFactory.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ export default function applyPatchFactory(patchers) {
1515
({ patch, target, prop, path }) => {
1616
const patch_value = patch[prop]
1717
const target_value = target[prop]
18-
const had_prop = target.hasOwnProperty(prop)
1918
if (
20-
!had_prop ||
19+
!target.hasOwnProperty(prop) ||
2120
(patch_value !== target_value &&
2221
!(
2322
isPlainObject(patch_value) &&
@@ -32,7 +31,6 @@ export default function applyPatchFactory(patchers) {
3231
target,
3332
prop,
3433
old_value,
35-
had_prop,
3634
applyPatch,
3735
}),
3836
target_value

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import createNodeFactory from './api/createNodeFactory'
66
import createStoreFactory from './api/createStoreFactory'
77
import applyPatchFactory from './api/applyPatchFactory'
88

9-
import Function from './types/Function'
109
import Primitives from './types/Primitives'
1110
import Delete from './types/Delete'
1211
import Replace from './types/Replace'
1312
import Splice from './types/Splice'
1413
import Swap from './types/Swap'
14+
import Function from './types/Function'
1515

1616
function factory() {
1717
const patchers = []
@@ -30,12 +30,12 @@ function factory() {
3030
if (isFunction(decode)) decoders.push(decode)
3131
}
3232

33-
addType(Function)
3433
addType(Primitives)
3534
addType(Delete)
3635
addType(Replace)
3736
addType(Splice)
3837
addType(Swap)
38+
addType(Function)
3939

4040
return {
4141
version,

src/types/Primitives.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,10 @@ import Delete from './Delete'
55

66
export default function Primitives() {}
77

8-
Primitives.patch = function ({
9-
patch,
10-
target,
11-
prop,
12-
old_value,
13-
had_prop,
14-
applyPatch,
15-
}) {
8+
Primitives.patch = function ({ patch, target, prop, old_value, applyPatch }) {
169
const patch_value = patch[prop]
1710

18-
if (!had_prop) {
11+
if (!target.hasOwnProperty(prop)) {
1912
old_value = new Delete()
2013
}
2114

@@ -34,14 +27,15 @@ Primitives.patch = function ({
3427
const unpatches = {}
3528
const length = old_value.length
3629
for (const key in patch_value) {
37-
const had_prop = old_value.hasOwnProperty(key)
3830
const patched = applyPatch(old_value[key], patch_value[key])
3931
if (
4032
old_value[key] !== patched.result ||
4133
isPlainObject(patch_value[key])
4234
) {
4335
old_value[key] = patched.result
44-
unpatches[key] = had_prop ? patched.unpatch : Delete()
36+
unpatches[key] = old_value.hasOwnProperty(key)
37+
? patched.unpatch
38+
: Delete()
4539
}
4640
}
4741
if (old_value.length !== length) {

test/applyPatch.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ test('13/b', function (t) {
114114

115115
testPatchUnpatch(t, target, patch, expected)
116116
})
117+
117118
test('13/c (not sure about this case)', function (t) {
118119
const target = { e: TYPE.Delete() }
119120
const patch = { a: 1, e: TYPE.Delete() }
@@ -128,7 +129,7 @@ test('14 / https://tools.ietf.org/html/rfc7386 ', function (t) {
128129
const patch = { a: 'b', c: TYPE.Delete() }
129130
const expected = { a: 'b' }
130131

131-
testPatchUnpatch(t, target, patch, expected, false)
132+
testPatchUnpatch(t, target, patch, expected)
132133
})
133134

134135
test('15 / https://tools.ietf.org/html/rfc7386 ', function (t) {
@@ -708,3 +709,19 @@ test('Changing lengh array', function (t) {
708709
const expected = ['A', undefined]
709710
testPatchUnpatch(t, target, patch, expected)
710711
})
712+
713+
test('adding inner arrays', function (t) {
714+
const target = { objarr: [0, 1, [false], [[]]] }
715+
const patch = { objarr: { 2: [2], 3: { 0: { 0: [3] } } } }
716+
const expected = { objarr: [0, 1, [2], [[[3]]]] }
717+
718+
testPatchUnpatch(t, target, patch, expected)
719+
})
720+
721+
test('adding inner arrays as top level', function (t) {
722+
const target = [0, 1, [false], [[]]]
723+
const patch = { length: 5, 2: [2], 3: { 2: 4, 0: { 0: [3] } } }
724+
const expected = [0, 1, [2], [[[3]], undefined, 4], undefined]
725+
726+
testPatchUnpatch(t, target, patch, expected)
727+
})

0 commit comments

Comments
 (0)