Skip to content

Commit 8945ff1

Browse files
committed
Object type
1 parent b5bf6ec commit 8945ff1

File tree

5 files changed

+128
-98
lines changed

5 files changed

+128
-98
lines changed

src/api/applyPatchFactory.js

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,45 +13,44 @@ export default function applyPatchFactory(patchers) {
1313
patch_root,
1414
target_root,
1515
({ origin, destiny, prop, path }) => {
16-
const origin_value = origin[prop]
17-
const destiny_value = destiny[prop]
18-
const had_prop = destiny.hasOwnProperty(prop)
16+
const target = destiny
17+
const patch = origin
18+
const patch_value = patch[prop]
19+
const target_value = target[prop]
20+
const had_prop = target.hasOwnProperty(prop)
1921
if (
2022
!had_prop ||
21-
(origin_value !== destiny_value &&
23+
(patch_value !== target_value &&
2224
!(
23-
isPlainObject(origin_value) &&
24-
isPlainObject(destiny_value)
25+
isPlainObject(patch_value) &&
26+
isPlainObject(target_value)
2527
))
2628
) {
27-
// This is where the merge with plain objects happen
28-
destiny[prop] = isPlainObject(origin_value)
29-
? applyPatch({}, origin_value).result
30-
: origin_value
29+
target[prop] = patch_value
3130

3231
// Applying patches
3332
const old_value = patchers.reduce(
3433
(old_value, p) =>
3534
p({
36-
patch: origin,
37-
target: destiny,
35+
patch,
36+
target,
3837
prop,
3938
path,
4039
old_value,
4140
had_prop,
42-
applyPatch
41+
applyPatch,
4342
}),
44-
destiny_value
43+
target_value
4544
)
4645

4746
// We register the mutation if old_value is different to the new value
48-
if (destiny[prop] !== old_value) {
47+
if (target[prop] !== old_value) {
4948
setDeep(unpatch_root, path.slice(0), old_value)
5049
mutations.push({
5150
old_value,
52-
object: destiny,
51+
object: target,
5352
prop,
54-
path: path.slice(1)
53+
path: path.slice(1),
5554
})
5655
}
5756

@@ -63,7 +62,7 @@ export default function applyPatchFactory(patchers) {
6362
return {
6463
result: target_root[''],
6564
unpatch: unpatch_root[''],
66-
mutations
65+
mutations,
6766
}
6867
}
6968
}

src/index.js

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

99
import Function from './types/Function'
10+
import Object from './types/Object'
1011
import Array from './types/Array'
1112
import Delete from './types/Delete'
1213
import Replace from './types/Replace'
@@ -32,6 +33,7 @@ function factory() {
3233
}
3334

3435
addType(Function)
36+
addType(Object)
3537
addType(Array)
3638
addType(Delete)
3739
addType(Replace)

src/types/Array.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
import { isArray } from '../util/is'
1+
import { isArray, isPlainObject } from '../util/is'
22
import merge from '../util/merge'
33

44
export default function Array() {}
55

6-
Array.patch = function({ patch, target, prop, old_value }) {
6+
Array.patch = function ({ patch, target, prop, old_value }) {
77
const patch_value = patch[prop]
88
if (isArray(patch_value)) {
99
target[prop] = merge([], patch_value)
1010
}
11+
12+
// if (isPlainObject(patch_value)) {
13+
// if (isArray(old_value)) {
14+
// console.log({ patch_value, old_value })
15+
// }
16+
// }
17+
1118
return old_value
1219
}

src/types/Object.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { isArray, isPlainObject } from '../util/is'
2+
import merge from '../util/merge'
3+
4+
export default function Object() {}
5+
6+
Object.patch = function ({ patch, target, prop, old_value, applyPatch }) {
7+
const patch_value = patch[prop]
8+
if (isPlainObject(patch_value)) {
9+
console.log('entra???')
10+
target[prop] = applyPatch({}, patch_value).result
11+
}
12+
13+
return old_value
14+
}

0 commit comments

Comments
 (0)