UNPKG

1.45 kBJavaScriptView Raw
1const assign = require('lodash.assign');
2function mergeArraysOnUniqueProp(key, target, source, prop) {
3 if (!target) throw new Error('No target to merge to!');
4 if (!source) throw new Error('No source to merge from!')
5 if (!target[key]) {
6 target[key] = source[key];
7 return;
8 }
9 if (!source[key]) {
10 return;
11 }
12 if (!Array.isArray(target[key])) {
13 throw new Error(`Expected ${key} property of target to be an array and ` +
14 `it was ${target[key]}`);
15 }
16 if (!Array.isArray(source[key])) {
17 throw new Error(`Expected ${key} property of source to be an array and ` +
18 `it was ${source[key]}`);
19 }
20 target[key] = target[key].concat(source[key].filter(item =>
21 target[key].every(targetItem => targetItem[prop] !== item[prop])));
22}
23
24module.exports = {
25 mergeThemeJson: function(target, source) {
26 target.settings = assign(target.settings || {}, source.settings);
27 [
28 'editors',
29 'emailTemplates',
30 'backofficeTemplates',
31 'pageTypes',
32 'widgets'
33 ].forEach(collection => mergeArraysOnUniqueProp(
34 collection,
35 target,
36 source,
37 'id'
38 ));
39 return target;
40 },
41 mergeLabels: function(target, source) {
42 // prefer own labels
43 return assign({}, source, target);
44 },
45 mergeThemeUI: function(target, source) {
46 // for now, not an optimistic merge; we'll take yours if you have it
47 return target = source;
48 }
49}