Skip to content

Commit 0173a0a

Browse files
committed
Prefer exact key match over verbose and partial matching
Closes #226
1 parent 6bc2f6e commit 0173a0a

File tree

3 files changed

+43
-13
lines changed

3 files changed

+43
-13
lines changed

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ PL-node-v1.1.0
99
- ADD: Added fuzzy pattern matching support based on patternType-substring(patternName) to align with PL PHP
1010
- FIX: Fixed issue with gulpfile not copying style.css and watching the wrong directory
1111
- THX: Thanks @robinsonaaron for the issue and pull request!
12+
- FIX: Prefer exact pattern key match over fuzzy matches inside getpatternbykey()
13+
- THX: Thanks @EvanLovely for the suggestion
1214

1315
PL-node-v1.0.0
1416
- FIX: Resolve issue with not hiding underscored patterns.

builder/pattern_assembler.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
/*
2-
* patternlab-node - v1.0.1 - 2015
3-
*
1+
/*
2+
* patternlab-node - v1.0.1 - 2015
3+
*
44
* Brian Muenzenmeyer, and the web community.
5-
* Licensed under the MIT license.
6-
*
7-
* Many thanks to Brad Frost and Dave Olsen for inspiration, encouragement, and advice.
5+
* Licensed under the MIT license.
6+
*
7+
* Many thanks to Brad Frost and Dave Olsen for inspiration, encouragement, and advice.
88
*
99
*/
1010

@@ -248,18 +248,25 @@
248248
}
249249

250250
function getpatternbykey(key, patternlab){
251+
252+
//look for exact key matches
253+
for(var i = 0; i < patternlab.patterns.length; i++){
254+
if(patternlab.patterns[i].key === key){
255+
return patternlab.patterns[i];
256+
}
257+
}
258+
259+
//else look by verbose syntax
251260
for(var i = 0; i < patternlab.patterns.length; i++){
252261
switch(key){
253-
case patternlab.patterns[i].key:
254262
case patternlab.patterns[i].subdir + '/' + patternlab.patterns[i].fileName:
255263
case patternlab.patterns[i].subdir + '/' + patternlab.patterns[i].fileName + '.mustache':
256264
return patternlab.patterns[i];
257265
}
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
266+
}
267+
268+
//return the fuzzy match if all else fails
269+
for(var i = 0; i < patternlab.patterns.length; i++){
263270
var keyParts = key.split('-'),
264271
keyType = keyParts[0],
265272
keyName = keyParts.slice(1).join('-');
@@ -270,7 +277,6 @@
270277
throw 'Could not find pattern with key ' + key;
271278
}
272279

273-
274280
function mergeData(obj1, obj2){
275281
if(typeof obj2 === 'undefined'){
276282
obj2 = {};

test/pattern_assembler_tests.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,28 @@
697697
//assert
698698
test.equals(result, patternlab.patterns[0]);
699699
test.done();
700+
},
701+
'get_pattern_by_key - returns the exact key if found' : function(test){
702+
//arrange
703+
var pattern_assembler = new pa();
704+
var patternlab = {};
705+
patternlab.patterns = [];
706+
707+
patternlab.patterns.push({
708+
key: 'molecules-primary-nav-jagged',
709+
subdir: 'molecules',
710+
fileName: 'primary-nav-jagged'
711+
}, {
712+
key: 'molecules-primary-nav',
713+
subdir: 'molecules',
714+
fileName: 'molecules-primary-nav'
715+
});
716+
717+
//act
718+
var result = pattern_assembler.get_pattern_by_key('molecules-primary-nav', patternlab);
719+
//assert
720+
test.equals(result, patternlab.patterns[1]);
721+
test.done();
700722
}
701723
};
702724
}());

0 commit comments

Comments
 (0)