Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions packages/core/src/lib/compose.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const parseLink = require('./parseLink');
const render = require('./render');
const uikitExcludePattern = require('./uikitExcludePattern');
const pm = require('./plugin_manager');
const dataMerger = require('./dataMerger');
const pluginManager = new pm();

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

allData = _.merge({}, patternlab.data, pattern.jsonFileData);
allData = _.merge({}, allData, allListItems);
allData = dataMerger(
patternlab.data,
pattern.jsonFileData,
patternlab.config
);
// _.merge({}, patternlab.data, pattern.jsonFileData);
allData = dataMerger(allData, allListItems, patternlab.config);
// _.merge({}, allData, allListItems);
allData.cacheBuster = patternlab.cacheBuster;
allData.patternPartial = pattern.patternPartial;

Expand Down
32 changes: 32 additions & 0 deletions packages/core/src/lib/dataMerger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const _ = require('lodash');

/**
* Merges two objects depending on the configuration and will either merge
* arrays and only replaces items on the index or replace the entire
* collection of the different parameters
*
* @param {*} dataObject the object that contains the main data
* @param {*} dataToMergeWithObject the object that should be merged with the original data
* @param {*} patternlabConfig the patternlab configuration object
*/
module.exports = function(dataObject, dataToMergeWithObject, patternlabConfig) {
return _.mergeWith(
{},
dataObject,
dataToMergeWithObject,
(objValue, srcValue) => {
if (
_.isArray(objValue) &&
// If the parameter is not available after updating pattern lab but
// not the patternlab-config it should not override arrays.
patternlabConfig.hasOwnProperty('patternMergeVariantArrays') &&
!patternlabConfig.patternMergeVariantArrays
) {
return srcValue;
}
// Lodash will only check for "undefined" and eslint needs a consistent
// return so do not remove
return undefined;
}
);
};
19 changes: 3 additions & 16 deletions packages/core/src/lib/pseudopattern_hunter.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const readDocumentation = require('./readDocumentation');
const lineage_hunter = new lh();
const changes_hunter = new ch();
const yaml = require('js-yaml');
const dataMerger = require('./dataMerger');

const pseudopattern_hunter = function() {};

Expand Down Expand Up @@ -60,24 +61,10 @@ pseudopattern_hunter.prototype.find_pseudopatterns = function(
}

//extend any existing data with variant data
variantFileData = _.mergeWith(
{},
variantFileData = dataMerger(
currentPattern.jsonFileData,
variantFileData,
(objValue, srcValue) => {
if (
_.isArray(objValue) &&
// If the parameter is not available after updating pattern lab but
// not the patternlab-config it should not override arrays.
patternlab.config.hasOwnProperty('patternMergeVariantArrays') &&
!patternlab.config.patternMergeVariantArrays
) {
return srcValue;
}
// Lodash will only check for "undefined" and eslint needs a consistent
// return so do not remove
return undefined;
}
patternlab.config
);

const variantName = pseudoPatterns[i]
Expand Down