Skip to content
This repository was archived by the owner on Dec 10, 2019. It is now read-only.

Commit 5517416

Browse files
committed
refactored to better utilize the pattern object constructor. less locals in patternlab.js
closes #81
1 parent 4dee74d commit 5517416

File tree

3 files changed

+30
-35
lines changed

3 files changed

+30
-35
lines changed

builder/object_factory.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@
1111
(function () {
1212
"use strict";
1313

14-
var oPattern = function(name, subdir, filename, data){
15-
this.name = name; //this is the unique name with the subDir
14+
var oPattern = function(subdir, filename, data){
15+
this.fileName = filename.substring(0, filename.indexOf('.'));
1616
this.subdir = subdir;
17-
this.filename = filename;
18-
this.data = data;
19-
this.template = '';
20-
this.patternPartial = '';
21-
this.patternName = ''; //this is the display name for the ui
22-
this.patternLink = name + '/' + name + '.html';
23-
this.patternGroup = name.substring(name.indexOf('-') + 1, name.indexOf('-', 4) + 1 - name.indexOf('-') + 1);
17+
this.name = (subdir.replace(/[\/\\]/g, '-') + '-' + this.fileName).replace(/\\/g, '-'); //this is the unique name with the subDir
18+
this.data = data || null;
19+
this.patternName = this.fileName.substring(this.fileName.indexOf('-') + 1); //this is the display name for the ui
20+
this.patternLink = this.name + '/' + this.name + '.html';
21+
this.patternGroup = this.name.substring(this.name.indexOf('-') + 1, this.name.indexOf('-', 4) + 1 - this.name.indexOf('-') + 1);
2422
this.patternSubGroup = subdir.substring(subdir.indexOf('/') + 4);
2523
this.flatPatternPath = subdir.replace(/\//g, '-');
26-
this.key = '';
24+
this.key = this.patternGroup + '-' + this.patternName;
25+
this.template = '';
26+
this.patternPartial = '';
2727
this.lineage = [];
2828
this.lineageIndex = [];
2929
this.lineageR = [];

builder/patternlab.js

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ var patternlab_engine = function(){
5858
patternlab.header = fs.readFileSync('./source/_patternlab-files/pattern-header-footer/header.html', 'utf8');
5959
patternlab.footer = fs.readFileSync('./source/_patternlab-files/pattern-header-footer/footer.html', 'utf8');
6060
patternlab.patterns = [];
61-
patternlab.patternIndex = [];
6261
patternlab.partials = {};
6362
patternlab.data.link = {};
6463

@@ -75,25 +74,15 @@ var patternlab_engine = function(){
7574
var subdir = path.dirname(path.relative('./source/_patterns', file));
7675
var filename = path.basename(file);
7776

78-
//check if the pattern already exists.
79-
var patternName = filename.substring(0, filename.indexOf('.')),
80-
patternIndex = patternlab.patternIndex.indexOf(subdir + '-' + patternName),
81-
currentPattern,
82-
flatPatternPath;
83-
84-
//ignore _underscored patterns, json, and dotfiles
77+
//ignore _underscored patterns, json (for now), and dotfiles
8578
if(filename.charAt(0) === '_' || path.extname(filename) === '.json' || filename.charAt(0) === '.'){
8679
return;
8780
}
8881

82+
//check for pattern parameters before we do much else. need to remove them into a data object so the rest of the filename parsing works
83+
8984
//make a new Pattern Object
90-
var flatPatternName = subdir.replace(/[\/\\]/g, '-') + '-' + patternName;
91-
92-
flatPatternName = flatPatternName.replace(/\\/g, '-');
93-
currentPattern = new of.oPattern(flatPatternName, subdir, filename, {});
94-
currentPattern.patternName = patternName.substring(patternName.indexOf('-') + 1);
95-
currentPattern.data = null;
96-
currentPattern.key = currentPattern.patternGroup + '-' + currentPattern.patternName;
85+
currentPattern = new of.oPattern(subdir, filename, {});
9786

9887
//see if this file has a state
9988
if(patternlab.config.patternStates[currentPattern.patternName]){
@@ -111,6 +100,8 @@ var patternlab_engine = function(){
111100

112101
}
113102
currentPattern.template = fs.readFileSync(abspath, 'utf8');
103+
104+
//look for a pseudo pattern by checking if there is a file containing same name, with ~ in it, ending in .json
114105

115106
//find pattern lineage
116107
var lineage_hunter = new lh();
@@ -125,14 +116,13 @@ var patternlab_engine = function(){
125116
//look for the full path on nested patters, else expect it to be flat
126117
var partialname = '';
127118
if(cleanSub !== ''){
128-
partialname = cleanSub + '-' + patternName.substring(patternName.indexOf('-') + 1);
119+
partialname = cleanSub + '-' + currentPattern.patternName;
129120
} else{
130-
partialname = currentPattern.patternGroup + '-' + patternName.substring(patternName.indexOf('-') + 1);
121+
partialname = currentPattern.patternGroup + '-' + currentPattern.patternName;
131122
}
132123
patternlab.partials[partialname] = currentPattern.template;
133124

134125
//add to patternlab object so we can look these up later.
135-
patternlab.patternIndex.push(currentPattern.name);
136126
patternlab.data.link[currentPattern.patternGroup + '-' + currentPattern.patternName] = '/patterns/' + currentPattern.patternLink;
137127
patternlab.patterns.push(currentPattern);
138128
});
@@ -145,7 +135,7 @@ var patternlab_engine = function(){
145135
//render the pattern. pass partials and data
146136
if(pattern.data) { // Pass found pattern-specific JSON as data
147137

148-
//extend patternIndex into link for pattern link shortcuts to work. we do this locally and globally
138+
//extend pattern data links into link for pattern link shortcuts to work. we do this locally and globally
149139
pattern.data.link = extend({}, patternlab.data.link);
150140

151141
pattern.patternPartial = renderPattern(pattern.template, pattern.data, patternlab.partials);
@@ -356,7 +346,7 @@ var patternlab_engine = function(){
356346

357347
function addToPatternPaths(bucketName, pattern){
358348
//this is messy, could use a refactor.
359-
patternlab.patternPaths[bucketName][pattern.patternName] = pattern.subdir.replace(/\\/g, '/') + "/" + pattern.filename.substring(0, pattern.filename.indexOf('.'));
349+
patternlab.patternPaths[bucketName][pattern.patternName] = pattern.subdir.replace(/\\/g, '/') + "/" + pattern.fileName;
360350
}
361351

362352
return {

test/object_factory_tests.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,23 @@
55

66
exports['oPattern initialization'] = {
77
'test oPattern initializes correctly' : function(test){
8-
var p = new of.oPattern('00-atoms-00-global-00-colors', '00-atoms/00-global', 'file.txt', { d: 123});
8+
var p = new of.oPattern('00-atoms/00-global', '00-colors.mustache', { d: 123});
99
test.equals(p.name, '00-atoms-00-global-00-colors');
1010
test.equals(p.subdir, '00-atoms/00-global');
11-
test.equals(p.filename, 'file.txt');
11+
test.equals(p.fileName, '00-colors');
1212
test.equals(p.data.d, 123);
13-
test.equals(p.template, '');
14-
test.equals(p.patternPartial, '');
15-
test.equals(p.patternName, '');
13+
test.equals(p.patternName, 'colors');
1614
test.equals(p.patternLink, '00-atoms-00-global-00-colors/00-atoms-00-global-00-colors.html');
1715
test.equals(p.patternGroup, 'atoms');
1816
test.equals(p.patternSubGroup, 'global');
1917
test.equals(p.flatPatternPath, '00-atoms-00-global');
18+
test.equals(p.key, 'atoms-colors');
19+
test.equals(p.template, '');
20+
test.equals(p.patternPartial, '');
21+
test.equals(p.lineage.length, 0);
22+
test.equals(p.lineageIndex.length, 0);
23+
test.equals(p.lineageR.length, 0);
24+
test.equals(p.lineageRIndex.length, 0);
2025
test.done();
2126
}
2227
};

0 commit comments

Comments
 (0)