Skip to content

Commit b0fdb01

Browse files
committed
Merge branch 'mergeData-2nd-param-priority-over-1st' of https://github.com/e2tha-e/patternlab-node into e2tha-e-mergeData-2nd-param-priority-over-1st
2 parents ef23440 + aab79d7 commit b0fdb01

File tree

3 files changed

+155
-16
lines changed

3 files changed

+155
-16
lines changed

builder/pattern_assembler.js

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -167,24 +167,39 @@
167167
throw 'Could not find pattern with key ' + key;
168168
}
169169

170-
171-
var self = this;
172-
function mergeData(obj1, obj2) {
173-
for (var p in obj2) {
170+
/**
171+
* Recursively merge properties of two objects.
172+
*
173+
* @param {Object} obj1 If obj1 has properties obj2 doesn't, add to obj2.
174+
* @param {Object} obj2 This object's properties have priority over obj1.
175+
* @returns {Object} obj2
176+
*/
177+
function mergeData(obj1, obj2){
178+
if(typeof obj2 === 'undefined'){
179+
obj2 = {};
180+
}
181+
for(var p in obj1){
174182
try {
175-
// Property in destination object set; update its value.
176-
if ( obj2[p].constructor == Object ) {
177-
obj1[p] = self.merge_data(obj1[p], obj2[p]);
178-
179-
} else {
180-
obj1[p] = obj2[p];
183+
// Only recurse if obj1[p] is an object.
184+
if(obj1[p].constructor === Object){
185+
// Requires 2 objects as params; create obj2[p] if undefined.
186+
if(typeof obj2[p] === 'undefined'){
187+
obj2[p] = {};
188+
}
189+
obj2[p] = mergeData(obj1[p], obj2[p]);
190+
// Pop when recursion meets a non-object. If obj1[p] is a non-object,
191+
// only copy to undefined obj2[p]. This way, obj2 maintains priority.
192+
} else if(typeof obj2[p] === 'undefined'){
193+
obj2[p] = obj1[p];
181194
}
182195
} catch(e) {
183196
// Property in destination object not set; create it and set its value.
184-
obj1[p] = obj2[p];
197+
if(typeof obj2[p] === 'undefined'){
198+
obj2[p] = obj1[p];
199+
}
185200
}
186201
}
187-
return obj1;
202+
return obj2;
188203
}
189204

190205
function buildListItems(patternlab){

builder/pseudopattern_hunter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
var variantFileData = fs.readJSONSync('source/_patterns/' + pseudoPatterns[i]);
4646

4747
//extend any existing data with variant data
48-
variantFileData = pattern_assembler.merge_data(variantFileData, currentPattern.jsonFileData);
48+
variantFileData = pattern_assembler.merge_data(currentPattern.jsonFileData, variantFileData);
4949

5050
var variantName = pseudoPatterns[i].substring(pseudoPatterns[i].indexOf('~') + 1).split('.')[0];
5151
var patternVariant = new of.oPattern(currentPattern.subdir, currentPattern.fileName + '-' + variantName + '.mustache', variantFileData);

test/list_item_hunter_tests.js

Lines changed: 127 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
test.done();
101101
},
102102

103-
'process_list_item_partials overwrites listItem data if local .listitem.json is found' : function(test){
103+
'process_list_item_partials overwrites listItem property if that property is in local .listitem.json' : function(test){
104104
//arrange
105105
//setup current pattern from what we would have during execution
106106
var currentPattern = {
@@ -117,7 +117,7 @@
117117
"title": "Two"
118118
},
119119
]
120-
}
120+
}
121121
};
122122

123123
var patternlab = {
@@ -155,10 +155,134 @@
155155

156156
//act
157157
list_item_hunter.process_list_item_partials(currentPattern, patternlab);
158-
158+
159159
//assert
160160
test.equals(currentPattern.extendedTemplate, "OneTwo" );
161161

162+
test.done();
163+
},
164+
165+
'process_list_item_partials keeps listItem property if that property is not in local .listitem.json' : function(test){
166+
//arrange
167+
//setup current pattern from what we would have during execution
168+
var currentPattern = {
169+
"template": "{{#listItems.one}}{{ title }}{{/listItems.one}}",
170+
"extendedTemplate" : "{{#listItems.one}}{{> test-simple }}{{/listItems.one}}",
171+
"key": "test-patternName",
172+
"jsonFileData" : {},
173+
"patternSpecificListJson" : {
174+
"2": [
175+
{
176+
"title": "One"
177+
},
178+
{
179+
"title": "Two"
180+
},
181+
]
182+
}
183+
};
184+
185+
var patternlab = {
186+
"listitems": {
187+
"1": [
188+
{
189+
"title": "Foo"
190+
}
191+
],
192+
"2": [
193+
{
194+
"title": "Foo"
195+
},
196+
{
197+
"title": "Bar"
198+
}
199+
]
200+
},
201+
"data": {
202+
"link": {},
203+
"partials": []
204+
},
205+
"config": {"debug": false},
206+
"patterns": [
207+
{
208+
"template": "{{ title }}",
209+
"extendedTemplate" : "{{ title }}",
210+
"key": "test-simple",
211+
"jsonFileData" : {}
212+
}
213+
]
214+
};
215+
216+
var list_item_hunter = new lih();
217+
218+
//act
219+
list_item_hunter.process_list_item_partials(currentPattern, patternlab);
220+
221+
//assert
222+
test.equals(currentPattern.extendedTemplate, "Foo" );
223+
224+
test.done();
225+
},
226+
227+
'process_list_item_partials uses local listItem property if that property is not set globally' : function(test){
228+
//arrange
229+
//setup current pattern from what we would have during execution
230+
var currentPattern = {
231+
"template": "{{#listItems.one}}{{ title }}{{/listItems.one}}",
232+
"extendedTemplate" : "{{#listItems.one}}{{> test-simple }}{{/listItems.one}}",
233+
"key": "test-patternName",
234+
"jsonFileData" : {},
235+
"patternSpecificListJson" : {
236+
"1": [
237+
{
238+
"title": "One"
239+
}
240+
],
241+
"2": [
242+
{
243+
"title": "One"
244+
},
245+
{
246+
"title": "Two"
247+
},
248+
]
249+
}
250+
};
251+
252+
var patternlab = {
253+
"listitems": {
254+
"2": [
255+
{
256+
"title": "Foo"
257+
},
258+
{
259+
"title": "Bar"
260+
}
261+
]
262+
},
263+
"data": {
264+
"link": {},
265+
"partials": []
266+
},
267+
"config": {"debug": false},
268+
"patterns": [
269+
{
270+
"template": "{{ title }}",
271+
"extendedTemplate" : "{{ title }}",
272+
"key": "test-simple",
273+
"jsonFileData" : {}
274+
}
275+
]
276+
};
277+
278+
var list_item_hunter = new lih();
279+
280+
//act
281+
list_item_hunter.process_list_item_partials(currentPattern, patternlab);
282+
283+
//assert
284+
test.equals(currentPattern.extendedTemplate, "One" );
285+
162286
test.done();
163287
}
164288

0 commit comments

Comments
 (0)