Skip to content

Commit 4310342

Browse files
committed
Array and Object as Plain
1 parent 8945ff1 commit 4310342

File tree

9 files changed

+90
-99
lines changed

9 files changed

+90
-99
lines changed

src/api/applyPatchFactory.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isPlainObject } from '../util/is'
1+
import { isPlainObject, isPlain } from '../util/is'
22
import { setDeep } from '../util/set'
33
import forEachObject from '../util/forEachObject'
44

@@ -12,9 +12,7 @@ export default function applyPatchFactory(patchers) {
1212
forEachObject(
1313
patch_root,
1414
target_root,
15-
({ origin, destiny, prop, path }) => {
16-
const target = destiny
17-
const patch = origin
15+
({ patch, target, prop, path }) => {
1816
const patch_value = patch[prop]
1917
const target_value = target[prop]
2018
const had_prop = target.hasOwnProperty(prop)

src/index.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +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'
11-
import Array from './types/Array'
10+
import Plain from './types/Plain'
1211
import Delete from './types/Delete'
1312
import Replace from './types/Replace'
1413
import Inner from './types/Inner'
@@ -33,8 +32,7 @@ function factory() {
3332
}
3433

3534
addType(Function)
36-
addType(Object)
37-
addType(Array)
35+
addType(Plain)
3836
addType(Delete)
3937
addType(Replace)
4038
addType(Inner)

src/types/Array.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/types/Object.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/types/Plain.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { isArray, isPlainObject } from '../util/is'
2+
import merge from '../util/merge'
3+
import Delete from './Delete'
4+
5+
export default function Plain() {}
6+
7+
Plain.patch = function ({ patch, target, prop, old_value, applyPatch }) {
8+
const patch_value = patch[prop]
9+
if (isPlainObject(patch_value)) {
10+
// if (isArray(old_value)) {
11+
// console.log({ patch, target, prop, old_value })
12+
// target[prop] = applyPatch([], patch_value).result
13+
// } else {
14+
target[prop] = applyPatch({}, patch_value).result
15+
// }
16+
}
17+
18+
// New array
19+
else if (isArray(patch_value)) {
20+
target[prop] = merge([], patch_value)
21+
}
22+
23+
return old_value
24+
}

src/util/converter.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,31 @@ import forEachObject from '../util/forEachObject'
22
import merge, { mergeMutator } from '../util/merge'
33
import { isArray } from '../util/is'
44

5-
export default function converter(origin, params, converters) {
6-
const destiny = isArray(origin) ? [] : {}
7-
forEachObject(origin, destiny, ({ origin, prop, destiny, path }) => {
5+
export default function converter(patch, params, converters) {
6+
const target = isArray(patch) ? [] : {}
7+
forEachObject(patch, target, ({ patch, prop, target, path }) => {
88
const value = converters.reduce(
99
(value, converter) =>
1010
converter(
1111
merge(
1212
{
1313
value,
14-
patch: origin,
15-
target: destiny,
14+
patch,
15+
target,
1616
prop,
17-
path
17+
path,
1818
},
1919
params
2020
)
2121
),
22-
origin[prop]
22+
patch[prop]
2323
)
24-
if (origin[prop] !== value) {
25-
destiny[prop] = value
24+
if (patch[prop] !== value) {
25+
target[prop] = value
2626
return false // we don't go deeper
2727
} else {
28-
return mergeMutator({ origin, destiny, prop })
28+
return mergeMutator({ patch, target, prop })
2929
}
3030
})
31-
return destiny
31+
return target
3232
}

src/util/forEachObject.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@ import forEach from './forEach'
33

44
// https://jsperf.com/dop-foreachobject
55
// https://2ality.com/2019/10/shared-mutable-state.html
6-
export default function forEachObject(origin, destiny, mutator) {
7-
forEachObjectLoop(origin, destiny, mutator, [])
6+
export default function forEachObject(patch, target, mutator) {
7+
forEachObjectLoop(patch, target, mutator, [])
88
}
99

10-
function forEachObjectLoop(origin, destiny, mutator, path) {
11-
forEach(origin, (value_origin, prop) => {
10+
function forEachObjectLoop(patch, target, mutator, path) {
11+
forEach(patch, (value_origin, prop) => {
1212
path.push(prop)
13-
const shallWeGoDown = mutator({ origin, destiny, prop, path })
14-
if (shallWeGoDown !== false && isObject(value_origin))
15-
forEachObjectLoop(value_origin, destiny[prop], mutator, path)
13+
const shallWeGoDown = mutator({ patch, target, prop, path })
14+
if (shallWeGoDown !== false && isObject(value_origin)) {
15+
forEachObjectLoop(value_origin, target[prop], mutator, path)
16+
}
1617
path.pop()
1718
})
1819
}

src/util/merge.js

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,88 +2,83 @@ import { is, isPlain } from './is'
22
import forEachObject from './forEachObject'
33

44
// https://jsperf.com/merge-challenge
5-
export default function merge(destiny, origin) {
5+
export default function merge(target, patch) {
66
const args = arguments
77
if (args.length > 2) {
8-
// Remove the destiny 2 arguments of the arguments and add thoose arguments as merged at the begining
9-
Array.prototype.splice.call(
10-
args,
11-
0,
12-
2,
13-
merge.call(this, destiny, origin)
14-
)
8+
// Remove the target 2 arguments of the arguments and add thoose arguments as merged at the begining
9+
Array.prototype.splice.call(args, 0, 2, merge.call(this, target, patch))
1510
// Recursion
1611
return merge.apply(this, args)
17-
} else if (origin === destiny) {
18-
return destiny
12+
} else if (patch === target) {
13+
return target
1914
} else {
20-
forEachObject(origin, destiny, mergeMutator)
21-
return destiny
15+
forEachObject(patch, target, mergeMutator)
16+
return target
2217
}
2318
}
2419

25-
export function mergeMutator({ origin, destiny, prop }) {
26-
const origin_value = origin[prop]
27-
const destiny_value = destiny[prop]
20+
export function mergeMutator({ patch, target, prop }) {
21+
const origin_value = patch[prop]
22+
const destiny_value = target[prop]
2823
const tof_origin = is(origin_value)
2924
const tof_destiny = is(destiny_value)
3025
if (isPlain(origin_value)) {
31-
if (!destiny.hasOwnProperty(prop) || tof_origin != tof_destiny) {
32-
destiny[prop] = tof_origin == 'array' ? [] : {}
26+
if (!target.hasOwnProperty(prop) || tof_origin != tof_destiny) {
27+
target[prop] = tof_origin == 'array' ? [] : {}
3328
}
3429
} else {
35-
destiny[prop] = origin_value
30+
target[prop] = origin_value
3631
return false // we dont go deeper
3732
}
3833
}
3934

40-
// export default createCustomMerge('merge', ({ origin, destiny, prop }) => {
41-
// const origin_value = origin[prop]
42-
// const destiny_value = destiny[prop]
35+
// export default createCustomMerge('merge', ({ patch, target, prop }) => {
36+
// const origin_value = patch[prop]
37+
// const destiny_value = target[prop]
4338
// const tof_origin = is(origin_value)
4439
// const tof_destiny = is(destiny_value)
4540
// if (isPlain(origin_value)) {
46-
// if (!destiny.hasOwnProperty(prop) || tof_origin !== tof_destiny) {
47-
// destiny[prop] = tof_origin === 'array' ? [] : {}
41+
// if (!target.hasOwnProperty(prop) || tof_origin !== tof_destiny) {
42+
// target[prop] = tof_origin === 'array' ? [] : {}
4843
// if (tof_origin === 'array') {
4944
// const array = []
5045
// if (tof_destiny === 'object') {
5146
// Object.keys(destiny_value)
5247
// .filter(key => !isNaN(Number(key)))
5348
// .forEach(key => (array[key] = destiny_value[key]))
5449
// }
55-
// destiny[prop] = array
50+
// target[prop] = array
5651
// } else {
57-
// destiny[prop] = {}
52+
// target[prop] = {}
5853
// }
5954
// }
60-
// } else if (tof_origin === 'undefined' && destiny.hasOwnProperty(prop)) {
55+
// } else if (tof_origin === 'undefined' && target.hasOwnProperty(prop)) {
6156
// //skipping
6257
// } else {
63-
// destiny[prop] = origin_value
58+
// target[prop] = origin_value
6459
// return true // skipping
6560
// }
6661
// })
6762

6863
// // https://stackoverflow.com/questions/5905492/dynamic-function-name-in-javascript
6964
// export default function createCustomMerge(function_name, mutator) {
70-
// return createFunction(function_name, function recursive(destiny, origin) {
65+
// return createFunction(function_name, function recursive(target, patch) {
7166
// const args = arguments
7267
// if (args.length > 2) {
73-
// // Remove the destiny 2 arguments of the arguments and add thoose arguments as recursived at the begining
68+
// // Remove the target 2 arguments of the arguments and add thoose arguments as recursived at the begining
7469
// Array.prototype.splice.call(
7570
// args,
7671
// 0,
7772
// 2,
78-
// recursive.call(this, destiny, origin)
73+
// recursive.call(this, target, patch)
7974
// )
8075
// // Recursion
8176
// return recursive.apply(this, args)
82-
// } else if (origin === destiny) {
83-
// return destiny
77+
// } else if (patch === target) {
78+
// return target
8479
// } else {
85-
// forEachObject(origin, mutator, destiny)
86-
// return destiny
80+
// forEachObject(patch, mutator, target)
81+
// return target
8782
// }
8883
// })
8984
// }

test/applyPatch.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ test('array complex', function (t) {
251251
})
252252

253253
test('from target to array', function (t) {
254-
const target = { objarr: { value: true } }
254+
const target = { objarr: { length: 5 } }
255255
const patch = { objarr: [0, 1] }
256256
const expected = { objarr: [0, 1] }
257257

@@ -433,11 +433,19 @@ test('checking different types and mutating multiple deep values', function (t)
433433
})
434434

435435
test.skip('Inner plain array', function (t) {
436-
const target = { a: [{ b: false }] }
437-
const patch = { a: { 0: { b: true } } }
438-
const expected = { a: [{ b: true }] }
436+
const target = [{ b: false }]
437+
const patch = { 0: { b: true } }
438+
const expected = [{ b: true }]
439439

440-
testPatchUnpatch(t, target, patch, expected, false)
440+
// console.log(target, expected)
441+
const { result, unpatch } = testPatchUnpatch(
442+
t,
443+
target,
444+
patch,
445+
expected,
446+
true
447+
)
448+
// console.log(target, result, unpatch)
441449
})
442450

443451
test('1 / https://tools.ietf.org/html/rfc7386 ', function (t) {
@@ -562,7 +570,7 @@ test('13/c (not sure about this case)', function (t) {
562570
})
563571

564572
test('14 / https://tools.ietf.org/html/rfc7386 ', function (t) {
565-
const target = [1, 2]
573+
const target = 'string' // [1, 2]
566574
const patch = { a: 'b', c: TYPE.Delete() }
567575
const expected = { a: 'b' }
568576

0 commit comments

Comments
 (0)