Skip to content

Commit 9e67c56

Browse files
Add merge functionality for global data.json (#1272)
1 parent 5b9e8b5 commit 9e67c56

File tree

3 files changed

+44
-18
lines changed

3 files changed

+44
-18
lines changed

packages/core/src/lib/compose.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const parseLink = require('./parseLink');
99
const render = require('./render');
1010
const uikitExcludePattern = require('./uikitExcludePattern');
1111
const pm = require('./plugin_manager');
12+
const dataMerger = require('./dataMerger');
1213
const pluginManager = new pm();
1314

1415
const Pattern = require('./object_factory').Pattern;
@@ -56,8 +57,14 @@ module.exports = async function(pattern, patternlab) {
5657
'listitems.json + any pattern listitems.json'
5758
);
5859

59-
allData = _.merge({}, patternlab.data, pattern.jsonFileData);
60-
allData = _.merge({}, allData, allListItems);
60+
allData = dataMerger(
61+
patternlab.data,
62+
pattern.jsonFileData,
63+
patternlab.config
64+
);
65+
// _.merge({}, patternlab.data, pattern.jsonFileData);
66+
allData = dataMerger(allData, allListItems, patternlab.config);
67+
// _.merge({}, allData, allListItems);
6168
allData.cacheBuster = patternlab.cacheBuster;
6269
allData.patternPartial = pattern.patternPartial;
6370

packages/core/src/lib/dataMerger.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const _ = require('lodash');
2+
3+
/**
4+
* Merges two objects depending on the configuration and will either merge
5+
* arrays and only replaces items on the index or replace the entire
6+
* collection of the different parameters
7+
*
8+
* @param {*} dataObject the object that contains the main data
9+
* @param {*} dataToMergeWithObject the object that should be merged with the original data
10+
* @param {*} patternlabConfig the patternlab configuration object
11+
*/
12+
module.exports = function(dataObject, dataToMergeWithObject, patternlabConfig) {
13+
return _.mergeWith(
14+
{},
15+
dataObject,
16+
dataToMergeWithObject,
17+
(objValue, srcValue) => {
18+
if (
19+
_.isArray(objValue) &&
20+
// If the parameter is not available after updating pattern lab but
21+
// not the patternlab-config it should not override arrays.
22+
patternlabConfig.hasOwnProperty('patternMergeVariantArrays') &&
23+
!patternlabConfig.patternMergeVariantArrays
24+
) {
25+
return srcValue;
26+
}
27+
// Lodash will only check for "undefined" and eslint needs a consistent
28+
// return so do not remove
29+
return undefined;
30+
}
31+
);
32+
};

packages/core/src/lib/pseudopattern_hunter.js

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const readDocumentation = require('./readDocumentation');
1313
const lineage_hunter = new lh();
1414
const changes_hunter = new ch();
1515
const yaml = require('js-yaml');
16+
const dataMerger = require('./dataMerger');
1617

1718
const pseudopattern_hunter = function() {};
1819

@@ -60,24 +61,10 @@ pseudopattern_hunter.prototype.find_pseudopatterns = function(
6061
}
6162

6263
//extend any existing data with variant data
63-
variantFileData = _.mergeWith(
64-
{},
64+
variantFileData = dataMerger(
6565
currentPattern.jsonFileData,
6666
variantFileData,
67-
(objValue, srcValue) => {
68-
if (
69-
_.isArray(objValue) &&
70-
// If the parameter is not available after updating pattern lab but
71-
// not the patternlab-config it should not override arrays.
72-
patternlab.config.hasOwnProperty('patternMergeVariantArrays') &&
73-
!patternlab.config.patternMergeVariantArrays
74-
) {
75-
return srcValue;
76-
}
77-
// Lodash will only check for "undefined" and eslint needs a consistent
78-
// return so do not remove
79-
return undefined;
80-
}
67+
patternlab.config
8168
);
8269

8370
const variantName = pseudoPatterns[i]

0 commit comments

Comments
 (0)