From 980118fb639274e01e485f5f0c7ed148c9d5a080 Mon Sep 17 00:00:00 2001 From: BRIAN MUENZENMEYER Date: Tue, 29 Dec 2015 00:20:51 -0600 Subject: [PATCH 1/2] fuzzy partial matching. unit test coverage for some basic scenarios, lineage, and style modifiers. still need to look into list items also, may be a bug with repeated style modifiers, which i think is a regression. --- builder/lineage_hunter.js | 56 +++++----- builder/pattern_assembler.js | 11 ++ test/lineage_hunter_tests.js | 183 ++++++++++++++++++++++++++++++-- test/list_item_hunter_tests.js | 10 +- test/object_factory_tests.js | 6 ++ test/pattern_assembler_tests.js | 18 ++++ 6 files changed, 243 insertions(+), 41 deletions(-) diff --git a/builder/lineage_hunter.js b/builder/lineage_hunter.js index 62321d5cb..7d4c78242 100644 --- a/builder/lineage_hunter.js +++ b/builder/lineage_hunter.js @@ -23,47 +23,41 @@ if(matches !== null){ matches.forEach(function(match, index, matches){ //strip out the template cruft - var foundPattern = match.replace("{{> ", "").replace(" }}", "").replace("{{>", "").replace("}}", ""); + var foundPatternKey = match.replace("{{> ", "").replace(" }}", "").replace("{{>", "").replace("}}", ""); // remove any potential pattern parameters. this and the above are rather brutish but I didn't want to do a regex at the time - if(foundPattern.indexOf('(') > 0){ - foundPattern = foundPattern.substring(0, foundPattern.indexOf('(')); + if(foundPatternKey.indexOf('(') > 0){ + foundPatternKey = foundPatternKey.substring(0, foundPatternKey.indexOf('(')); } - //add if it doesnt exist - if (pattern.lineageIndex.indexOf(foundPattern) === -1){ + //remove any potential stylemodifiers. + foundPatternKey = foundPatternKey.split(':')[0]; - pattern.lineageIndex.push(foundPattern); + //get the ancestorPattern + var ancestorPattern = pattern_assembler.get_pattern_by_key(foundPatternKey, patternlab); - patternlab.patterns.forEach(function(ancestorPattern, index, patterns){ + if (ancestorPattern && pattern.lineageIndex.indexOf(ancestorPattern.key) === -1){ - //find the pattern in question - var searchPattern = ancestorPattern.patternGroup + "-" + ancestorPattern.patternName; + //add it since it didnt exist + pattern.lineageIndex.push(ancestorPattern.key); + //create the more complex patternLineage object too + var l = { + "lineagePattern": ancestorPattern.key, + "lineagePath": "../../patterns/" + ancestorPattern.patternLink + }; + pattern.lineage.push(JSON.stringify(l)); - if(searchPattern === foundPattern){ - //create the more complex patternLineage object too - var l = { - "lineagePattern": foundPattern, - "lineagePath": "../../patterns/" + ancestorPattern.patternLink + //also, add the lineageR entry if it doesn't exist + if (ancestorPattern.lineageRIndex.indexOf(pattern.key) === -1){ + ancestorPattern.lineageRIndex.push(pattern.key); + + //create the more complex patternLineage object in reverse + var lr = { + "lineagePattern": pattern.key, + "lineagePath": "../../patterns/" + pattern.patternLink }; - pattern.lineage.push(JSON.stringify(l)); - - //also, add the lineageR entry if it doesn't exist - var patternLabel = pattern.patternGroup + "-" + pattern.patternName; - if (ancestorPattern.lineageRIndex.indexOf(patternLabel) === -1){ - ancestorPattern.lineageRIndex.push(patternLabel); - - //create the more complex patternLineage object in reverse - var lr = { - "lineagePattern": patternLabel, - "lineagePath": "../../patterns/" + pattern.patternLink - }; - ancestorPattern.lineageR.push(JSON.stringify(lr)); - } + ancestorPattern.lineageR.push(JSON.stringify(lr)); } - - }); - } }); } diff --git a/builder/pattern_assembler.js b/builder/pattern_assembler.js index 608289495..2bbcd1830 100644 --- a/builder/pattern_assembler.js +++ b/builder/pattern_assembler.js @@ -255,6 +255,17 @@ case patternlab.patterns[i].subdir + '/' + patternlab.patterns[i].fileName + '.mustache': return patternlab.patterns[i]; } + //look for exact key matches + if(key === patternlab.patterns[i].key){ + return patternlab.patterns[i]; + } + //return the fuzzy match within the type if it exists + var keyParts = key.split('-'), + keyType = keyParts[0], + keyName = keyParts.slice(1).join('-'); + if(patternlab.patterns[i].key.split('-')[0] === keyType && patternlab.patterns[i].key.indexOf(keyName) > -1){ + return patternlab.patterns[i]; + } } throw 'Could not find pattern with key ' + key; } diff --git a/test/lineage_hunter_tests.js b/test/lineage_hunter_tests.js index d773f6d6e..0cd6a4aea 100644 --- a/test/lineage_hunter_tests.js +++ b/test/lineage_hunter_tests.js @@ -4,7 +4,7 @@ var lh = require('../builder/lineage_hunter'); exports['lineage hunter '] = { - 'test lineage hunter finds lineage' : function(test){ + 'find_lineage - finds lineage' : function(test){ //setup current pattern from what we would have during execution var currentPattern = { @@ -19,6 +19,7 @@ "patternGroup": "organisms", "patternSubGroup": "organisms\\00-global", "flatPatternPath": "02-organisms\\00-global", + "key": "organisms-header", "patternState": "", "lineage": [], "lineageIndex": [], @@ -39,6 +40,7 @@ "patternGroup": "atoms", "patternSubGroup": "atoms\\03-images", "flatPatternPath": "00-atoms\\03-images", + "key": "atoms-logo", "patternState": "", "lineage": [], "lineageIndex": [], @@ -57,6 +59,7 @@ "patternGroup": "molecules", "patternSubGroup": "molecules\\05-navigation", "flatPatternPath": "01-molecules\\05-navigation", + "key": "molecules-primary-nav", "patternState": "", "lineage": [], "lineageIndex": [], @@ -75,6 +78,7 @@ "patternGroup": "molecules", "patternSubGroup": "molecules\\04-forms", "flatPatternPath": "01-molecules\\04-forms", + "key": "molecules-search", "patternState": "", "lineage": [], "lineageIndex": [], @@ -95,7 +99,7 @@ test.done(); }, - 'test lineage hunter finds lineage with spaced pattern parameters' : function(test){ + 'find_lineage - finds lineage with spaced pattern parameters' : function(test){ //setup current pattern from what we would have during execution var currentPattern = { "name": "01-molecules-01-toast-00-error", @@ -109,6 +113,7 @@ "patternGroup": "molecules", "patternSubGroup": "molecules\\01-toast", "flatPatternPath": "01-molecules\\01-toast", + "key": "molecules-error", "patternState": "", "lineage": [], "lineageIndex": [], @@ -129,6 +134,7 @@ "patternGroup": "atoms", "patternSubGroup": "atoms\\05-alerts", "flatPatternPath": "01-atoms\\05-alerts", + "key": "atoms-error", "patternState": "", "lineage": [], "lineageIndex": [], @@ -147,7 +153,7 @@ test.done(); }, - 'test lineage hunter finds lineage with unspaced pattern parameters' : function(test){ + 'find_lineage - finds lineage with unspaced pattern parameters' : function(test){ //setup current pattern from what we would have during execution var currentPattern = { "name": "01-molecules-01-toast-00-error", @@ -161,6 +167,7 @@ "patternGroup": "molecules", "patternSubGroup": "molecules\\01-toast", "flatPatternPath": "01-molecules\\01-toast", + "key": "molecules-error", "patternState": "", "lineage": [], "lineageIndex": [], @@ -181,6 +188,7 @@ "patternGroup": "atoms", "patternSubGroup": "atoms\\05-alerts", "flatPatternPath": "01-atoms\\05-alerts", + "key": "atoms-error", "patternState": "", "lineage": [], "lineageIndex": [], @@ -201,7 +209,169 @@ test.done(); }, - 'test lineage hunter does not apply lineage twice' : function(test){ + 'find_lineage - finds lineage with spaced styleModifier' : function(test){ + //setup current pattern from what we would have during execution + var currentPattern = { + "name": "01-molecules-01-toast-00-error", + "subdir": "01-molecules\\01-toast", + "filename": "00-error.mustache", + "data": null, + "template": "{{> atoms-error:foo }}", + "patternPartial": "{{> atoms-error:foo }}", + "patternName": "error", + "patternLink": "01-molecules-01-toast-00-error/01-molecules-01-toast-00-error.html", + "patternGroup": "molecules", + "patternSubGroup": "molecules\\01-toast", + "flatPatternPath": "01-molecules\\01-toast", + "key": "molecules-error", + "patternState": "", + "lineage": [], + "lineageIndex": [], + "lineageR": [], + "lineageRIndex": [] + }; + var patternlab = { + patterns: [ + { + "name": "01-atoms-05-alerts-00-error", + "subdir": "01-atoms\\05-alerts", + "filename": "00-error.mustache", + "data": null, + "template": "

{{message}}

", + "patternPartial": "

{{message}}

", + "patternName": "error", + "patternLink": "01-atoms-05-alerts-00-error/01-atoms-05-alerts-00-error.html", + "patternGroup": "atoms", + "patternSubGroup": "atoms\\05-alerts", + "flatPatternPath": "01-atoms\\05-alerts", + "key": "atoms-error", + "patternState": "", + "lineage": [], + "lineageIndex": [], + "lineageR": [], + "lineageRIndex": [] + } + ] + }; + + var lineage_hunter = new lh(); + lineage_hunter.find_lineage(currentPattern, patternlab); + + test.equals(currentPattern.lineageIndex.length, 1); + test.equals(currentPattern.lineageIndex[0], "atoms-error"); + + test.done(); + }, + + 'find_lineage - finds lineage with unspaced styleModifier' : function(test){ + //setup current pattern from what we would have during execution + var currentPattern = { + "name": "01-molecules-01-toast-00-error", + "subdir": "01-molecules\\01-toast", + "filename": "00-error.mustache", + "data": null, + "template": "{{> atoms-error:foo }}", + "patternPartial": "{{>atoms-error:foo}}", + "patternName": "error", + "patternLink": "01-molecules-01-toast-00-error/01-molecules-01-toast-00-error.html", + "patternGroup": "molecules", + "patternSubGroup": "molecules\\01-toast", + "flatPatternPath": "01-molecules\\01-toast", + "key": "molecules-error", + "patternState": "", + "lineage": [], + "lineageIndex": [], + "lineageR": [], + "lineageRIndex": [] + }; + var patternlab = { + patterns: [ + { + "name": "01-atoms-05-alerts-00-error", + "subdir": "01-atoms\\05-alerts", + "filename": "00-error.mustache", + "data": null, + "template": "

{{message}}

", + "patternPartial": "

{{message}}

", + "patternName": "error", + "patternLink": "01-atoms-05-alerts-00-error/01-atoms-05-alerts-00-error.html", + "patternGroup": "atoms", + "patternSubGroup": "atoms\\05-alerts", + "flatPatternPath": "01-atoms\\05-alerts", + "key": "atoms-error", + "patternState": "", + "lineage": [], + "lineageIndex": [], + "lineageR": [], + "lineageRIndex": [] + } + ] + }; + + var lineage_hunter = new lh(); + lineage_hunter.find_lineage(currentPattern, patternlab); + + test.equals(currentPattern.lineageIndex.length, 1); + test.equals(currentPattern.lineageIndex[0], "atoms-error"); + + test.done(); + }, + + 'find_lineage - finds lineage with fuzzy partial with styleModifier' : function(test){ + //setup current pattern from what we would have during execution + var currentPattern = { + "name": "01-molecules-01-toast-00-error", + "subdir": "01-molecules\\01-toast", + "filename": "00-error.mustache", + "data": null, + "template": "{{> atoms-e:foo }}", + "patternPartial": "{{>atoms-e:foo}}", + "patternName": "error", + "patternLink": "01-molecules-01-toast-00-error/01-molecules-01-toast-00-error.html", + "patternGroup": "molecules", + "patternSubGroup": "molecules\\01-toast", + "flatPatternPath": "01-molecules\\01-toast", + "key": "molecules-error", + "patternState": "", + "lineage": [], + "lineageIndex": [], + "lineageR": [], + "lineageRIndex": [] + }; + var patternlab = { + patterns: [ + { + "name": "01-atoms-05-alerts-00-error", + "subdir": "01-atoms\\05-alerts", + "filename": "00-error.mustache", + "data": null, + "template": "

{{message}}

", + "patternPartial": "

{{message}}

", + "patternName": "error", + "patternLink": "01-atoms-05-alerts-00-error/01-atoms-05-alerts-00-error.html", + "patternGroup": "atoms", + "patternSubGroup": "atoms\\05-alerts", + "flatPatternPath": "01-atoms\\05-alerts", + "key": "atoms-error", + "patternState": "", + "lineage": [], + "lineageIndex": [], + "lineageR": [], + "lineageRIndex": [] + } + ] + }; + + var lineage_hunter = new lh(); + lineage_hunter.find_lineage(currentPattern, patternlab); + + test.equals(currentPattern.lineageIndex.length, 1); + test.equals(currentPattern.lineageIndex[0], "atoms-error"); + + test.done(); + }, + + 'find_lineage - does not apply lineage twice' : function(test){ //setup current pattern from what we would have during execution var currentPattern = { "name": "01-molecules-01-toast-00-error", @@ -215,6 +385,7 @@ "patternGroup": "molecules", "patternSubGroup": "molecules\\01-toast", "flatPatternPath": "01-molecules\\01-toast", + "key": "molecules-error", "patternState": "", "lineage": [], "lineageIndex": [], @@ -235,6 +406,7 @@ "patternGroup": "atoms", "patternSubGroup": "atoms\\05-alerts", "flatPatternPath": "01-atoms\\05-alerts", + "key": "atoms-error", "patternState": "", "lineage": [], "lineageIndex": [], @@ -254,8 +426,7 @@ test.equals(JSON.parse(patternlab.patterns[0].lineageR).lineagePattern, 'molecules-error'); test.done(); - }, - + } }; diff --git a/test/list_item_hunter_tests.js b/test/list_item_hunter_tests.js index d2de41468..3063ceffe 100644 --- a/test/list_item_hunter_tests.js +++ b/test/list_item_hunter_tests.js @@ -148,14 +148,14 @@ var pattern1 = { "template": "{{#listItems.one}}{{> 00-test/00-foo }}{{/listItems.one}}", "extendedTemplate" : "{{#listItems.one}}{{> 00-test/00-foo }}{{/listItems.one}}", - "key": "test-patternName1", + "key": "test-foo", "jsonFileData" : {} }; var pattern2 = { "template": "{{#listItems.two}}{{> 00-test/01-bar.mustache }}{{/listItems.two}}", "extendedTemplate" : "{{#listItems.two}}{{> 00-test/01-bar.mustache }}{{/listItems.two}}", - "key": "test-patternName2", + "key": "test-bar", "jsonFileData" : {} }; @@ -186,14 +186,16 @@ "extendedTemplate" : "{{ title }}", "subdir": "00-test", "fileName": "00-foo", - "jsonFileData" : {} + "jsonFileData" : {}, + "key": "test-foo", }, { "template": "{{ title }}", "extendedTemplate" : "{{ title }}", "subdir": "00-test", "fileName": "01-bar", - "jsonFileData" : {} + "jsonFileData" : {}, + "key": "test-bar", } ] }; diff --git a/test/object_factory_tests.js b/test/object_factory_tests.js index 6ce2693a1..f58b2e664 100644 --- a/test/object_factory_tests.js +++ b/test/object_factory_tests.js @@ -31,6 +31,12 @@ test.equals(p.patternName, 'colors-alt'); test.equals(p.patternDisplayName, 'Colors Alt'); test.done(); + }, + 'test oPattern removes pattern paramter from key correctly' : function(test){ + var p = new of.oPattern('source/_patterns/00-atoms/00-global/00-colors-alt.mustache', '00-atoms/00-global', '00-colors-alt.mustache', { d: 123}); + test.equals(p.patternName, 'colors-alt'); + test.equals(p.patternDisplayName, 'Colors Alt'); + test.done(); } }; diff --git a/test/pattern_assembler_tests.js b/test/pattern_assembler_tests.js index f623e754b..a786dd3e0 100644 --- a/test/pattern_assembler_tests.js +++ b/test/pattern_assembler_tests.js @@ -646,6 +646,24 @@ test.equals(pattern.jsonFileData.dave.url, "https://twitter.com/dmolsen"); test.equals(pattern.jsonFileData.brian.url, "https://twitter.com/bmuenzenmeyer"); test.done(); + }, + 'get_pattern_by_key - returns the fuzzy result when no others found' : function(test){ + //arrange + var pattern_assembler = new pa(); + var patternlab = {}; + patternlab.patterns = []; + + patternlab.patterns.push({ + key: 'character-han-solo', + subdir: 'character', + fileName: 'han-solo' + }); + + //act + var result = pattern_assembler.get_pattern_by_key('character-han', patternlab); + //assert + test.equals(result, patternlab.patterns[0]); + test.done(); } }; }()); From 22bf2324d28d0c9449cfc46949e2abf3ad085072 Mon Sep 17 00:00:00 2001 From: BRIAN MUENZENMEYER Date: Tue, 29 Dec 2015 23:20:05 -0600 Subject: [PATCH 2/2] updated changelog to reference fuzzy pattern support closes #202 --- CHANGELOG | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index b17f7fbdb..66d1bd4ec 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,11 +1,12 @@ THIS CHANGELOG IS AN ATTEMPT TO DOCUMENT CHANGES TO THIS PROJECT. -PL-node-v1.0.1 -- FIX: Fix issue where partials containing styleModifiers with integers were not found correctly under all circumstances -- FIX: Fix issue where excluded patterns were still rendered on the Pattern Lab site. Now they do not directly get rendered via the menu, view all links, or the styleguide, but are accessible for inclusion as pattern partials, and can be accessed via lineage. +PL-node-v1.1.0 +- FIX: Fixed issue where partials containing styleModifiers with integers were not found correctly under all circumstances +- FIX: Fixed issue where excluded patterns were still rendered on the Pattern Lab site. Now they do not directly get rendered via the menu, view all links, or the styleguide, but are accessible for inclusion as pattern partials, and can be accessed via lineage. - THX: Thanks @theorise for reporting these issues. - THX: Thanks @dmolsen for input on desired behavior. -- FIX: Fix issue where style modifier partials within list item blocks where not uniquely being applied. this seems like a regression. added a unit test with fix +- FIX: Fixed issue where style modifier partials within list item blocks where not uniquely being applied. this seems like a regression. added a unit test with fix +- ADD: Added fuzzy pattern matching support based on patternType-substring(patternName) to align with PL PHP PL-node-v1.0.0 - FIX: Resolve issue with not hiding underscored patterns.