Skip to content

Commit 6ad11a6

Browse files
author
Brian Muenzenmeyer
committed
Merge pull request #219 from pattern-lab/fuzzy-patterns
Fuzzy pattern support
2 parents ea6c625 + 22bf232 commit 6ad11a6

File tree

7 files changed

+248
-45
lines changed

7 files changed

+248
-45
lines changed

CHANGELOG

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
THIS CHANGELOG IS AN ATTEMPT TO DOCUMENT CHANGES TO THIS PROJECT.
22

3-
PL-node-v1.0.1
4-
- FIX: Fix issue where partials containing styleModifiers with integers were not found correctly under all circumstances
5-
- 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.
3+
PL-node-v1.1.0
4+
- FIX: Fixed issue where partials containing styleModifiers with integers were not found correctly under all circumstances
5+
- 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.
66
- THX: Thanks @theorise for reporting these issues.
77
- THX: Thanks @dmolsen for input on desired behavior.
8-
- 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
8+
- 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
9+
- ADD: Added fuzzy pattern matching support based on patternType-substring(patternName) to align with PL PHP
910

1011
PL-node-v1.0.0
1112
- FIX: Resolve issue with not hiding underscored patterns.

builder/lineage_hunter.js

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,47 +23,41 @@
2323
if(matches !== null){
2424
matches.forEach(function(match, index, matches){
2525
//strip out the template cruft
26-
var foundPattern = match.replace("{{> ", "").replace(" }}", "").replace("{{>", "").replace("}}", "");
26+
var foundPatternKey = match.replace("{{> ", "").replace(" }}", "").replace("{{>", "").replace("}}", "");
2727

2828
// remove any potential pattern parameters. this and the above are rather brutish but I didn't want to do a regex at the time
29-
if(foundPattern.indexOf('(') > 0){
30-
foundPattern = foundPattern.substring(0, foundPattern.indexOf('('));
29+
if(foundPatternKey.indexOf('(') > 0){
30+
foundPatternKey = foundPatternKey.substring(0, foundPatternKey.indexOf('('));
3131
}
3232

33-
//add if it doesnt exist
34-
if (pattern.lineageIndex.indexOf(foundPattern) === -1){
33+
//remove any potential stylemodifiers.
34+
foundPatternKey = foundPatternKey.split(':')[0];
3535

36-
pattern.lineageIndex.push(foundPattern);
36+
//get the ancestorPattern
37+
var ancestorPattern = pattern_assembler.get_pattern_by_key(foundPatternKey, patternlab);
3738

38-
patternlab.patterns.forEach(function(ancestorPattern, index, patterns){
39+
if (ancestorPattern && pattern.lineageIndex.indexOf(ancestorPattern.key) === -1){
3940

40-
//find the pattern in question
41-
var searchPattern = ancestorPattern.patternGroup + "-" + ancestorPattern.patternName;
41+
//add it since it didnt exist
42+
pattern.lineageIndex.push(ancestorPattern.key);
43+
//create the more complex patternLineage object too
44+
var l = {
45+
"lineagePattern": ancestorPattern.key,
46+
"lineagePath": "../../patterns/" + ancestorPattern.patternLink
47+
};
48+
pattern.lineage.push(JSON.stringify(l));
4249

43-
if(searchPattern === foundPattern){
44-
//create the more complex patternLineage object too
45-
var l = {
46-
"lineagePattern": foundPattern,
47-
"lineagePath": "../../patterns/" + ancestorPattern.patternLink
50+
//also, add the lineageR entry if it doesn't exist
51+
if (ancestorPattern.lineageRIndex.indexOf(pattern.key) === -1){
52+
ancestorPattern.lineageRIndex.push(pattern.key);
53+
54+
//create the more complex patternLineage object in reverse
55+
var lr = {
56+
"lineagePattern": pattern.key,
57+
"lineagePath": "../../patterns/" + pattern.patternLink
4858
};
49-
pattern.lineage.push(JSON.stringify(l));
50-
51-
//also, add the lineageR entry if it doesn't exist
52-
var patternLabel = pattern.patternGroup + "-" + pattern.patternName;
53-
if (ancestorPattern.lineageRIndex.indexOf(patternLabel) === -1){
54-
ancestorPattern.lineageRIndex.push(patternLabel);
55-
56-
//create the more complex patternLineage object in reverse
57-
var lr = {
58-
"lineagePattern": patternLabel,
59-
"lineagePath": "../../patterns/" + pattern.patternLink
60-
};
61-
ancestorPattern.lineageR.push(JSON.stringify(lr));
62-
}
59+
ancestorPattern.lineageR.push(JSON.stringify(lr));
6360
}
64-
65-
});
66-
6761
}
6862
});
6963
}

builder/pattern_assembler.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,17 @@
255255
case patternlab.patterns[i].subdir + '/' + patternlab.patterns[i].fileName + '.mustache':
256256
return patternlab.patterns[i];
257257
}
258+
//look for exact key matches
259+
if(key === patternlab.patterns[i].key){
260+
return patternlab.patterns[i];
261+
}
262+
//return the fuzzy match within the type if it exists
263+
var keyParts = key.split('-'),
264+
keyType = keyParts[0],
265+
keyName = keyParts.slice(1).join('-');
266+
if(patternlab.patterns[i].key.split('-')[0] === keyType && patternlab.patterns[i].key.indexOf(keyName) > -1){
267+
return patternlab.patterns[i];
268+
}
258269
}
259270
throw 'Could not find pattern with key ' + key;
260271
}

test/lineage_hunter_tests.js

Lines changed: 177 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
var lh = require('../builder/lineage_hunter');
55

66
exports['lineage hunter '] = {
7-
'test lineage hunter finds lineage' : function(test){
7+
'find_lineage - finds lineage' : function(test){
88

99
//setup current pattern from what we would have during execution
1010
var currentPattern = {
@@ -19,6 +19,7 @@
1919
"patternGroup": "organisms",
2020
"patternSubGroup": "organisms\\00-global",
2121
"flatPatternPath": "02-organisms\\00-global",
22+
"key": "organisms-header",
2223
"patternState": "",
2324
"lineage": [],
2425
"lineageIndex": [],
@@ -39,6 +40,7 @@
3940
"patternGroup": "atoms",
4041
"patternSubGroup": "atoms\\03-images",
4142
"flatPatternPath": "00-atoms\\03-images",
43+
"key": "atoms-logo",
4244
"patternState": "",
4345
"lineage": [],
4446
"lineageIndex": [],
@@ -57,6 +59,7 @@
5759
"patternGroup": "molecules",
5860
"patternSubGroup": "molecules\\05-navigation",
5961
"flatPatternPath": "01-molecules\\05-navigation",
62+
"key": "molecules-primary-nav",
6063
"patternState": "",
6164
"lineage": [],
6265
"lineageIndex": [],
@@ -75,6 +78,7 @@
7578
"patternGroup": "molecules",
7679
"patternSubGroup": "molecules\\04-forms",
7780
"flatPatternPath": "01-molecules\\04-forms",
81+
"key": "molecules-search",
7882
"patternState": "",
7983
"lineage": [],
8084
"lineageIndex": [],
@@ -95,7 +99,7 @@
9599
test.done();
96100
},
97101

98-
'test lineage hunter finds lineage with spaced pattern parameters' : function(test){
102+
'find_lineage - finds lineage with spaced pattern parameters' : function(test){
99103
//setup current pattern from what we would have during execution
100104
var currentPattern = {
101105
"name": "01-molecules-01-toast-00-error",
@@ -109,6 +113,7 @@
109113
"patternGroup": "molecules",
110114
"patternSubGroup": "molecules\\01-toast",
111115
"flatPatternPath": "01-molecules\\01-toast",
116+
"key": "molecules-error",
112117
"patternState": "",
113118
"lineage": [],
114119
"lineageIndex": [],
@@ -129,6 +134,7 @@
129134
"patternGroup": "atoms",
130135
"patternSubGroup": "atoms\\05-alerts",
131136
"flatPatternPath": "01-atoms\\05-alerts",
137+
"key": "atoms-error",
132138
"patternState": "",
133139
"lineage": [],
134140
"lineageIndex": [],
@@ -147,7 +153,7 @@
147153
test.done();
148154
},
149155

150-
'test lineage hunter finds lineage with unspaced pattern parameters' : function(test){
156+
'find_lineage - finds lineage with unspaced pattern parameters' : function(test){
151157
//setup current pattern from what we would have during execution
152158
var currentPattern = {
153159
"name": "01-molecules-01-toast-00-error",
@@ -161,6 +167,7 @@
161167
"patternGroup": "molecules",
162168
"patternSubGroup": "molecules\\01-toast",
163169
"flatPatternPath": "01-molecules\\01-toast",
170+
"key": "molecules-error",
164171
"patternState": "",
165172
"lineage": [],
166173
"lineageIndex": [],
@@ -181,6 +188,7 @@
181188
"patternGroup": "atoms",
182189
"patternSubGroup": "atoms\\05-alerts",
183190
"flatPatternPath": "01-atoms\\05-alerts",
191+
"key": "atoms-error",
184192
"patternState": "",
185193
"lineage": [],
186194
"lineageIndex": [],
@@ -201,7 +209,169 @@
201209
test.done();
202210
},
203211

204-
'test lineage hunter does not apply lineage twice' : function(test){
212+
'find_lineage - finds lineage with spaced styleModifier' : function(test){
213+
//setup current pattern from what we would have during execution
214+
var currentPattern = {
215+
"name": "01-molecules-01-toast-00-error",
216+
"subdir": "01-molecules\\01-toast",
217+
"filename": "00-error.mustache",
218+
"data": null,
219+
"template": "{{> atoms-error:foo }}",
220+
"patternPartial": "{{> atoms-error:foo }}",
221+
"patternName": "error",
222+
"patternLink": "01-molecules-01-toast-00-error/01-molecules-01-toast-00-error.html",
223+
"patternGroup": "molecules",
224+
"patternSubGroup": "molecules\\01-toast",
225+
"flatPatternPath": "01-molecules\\01-toast",
226+
"key": "molecules-error",
227+
"patternState": "",
228+
"lineage": [],
229+
"lineageIndex": [],
230+
"lineageR": [],
231+
"lineageRIndex": []
232+
};
233+
var patternlab = {
234+
patterns: [
235+
{
236+
"name": "01-atoms-05-alerts-00-error",
237+
"subdir": "01-atoms\\05-alerts",
238+
"filename": "00-error.mustache",
239+
"data": null,
240+
"template": "<h1> {{message}} </h1>",
241+
"patternPartial": "<h1> {{message}} </h1>",
242+
"patternName": "error",
243+
"patternLink": "01-atoms-05-alerts-00-error/01-atoms-05-alerts-00-error.html",
244+
"patternGroup": "atoms",
245+
"patternSubGroup": "atoms\\05-alerts",
246+
"flatPatternPath": "01-atoms\\05-alerts",
247+
"key": "atoms-error",
248+
"patternState": "",
249+
"lineage": [],
250+
"lineageIndex": [],
251+
"lineageR": [],
252+
"lineageRIndex": []
253+
}
254+
]
255+
};
256+
257+
var lineage_hunter = new lh();
258+
lineage_hunter.find_lineage(currentPattern, patternlab);
259+
260+
test.equals(currentPattern.lineageIndex.length, 1);
261+
test.equals(currentPattern.lineageIndex[0], "atoms-error");
262+
263+
test.done();
264+
},
265+
266+
'find_lineage - finds lineage with unspaced styleModifier' : function(test){
267+
//setup current pattern from what we would have during execution
268+
var currentPattern = {
269+
"name": "01-molecules-01-toast-00-error",
270+
"subdir": "01-molecules\\01-toast",
271+
"filename": "00-error.mustache",
272+
"data": null,
273+
"template": "{{> atoms-error:foo }}",
274+
"patternPartial": "{{>atoms-error:foo}}",
275+
"patternName": "error",
276+
"patternLink": "01-molecules-01-toast-00-error/01-molecules-01-toast-00-error.html",
277+
"patternGroup": "molecules",
278+
"patternSubGroup": "molecules\\01-toast",
279+
"flatPatternPath": "01-molecules\\01-toast",
280+
"key": "molecules-error",
281+
"patternState": "",
282+
"lineage": [],
283+
"lineageIndex": [],
284+
"lineageR": [],
285+
"lineageRIndex": []
286+
};
287+
var patternlab = {
288+
patterns: [
289+
{
290+
"name": "01-atoms-05-alerts-00-error",
291+
"subdir": "01-atoms\\05-alerts",
292+
"filename": "00-error.mustache",
293+
"data": null,
294+
"template": "<h1> {{message}} </h1>",
295+
"patternPartial": "<h1> {{message}} </h1>",
296+
"patternName": "error",
297+
"patternLink": "01-atoms-05-alerts-00-error/01-atoms-05-alerts-00-error.html",
298+
"patternGroup": "atoms",
299+
"patternSubGroup": "atoms\\05-alerts",
300+
"flatPatternPath": "01-atoms\\05-alerts",
301+
"key": "atoms-error",
302+
"patternState": "",
303+
"lineage": [],
304+
"lineageIndex": [],
305+
"lineageR": [],
306+
"lineageRIndex": []
307+
}
308+
]
309+
};
310+
311+
var lineage_hunter = new lh();
312+
lineage_hunter.find_lineage(currentPattern, patternlab);
313+
314+
test.equals(currentPattern.lineageIndex.length, 1);
315+
test.equals(currentPattern.lineageIndex[0], "atoms-error");
316+
317+
test.done();
318+
},
319+
320+
'find_lineage - finds lineage with fuzzy partial with styleModifier' : function(test){
321+
//setup current pattern from what we would have during execution
322+
var currentPattern = {
323+
"name": "01-molecules-01-toast-00-error",
324+
"subdir": "01-molecules\\01-toast",
325+
"filename": "00-error.mustache",
326+
"data": null,
327+
"template": "{{> atoms-e:foo }}",
328+
"patternPartial": "{{>atoms-e:foo}}",
329+
"patternName": "error",
330+
"patternLink": "01-molecules-01-toast-00-error/01-molecules-01-toast-00-error.html",
331+
"patternGroup": "molecules",
332+
"patternSubGroup": "molecules\\01-toast",
333+
"flatPatternPath": "01-molecules\\01-toast",
334+
"key": "molecules-error",
335+
"patternState": "",
336+
"lineage": [],
337+
"lineageIndex": [],
338+
"lineageR": [],
339+
"lineageRIndex": []
340+
};
341+
var patternlab = {
342+
patterns: [
343+
{
344+
"name": "01-atoms-05-alerts-00-error",
345+
"subdir": "01-atoms\\05-alerts",
346+
"filename": "00-error.mustache",
347+
"data": null,
348+
"template": "<h1> {{message}} </h1>",
349+
"patternPartial": "<h1> {{message}} </h1>",
350+
"patternName": "error",
351+
"patternLink": "01-atoms-05-alerts-00-error/01-atoms-05-alerts-00-error.html",
352+
"patternGroup": "atoms",
353+
"patternSubGroup": "atoms\\05-alerts",
354+
"flatPatternPath": "01-atoms\\05-alerts",
355+
"key": "atoms-error",
356+
"patternState": "",
357+
"lineage": [],
358+
"lineageIndex": [],
359+
"lineageR": [],
360+
"lineageRIndex": []
361+
}
362+
]
363+
};
364+
365+
var lineage_hunter = new lh();
366+
lineage_hunter.find_lineage(currentPattern, patternlab);
367+
368+
test.equals(currentPattern.lineageIndex.length, 1);
369+
test.equals(currentPattern.lineageIndex[0], "atoms-error");
370+
371+
test.done();
372+
},
373+
374+
'find_lineage - does not apply lineage twice' : function(test){
205375
//setup current pattern from what we would have during execution
206376
var currentPattern = {
207377
"name": "01-molecules-01-toast-00-error",
@@ -215,6 +385,7 @@
215385
"patternGroup": "molecules",
216386
"patternSubGroup": "molecules\\01-toast",
217387
"flatPatternPath": "01-molecules\\01-toast",
388+
"key": "molecules-error",
218389
"patternState": "",
219390
"lineage": [],
220391
"lineageIndex": [],
@@ -235,6 +406,7 @@
235406
"patternGroup": "atoms",
236407
"patternSubGroup": "atoms\\05-alerts",
237408
"flatPatternPath": "01-atoms\\05-alerts",
409+
"key": "atoms-error",
238410
"patternState": "",
239411
"lineage": [],
240412
"lineageIndex": [],
@@ -254,8 +426,7 @@
254426
test.equals(JSON.parse(patternlab.patterns[0].lineageR).lineagePattern, 'molecules-error');
255427

256428
test.done();
257-
},
258-
429+
}
259430

260431
};
261432

0 commit comments

Comments
 (0)