-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
executable file
·217 lines (178 loc) · 6.64 KB
/
index.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
var _ = require('./common');
var split = require('./path/split');
var expand = require('./path/expand');
var testPath = require('./path/test');
var Operations = require('./operations');
/** @type {Function} */
exports.expand = expand;
/** @type {Function} */
exports.patch = patch;
/** @type {Function} */
exports.register = Operations.register;
/** @type {Function} */
exports.unregister = Operations.unregister;
/** @type {Function} */
exports.get = get;
/** @type {Function} */
exports.set = set;
/** @type {Function} */
exports.split = split;
/** @type {Function} */
exports.testPath = testPath;
/**
* Patch a supplied target using the supplied source.
* @param {Object} target Target object.
* @param {Object} source Source object.
* @param {Array} patches Patches to run.
* @param {Array} log Array to push entries into.
* @returns {Object}
*/
function patch(target, source, patches, log, ctx) {
var dirty = false;
if (!_.isArray(patches)) {
return dirty;
}
for (var patchIdx = 0; patchIdx < patches.length; patchIdx++) {
// Globals
var patch = Object.assign({}, {
source: {},
target: {},
operations: []
}, patches[patchIdx]);
var merge = patch.merge;
var depth = _.toInt(patch.depth, 0);
var collect = patch.collect;
var unique = patch.unique;
var operations = patch.operations;
var targetAsSource = patch.targetAsSource;
// Source
var unresolvedSourcePath = patch.source.path;
var sourceIsRoot = unresolvedSourcePath === '/';
var sourcePaths = sourceIsRoot ? [undefined] : // this is essentially a noop to symbolize a root
expand(source, unresolvedSourcePath, true, true); // paths must exist
var sourceTest = patch.source.tests;
// Target
var unresolvedTargetPath = patch.target.path;
var targetIsRoot = unresolvedTargetPath === '/';
var targetPaths = targetIsRoot ? [undefined] : // this is essentially a noop to symbolize a root
expand(target, unresolvedTargetPath, false, true); // path existance is optional
var targetTest = patch.target.tests;
// Updater
var transformValue = function(sourcePath, sourceValue, targetPath, targetValue, isRoot) {
isRoot = !!isRoot;
// Integration of the regex validity for value/object conformity;
// Returns `targetValue` should this not pass...
if (targetTest && testPath(targetTest, target, source, targetPath, sourcePath, unresolvedTargetPath, unresolvedSourcePath) === false) {
return targetValue;
}
var newValue = targetAsSource ? targetValue : sourceValue;
if (_.isArray(operations)) {
for (var operationIdx = 0; operationIdx < operations.length; operationIdx++) {
newValue = Operations.execute(operations[operationIdx], newValue, ctx);
}
}
var cleanValue = newValue;
// Explicitly merge `newValue` over `targetValue` replacing
// any object keys or collection indices with the
// appropriate values;
if (merge && _.isObject(newValue) && _.isObject(targetValue)) {
newValue = _.extend(
_.isArray(targetValue) ? [] : {},
targetValue,
newValue
);
}
// Create or add to an existing collection
if (collect === true) {
if (targetValue !== undefined) {
if (isRoot && _.isPlainObject(targetValue)) {
if (_.isArrayLike(targetValue)) {
var newValueArray = _.toArray(targetValue).concat(newValue);
var newArrayLikeValue = {
length: newValueArray.length
};
for (var nvArrayIdx = 0; nvArrayIdx < newValueArray.length; nvArrayIdx++) {
newArrayLikeValue[nvArrayIdx] = newValueArray[nvArrayIdx];
}
newValue = newArrayLikeValue;
} else {
newValue = {
0: newValue,
length: 1
};
}
} else {
newValue = (_.isArray(targetValue) ? targetValue : [targetValue]).concat(newValue);
}
} else {
newValue = _.isArray(newValue) ? newValue : [newValue];
}
}
// Remove non-unique values from a given collection
if (unique && _.isArray(newValue) && newValue.length > 1) {
newValue = _.uniqWith(newValue, _.isEqual);
}
dirty = true;
if (_.isArray(log)) {
log.push({
to: targetPath,
from: sourcePath,
value: cleanValue,
patch: patch
});
}
return newValue;
};
for (var sourcePathIdx = 0, sourceLen = targetAsSource ? 1 : sourcePaths.length; sourcePathIdx < sourceLen; sourcePathIdx++) {
var sourcePath = sourcePaths[sourcePathIdx];
var sourceValue = sourcePath === undefined ? source : _.get(source, sourcePath);
for (var targetPathIdx = 0; targetPathIdx < targetPaths.length; targetPathIdx++) {
var targetPath = targetIsRoot ? [] : targetPaths[targetPathIdx];
if (depth < 0) {
targetPath = targetPath.concat(sourcePath.slice(depth));
}
// Integration of the regex validity for value/object conformity;
// Skip single source patch operation should this not pass...
if (sourceTest && testPath(sourceTest, source, target, sourcePath, targetPath, unresolvedSourcePath, unresolvedTargetPath) === false) {
continue;
}
if (targetPath.length === 0) {
var newRootValue = transformValue(sourcePath, sourceValue, targetPath, target, true);
if (_.isArray(target)) {
target.splice.apply(target, [0, target.length].concat(newRootValue));
} else if (_.isPlainObject(target)) {
for (var keyIdx = 0, keys = Object.keys(target); keyIdx < keys.length; keyIdx++) {
delete target[keys[keyIdx]];
}
_.extend(target, newRootValue);
}
} else {
_.update(target, targetPath, _.partial(transformValue, sourcePath, sourceValue, targetPath));
}
}
}
}
return dirty;
}
/**
* Get a value using a patchwork compatible path.
* @param {Object} input Source object.
* @param {(Array|String)} path Key-path to get.
* @param {mixed} def Default value.
* @returns {mixed}
*/
function get(input, path, def) {
path = split(path);
return path.length ? _.get(input, path, def) : input;
}
/**
* Set a value using a patchwork compatible path.
* @param {Object} input Target object.
* @param {(Array|String)} path Key-path to set.
* @param {mixed} value Value to set.
* @returns {Object}
*/
function set(input, path, value) {
path = split(path);
return path.length ? _.set(input, path, value) : value;
}