diff --git a/packages/core/src/lib/compose.js b/packages/core/src/lib/compose.js index 63dd8aa3f..1816b1dfc 100644 --- a/packages/core/src/lib/compose.js +++ b/packages/core/src/lib/compose.js @@ -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; @@ -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; diff --git a/packages/core/src/lib/dataMerger.js b/packages/core/src/lib/dataMerger.js new file mode 100644 index 000000000..5ebdd21bb --- /dev/null +++ b/packages/core/src/lib/dataMerger.js @@ -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; + } + ); +}; diff --git a/packages/core/src/lib/pseudopattern_hunter.js b/packages/core/src/lib/pseudopattern_hunter.js index 88f1ad646..1d1ee92a3 100644 --- a/packages/core/src/lib/pseudopattern_hunter.js +++ b/packages/core/src/lib/pseudopattern_hunter.js @@ -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() {}; @@ -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]