-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathprop.js
77 lines (69 loc) · 1.31 KB
/
prop.js
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
import { safeGetProp } from './get.js'
import { omitInvalidKey } from './key.js'
import { addSize } from './size.js'
// Transform an object property or an array item
export const transformProp = ({
parent,
changes,
ancestors,
path,
maxSize,
key,
type,
empty,
size,
recurse,
}) => {
const propPath = [...path, key]
const { size: sizeA, stop } = addSize({
type,
size,
maxSize,
changes,
path: propPath,
context: { empty, parent, key },
})
if (stop) {
return { empty, size }
}
const { value, size: sizeB } = transformPropValue({
parent,
key,
changes,
ancestors,
path: propPath,
size: sizeA,
maxSize,
recurse,
})
return value === undefined
? { empty, size }
: { empty: false, size: sizeB, value }
}
// Recurse over an object property or array index
const transformPropValue = ({
parent,
key,
changes,
ancestors,
path,
size,
maxSize,
recurse,
}) => {
const { prop, safe } = safeGetProp({ parent, key, changes, path })
if (!safe) {
return { value: prop, size }
}
const { prop: propA, validKey } = omitInvalidKey({
parent,
key,
prop,
changes,
path,
})
if (!validKey) {
return { value: propA, size }
}
return recurse({ value: propA, changes, ancestors, path, size, maxSize })
}