From ca59e835c4b2f386f48d69146b6b15bef5d78f7b Mon Sep 17 00:00:00 2001 From: e2tha-e Date: Sun, 20 Sep 2015 10:41:47 -0400 Subject: [PATCH 1/5] updating mergeData() so obj2 has priority --- builder/pattern_assembler.js | 38 ++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/builder/pattern_assembler.js b/builder/pattern_assembler.js index 65e80eb5b..7e3f4bf36 100644 --- a/builder/pattern_assembler.js +++ b/builder/pattern_assembler.js @@ -167,24 +167,38 @@ throw 'Could not find pattern with key ' + key; } - - var self = this; - function mergeData(obj1, obj2) { - for (var p in obj2) { + /** + * Recursively merge properties of two objects. + * + * @param {object} obj1 If obj1 has properties obj2 doesn't, add to obj2. + * @param {object} obj2 This object's properties have priority over obj1. + */ + function mergeData(obj1, obj2){ + if(typeof obj2 === 'undefined'){ + obj2 = {}; + } + for(var p in obj1){ try { - // Property in destination object set; update its value. - if ( obj2[p].constructor == Object ) { - obj1[p] = self.merge_data(obj1[p], obj2[p]); - - } else { - obj1[p] = obj2[p]; + // Only recurse if obj1[p] is an object. + if(obj1[p].constructor === Object){ + // Requires 2 objects as params; create obj2[p] if undefined. + if(typeof obj2[p] === 'undefined'){ + obj2[p] = {}; + } + obj2[p] = mergeData(obj1[p], obj2[p]); + // Pop when recursion meets a non-object. If obj1[p] is a non-object, + // only copy to undefined obj2[p]. This way, obj2 maintains priority. + } else if(typeof obj2[p] === 'undefined'){ + obj2[p] = obj1[p]; } } catch(e) { // Property in destination object not set; create it and set its value. - obj1[p] = obj2[p]; + if(typeof obj2[p] === 'undefined'){ + obj2[p] = obj1[p]; + } } } - return obj1; + return obj2; } function buildListItems(patternlab){ From 22c67d90f1f42d02f09bcb80461935a78a6cc69d Mon Sep 17 00:00:00 2001 From: e2tha-e Date: Sun, 20 Sep 2015 10:44:06 -0400 Subject: [PATCH 2/5] reversing order of params in pseudopattern_hunter.js for variantFileData --- builder/pseudopattern_hunter.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builder/pseudopattern_hunter.js b/builder/pseudopattern_hunter.js index 9f0a33a4e..2ec038bb6 100644 --- a/builder/pseudopattern_hunter.js +++ b/builder/pseudopattern_hunter.js @@ -45,7 +45,7 @@ var variantFileData = fs.readJSONSync('source/_patterns/' + pseudoPatterns[i]); //extend any existing data with variant data - variantFileData = pattern_assembler.merge_data(variantFileData, currentPattern.jsonFileData); + variantFileData = pattern_assembler.merge_data(currentPattern.jsonFileData, variantFileData); var variantName = pseudoPatterns[i].substring(pseudoPatterns[i].indexOf('~') + 1).split('.')[0]; var patternVariant = new of.oPattern(currentPattern.subdir, currentPattern.fileName + '-' + variantName + '.mustache', variantFileData); From e5f937db5cb2a34c5531efa01f3124c9eae3687d Mon Sep 17 00:00:00 2001 From: e2tha-e Date: Sun, 20 Sep 2015 10:56:30 -0400 Subject: [PATCH 3/5] unit tests --- test/list_item_hunter_tests.js | 128 ++++++++++++++++++++++++++++++++- 1 file changed, 126 insertions(+), 2 deletions(-) diff --git a/test/list_item_hunter_tests.js b/test/list_item_hunter_tests.js index 0da3c9288..b6acc101c 100644 --- a/test/list_item_hunter_tests.js +++ b/test/list_item_hunter_tests.js @@ -100,7 +100,7 @@ test.done(); }, - 'process_list_item_partials overwrites listItem data if local .listitem.json is found' : function(test){ + 'process_list_item_partials overwrites listItem property if that property is in local .listitem.json' : function(test){ //arrange //setup current pattern from what we would have during execution var currentPattern = { @@ -108,6 +108,68 @@ "extendedTemplate" : "{{#listItems.two}}{{> test-simple }}{{/listItems.two}}", "key": "test-patternName", "jsonFileData" : {}, + "patternSpecificListJson" : { + "2": [ + { + "title": "One" + }, + { + "title": "Two" + }, + ] + } + }; + + var patternlab = { + "listitems": { + "1": [ + { + "title": "Foo" + } + ], + "2": [ + { + "title": "Foo" + }, + { + "title": "Bar" + } + ] + }, + "data": { + "link": {}, + "partials": [] + }, + "config": {"debug": false}, + "patterns": [ + { + "template": "{{ title }}", + "extendedTemplate" : "{{ title }}", + "key": "test-simple", + "jsonFileData" : {} + } + ] + }; + + var list_item_hunter = new lih(); + + //act + list_item_hunter.process_list_item_partials(currentPattern, patternlab); + + //assert + test.equals(currentPattern.extendedTemplate, "OneTwo" ); + + test.done(); + }, + + 'process_list_item_partials keeps listItem property if that property is not in local .listitem.json' : function(test){ + //arrange + //setup current pattern from what we would have during execution + var currentPattern = { + "template": "{{#listItems.one}}{{ title }}{{/listItems.one}}", + "extendedTemplate" : "{{#listItems.one}}{{> test-simple }}{{/listItems.one}}", + "key": "test-patternName", + "jsonFileData" : {}, "patternSpecificListJson" : { "2": [ { @@ -157,7 +219,69 @@ list_item_hunter.process_list_item_partials(currentPattern, patternlab); //assert - test.equals(currentPattern.extendedTemplate, "OneTwo" ); + test.equals(currentPattern.extendedTemplate, "Foo" ); + + test.done(); + }, + + 'process_list_item_partials uses local listItem property if that property is not set globally' : function(test){ + //arrange + //setup current pattern from what we would have during execution + var currentPattern = { + "template": "{{#listItems.one}}{{ title }}{{/listItems.one}}", + "extendedTemplate" : "{{#listItems.one}}{{> test-simple }}{{/listItems.one}}", + "key": "test-patternName", + "jsonFileData" : {}, + "patternSpecificListJson" : { + "1": [ + { + "title": "One" + } + ], + "2": [ + { + "title": "One" + }, + { + "title": "Two" + }, + ] + } + }; + + var patternlab = { + "listitems": { + "2": [ + { + "title": "Foo" + }, + { + "title": "Bar" + } + ] + }, + "data": { + "link": {}, + "partials": [] + }, + "config": {"debug": false}, + "patterns": [ + { + "template": "{{ title }}", + "extendedTemplate" : "{{ title }}", + "key": "test-simple", + "jsonFileData" : {} + } + ] + }; + + var list_item_hunter = new lih(); + + //act + list_item_hunter.process_list_item_partials(currentPattern, patternlab); + + //assert + test.equals(currentPattern.extendedTemplate, "One" ); test.done(); } From 986f2eade6b82a89975844c5a21411d0959b9bb2 Mon Sep 17 00:00:00 2001 From: e2tha-e Date: Sun, 20 Sep 2015 10:58:52 -0400 Subject: [PATCH 4/5] docblock --- builder/pattern_assembler.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/builder/pattern_assembler.js b/builder/pattern_assembler.js index 7e3f4bf36..791f4fe06 100644 --- a/builder/pattern_assembler.js +++ b/builder/pattern_assembler.js @@ -170,8 +170,9 @@ /** * Recursively merge properties of two objects. * - * @param {object} obj1 If obj1 has properties obj2 doesn't, add to obj2. - * @param {object} obj2 This object's properties have priority over obj1. + * @param {Object} obj1 If obj1 has properties obj2 doesn't, add to obj2. + * @param {Object} obj2 This object's properties have priority over obj1. + * @returns {Object} obj2 */ function mergeData(obj1, obj2){ if(typeof obj2 === 'undefined'){ From aab79d79bb0416eff09a081da6f270bd1c9470e4 Mon Sep 17 00:00:00 2001 From: e2tha-e Date: Sun, 20 Sep 2015 11:10:56 -0400 Subject: [PATCH 5/5] deleting whitespace --- test/list_item_hunter_tests.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/list_item_hunter_tests.js b/test/list_item_hunter_tests.js index b6acc101c..7f41cee88 100644 --- a/test/list_item_hunter_tests.js +++ b/test/list_item_hunter_tests.js @@ -179,7 +179,7 @@ "title": "Two" }, ] - } + } }; var patternlab = { @@ -217,7 +217,7 @@ //act list_item_hunter.process_list_item_partials(currentPattern, patternlab); - + //assert test.equals(currentPattern.extendedTemplate, "Foo" ); @@ -246,7 +246,7 @@ "title": "Two" }, ] - } + } }; var patternlab = { @@ -279,7 +279,7 @@ //act list_item_hunter.process_list_item_partials(currentPattern, patternlab); - + //assert test.equals(currentPattern.extendedTemplate, "One" );