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

Commit d7a531e

Browse files
committed
factor utility functions out of the pattern assembler and put 'em in our
utilities module; move isPatternFile into pattern_engines module since that's where the knowledge it needs is concentrated
1 parent 96c88c0 commit d7a531e

File tree

9 files changed

+109
-108
lines changed

9 files changed

+109
-108
lines changed

builder/list_item_hunter.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
var extend = require('util')._extend,
1717
pa = require('./pattern_assembler'),
1818
smh = require('./style_modifier_hunter'),
19+
plutils = require('./utilities'),
1920
config = require('../config.json'),
2021
of = require('./object_factory');
2122

@@ -48,7 +49,7 @@
4849

4950
//check for a local listitems.json file
5051
var listData = JSON.parse(JSON.stringify(patternlab.listitems));
51-
listData = pattern_assembler.merge_data(listData, pattern.listitems);
52+
listData = plutils.mergeData(listData, pattern.listitems);
5253

5354
//iterate over each copied block, rendering its contents along with pattenlab.listitems[i]
5455
for(var i = 0; i < repeatedBlockTemplate.length; i++){
@@ -61,8 +62,8 @@
6162
var globalData = JSON.parse(JSON.stringify(patternlab.data));
6263
var localData = JSON.parse(JSON.stringify(pattern.jsonFileData));
6364

64-
var allData = pattern_assembler.merge_data(globalData, localData);
65-
allData = pattern_assembler.merge_data(allData, itemData != undefined ? itemData[i] : {}); //itemData could be undefined if the listblock contains no partial, just markup
65+
var allData = plutils.mergeData(globalData, localData);
66+
allData = plutils.mergeData(allData, itemData != undefined ? itemData[i] : {}); //itemData could be undefined if the listblock contains no partial, just markup
6667
allData.link = extend({}, patternlab.data.link);
6768

6869
//check for partials within the repeated block

builder/parameter_hunter.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
var extend = require('util')._extend,
1717
pa = require('./pattern_assembler'),
1818
smh = require('./style_modifier_hunter'),
19+
plutils = require('./utilities'),
1920
style_modifier_hunter = new smh(),
2021
pattern_assembler = new pa();
2122

@@ -43,8 +44,8 @@
4344
var globalData = JSON.parse(JSON.stringify(patternlab.data));
4445
var localData = JSON.parse(JSON.stringify(pattern.jsonFileData || {}));
4546

46-
var allData = pattern_assembler.merge_data(globalData, localData);
47-
allData = pattern_assembler.merge_data(allData, paramData);
47+
var allData = plutils.mergeData(globalData, localData);
48+
allData = plutils.mergeData(allData, paramData);
4849

4950
//if partial has style modifier data, replace the styleModifier value
5051
if(pattern.stylePartials && pattern.stylePartials.length > 0){

builder/pattern_assembler.js

Lines changed: 6 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,10 @@
1515

1616
var fs = require('fs-extra'),
1717
of = require('./object_factory'),
18-
path = require('path'),
18+
plutils = require('./utilities'),
1919
patternEngines = require('./pattern_engines/pattern_engines'),
2020
config = fs.readJSONSync('./config.json');
2121

22-
function isObjectEmpty(obj) {
23-
for(var prop in obj) {
24-
if(obj.hasOwnProperty(prop))
25-
return false;
26-
}
27-
28-
return true;
29-
}
30-
3122
function setState(pattern, patternlab){
3223
if(patternlab.config.patternStates[pattern.patternName]){
3324
pattern.patternState = patternlab.config.patternStates[pattern.patternName];
@@ -74,24 +65,6 @@
7465
}
7566
}
7667

77-
// takes a filename string, not a full path; a basename (plus extension)
78-
// ignore _underscored patterns, dotfiles, and anything not recognized by a
79-
// loaded pattern engine. Pseudo-pattern .json files ARE considered to be
80-
// pattern files!
81-
function isPatternFile(filename) {
82-
// skip hidden patterns/files without a second thought
83-
var extension = path.extname(filename);
84-
if(filename.charAt(0) === '.' ||
85-
filename.charAt(0) === '_' ||
86-
(extension === '.json' && filename.indexOf('~') === -1)) {
87-
return false;
88-
}
89-
90-
// not a hidden pattern, let's dig deeper
91-
var supportedPatternFileExtensions = patternEngines.getSupportedFileExtensions();
92-
return (supportedPatternFileExtensions.lastIndexOf(extension) !== -1);
93-
}
94-
9568
function processPatternIterative(file, patternlab){
9669
var fs = require('fs-extra'),
9770
of = require('./object_factory'),
@@ -107,7 +80,7 @@
10780
}
10881

10982
// skip non-pattern files
110-
if (!isPatternFile(filename, patternlab)) { return; }
83+
if (!patternEngines.isPatternFile(filename, patternlab)) { return; }
11184
if (config.debug) {
11285
console.log('processPatternIterative:', 'found pattern', file);
11386
}
@@ -274,40 +247,7 @@
274247
throw 'Could not find pattern with key ' + key;
275248
}
276249

277-
/**
278-
* Recursively merge properties of two objects.
279-
*
280-
* @param {Object} obj1 If obj1 has properties obj2 doesn't, add to obj2.
281-
* @param {Object} obj2 This object's properties have priority over obj1.
282-
* @returns {Object} obj2
283-
*/
284-
function mergeData(obj1, obj2){
285-
if(typeof obj2 === 'undefined'){
286-
obj2 = {};
287-
}
288-
for(var p in obj1){
289-
try {
290-
// Only recurse if obj1[p] is an object.
291-
if(obj1[p].constructor === Object){
292-
// Requires 2 objects as params; create obj2[p] if undefined.
293-
if(typeof obj2[p] === 'undefined'){
294-
obj2[p] = {};
295-
}
296-
obj2[p] = mergeData(obj1[p], obj2[p]);
297-
// Pop when recursion meets a non-object. If obj1[p] is a non-object,
298-
// only copy to undefined obj2[p]. This way, obj2 maintains priority.
299-
} else if(typeof obj2[p] === 'undefined'){
300-
obj2[p] = obj1[p];
301-
}
302-
} catch(e) {
303-
// Property in destination object not set; create it and set its value.
304-
if(typeof obj2[p] === 'undefined'){
305-
obj2[p] = obj1[p];
306-
}
307-
}
308-
}
309-
return obj2;
310-
}
250+
311251

312252
function buildListItems(container){
313253
//combine all list items into one structure
@@ -317,7 +257,7 @@
317257
list.push(container.listitems[item]);
318258
}
319259
}
320-
container.listItemArray = shuffle(list);
260+
container.listItemArray = plutils.shuffle(list);
321261

322262
for(var i = 1; i <= container.listItemArray.length; i++){
323263
var tempItems = [];
@@ -333,11 +273,7 @@
333273
}
334274
}
335275

336-
//http://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array-in-javascript
337-
function shuffle(o){
338-
for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
339-
return o;
340-
}
276+
341277

342278
return {
343279
find_pattern_partials: function(pattern){
@@ -370,16 +306,9 @@
370306
get_pattern_by_key: function(key, patternlab){
371307
return getpatternbykey(key, patternlab);
372308
},
373-
merge_data: function(existingData, newData){
374-
return mergeData(existingData, newData);
375-
},
376309
combine_listItems: function(patternlab){
377310
buildListItems(patternlab);
378-
},
379-
is_object_empty: function(obj){
380-
return isObjectEmpty(obj);
381-
},
382-
is_pattern_file: isPatternFile
311+
}
383312
};
384313

385314
};

builder/pattern_engines/pattern_engines.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
(function () {
1313
'use strict';
1414

15+
var path = require('path');
16+
1517
// list of supported pattern engines
1618
var supportedPatternEngineNames = [
1719
'mustache',
@@ -42,6 +44,7 @@
4244
// accordingly
4345
return 'mustache';
4446
},
47+
4548
getEngineForPattern: function (pattern) {
4649
if (pattern.isPseudoPattern) {
4750
return this.getEngineForPattern(pattern.basePattern);
@@ -50,15 +53,35 @@
5053
return this[engineName];
5154
}
5255
},
56+
5357
getSupportedFileExtensions: function () {
5458
var engineNames = Object.keys(PatternEngines);
5559
return engineNames.map(function (engineName) {
5660
return PatternEngines[engineName].engineFileExtension;
5761
});
5862
},
63+
5964
isFileExtensionSupported: function (fileExtension) {
6065
var supportedExtensions = PatternEngines.getSupportedFileExtensions();
6166
return (supportedExtensions.lastIndexOf(fileExtension) !== -1);
67+
},
68+
69+
// takes a filename string, not a full path; a basename (plus extension)
70+
// ignore _underscored patterns, dotfiles, and anything not recognized by a
71+
// loaded pattern engine. Pseudo-pattern .json files ARE considered to be
72+
// pattern files!
73+
isPatternFile: function (filename) {
74+
// skip hidden patterns/files without a second thought
75+
var extension = path.extname(filename);
76+
if(filename.charAt(0) === '.' ||
77+
filename.charAt(0) === '_' ||
78+
(extension === '.json' && filename.indexOf('~') === -1)) {
79+
return false;
80+
}
81+
82+
// not a hidden pattern, let's dig deeper
83+
var supportedPatternFileExtensions = PatternEngines.getSupportedFileExtensions();
84+
return (supportedPatternFileExtensions.lastIndexOf(extension) !== -1);
6285
}
6386
});
6487

builder/patternlab.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var patternlab_engine = function () {
1919
mh = require('./media_hunter'),
2020
pe = require('./pattern_exporter'),
2121
he = require('html-entities').AllHtmlEntities,
22+
plutils = require('./utilities'),
2223
patternlab = {};
2324

2425
patternlab.package = fs.readJSONSync('./package.json');
@@ -120,7 +121,7 @@ var patternlab_engine = function () {
120121
patternlab.patterns.forEach(function(pattern, index, patterns){
121122
//render the pattern, but first consolidate any data we may have
122123
var allData = JSON.parse(JSON.stringify(patternlab.data));
123-
allData = pattern_assembler.merge_data(allData, pattern.jsonFileData);
124+
allData = plutils.mergeData(allData, pattern.jsonFileData);
124125

125126
pattern.patternPartial = pattern_assembler.renderPattern(pattern, allData);
126127

builder/pseudopattern_hunter.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
fs = require('fs-extra'),
2020
pa = require('./pattern_assembler'),
2121
lh = require('./lineage_hunter'),
22-
of = require('./object_factory');
22+
of = require('./object_factory'),
23+
plutils = require('./utilities');
2324

2425
var pattern_assembler = new pa();
2526
var lineage_hunter = new lh();
@@ -38,14 +39,15 @@
3839
for(var i = 0; i < pseudoPatterns.length; i++){
3940

4041
if(patternlab.config.debug){
42+
debugger;
4143
console.log('found pseudoPattern variant of ' + currentPattern.key);
4244
}
4345

4446
//we want to do everything we normally would here, except instead head the pseudoPattern data
4547
var variantFileData = fs.readJSONSync('source/_patterns/' + pseudoPatterns[i]);
4648

4749
//extend any existing data with variant data
48-
variantFileData = pattern_assembler.merge_data(currentPattern.jsonFileData, variantFileData);
50+
variantFileData = plutils.mergeData(currentPattern.jsonFileData, variantFileData);
4951

5052
var variantName = pseudoPatterns[i].substring(pseudoPatterns[i].indexOf('~') + 1).split('.')[0];
5153
var variantFilePath = 'source/_patterns/' + currentPattern.subdir + '/' + currentPattern.fileName + '~' + variantName + '.json';

builder/util.js

Lines changed: 0 additions & 22 deletions
This file was deleted.

builder/utilities.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* patternlab-node - v0.14.0 - 2015
3+
*
4+
* Brian Muenzenmeyer, Geoffrey Pursell 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.
8+
*
9+
*/
10+
11+
(function () {
12+
var path = require('path');
13+
14+
var util = {
15+
// http://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array-in-javascript
16+
shuffle: function (o) {
17+
for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
18+
return o;
19+
},
20+
21+
/**
22+
* Recursively merge properties of two objects.
23+
*
24+
* @param {Object} obj1 If obj1 has properties obj2 doesn't, add to obj2.
25+
* @param {Object} obj2 This object's properties have priority over obj1.
26+
* @returns {Object} obj2
27+
*/
28+
mergeData: function (obj1, obj2) {
29+
if(typeof obj2 === 'undefined'){
30+
obj2 = {};
31+
}
32+
for(var p in obj1){
33+
try {
34+
// Only recurse if obj1[p] is an object.
35+
if(obj1[p].constructor === Object){
36+
// Requires 2 objects as params; create obj2[p] if undefined.
37+
if(typeof obj2[p] === 'undefined'){
38+
obj2[p] = {};
39+
}
40+
obj2[p] = util.mergeData(obj1[p], obj2[p]);
41+
// Pop when recursion meets a non-object. If obj1[p] is a non-object,
42+
// only copy to undefined obj2[p]. This way, obj2 maintains priority.
43+
} else if(typeof obj2[p] === 'undefined'){
44+
obj2[p] = obj1[p];
45+
}
46+
} catch(e) {
47+
// Property in destination object not set; create it and set its value.
48+
if(typeof obj2[p] === 'undefined'){
49+
obj2[p] = obj1[p];
50+
}
51+
}
52+
}
53+
return obj2;
54+
},
55+
56+
isObjectEmpty: function (obj) {
57+
for(var prop in obj) {
58+
if(obj.hasOwnProperty(prop)) { return false; }
59+
}
60+
return true;
61+
}
62+
};
63+
64+
module.exports = util;
65+
}());

test/pattern_assembler_tests.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
var pa = require('../builder/pattern_assembler');
55
var of = require('../builder/object_factory');
6+
var patternEngines = require('../builder/pattern_engines/pattern_engines');
67

78
exports['pattern_assembler'] = {
89
'find_pattern_partials finds partials' : function(test){
@@ -341,7 +342,7 @@
341342
// loop over each test case and test it
342343
Object.keys(filenames).forEach(function (filename) {
343344
var expectedResult = filenames[filename],
344-
actualResult = pattern_assembler.is_pattern_file(filename),
345+
actualResult = patternEngines.isPatternFile(filename),
345346
testMessage = 'isPatternFile should return ' + expectedResult + ' for ' + filename;
346347
test.strictEqual(actualResult, expectedResult, testMessage);
347348
});

0 commit comments

Comments
 (0)