Skip to content

Commit 7971a2e

Browse files
Merge pull request #659 from pattern-lab/feature/replace-common-utils
Feature/replace common utils
2 parents 4eff3f2 + 6a9674b commit 7971a2e

File tree

7 files changed

+15
-110
lines changed

7 files changed

+15
-110
lines changed

core/lib/list_item_hunter.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
const list_item_hunter = function () {
44
const extend = require('util')._extend;
5+
const _ = require('lodash');
56
const pa = require('./pattern_assembler');
67
const smh = require('./style_modifier_hunter');
7-
const plutils = require('./utilities');
88
const jsonCopy = require('./json_copy');
99
const Pattern = require('./object_factory').Pattern;
1010

@@ -47,7 +47,7 @@ const list_item_hunter = function () {
4747
console.log(err);
4848
}
4949

50-
listData = plutils.mergeData(listData, pattern.listitems);
50+
listData = _.merge(listData, pattern.listitems);
5151
listData = pattern_assembler.parse_data_links_specific(patternlab, listData, 'listitems.json + any pattern listitems.json');
5252

5353
//iterate over each copied block, rendering its contents along with pattenlab.listitems[i]
@@ -68,8 +68,8 @@ const list_item_hunter = function () {
6868
console.log(err);
6969
}
7070

71-
let allData = plutils.mergeData(globalData, localData);
72-
allData = plutils.mergeData(allData, itemData !== undefined ? itemData[i] : {}); //itemData could be undefined if the listblock contains no partial, just markup
71+
let allData = _.merge(globalData, localData);
72+
allData = _.merge(allData, itemData !== undefined ? itemData[i] : {}); //itemData could be undefined if the listblock contains no partial, just markup
7373
allData.link = extend({}, patternlab.data.link);
7474

7575
//check for partials within the repeated block

core/lib/parameter_hunter.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
const parameter_hunter = function () {
44
const extend = require('util')._extend;
5+
const _ = require('lodash');
56
const jsonCopy = require('./json_copy');
67
const pa = require('./pattern_assembler');
78
const smh = require('./style_modifier_hunter');
8-
const plutils = require('./utilities');
99
const style_modifier_hunter = new smh();
1010
const pattern_assembler = new pa();
1111

@@ -267,8 +267,8 @@ const parameter_hunter = function () {
267267
console.log(err);
268268
}
269269

270-
let allData = plutils.mergeData(globalData, localData);
271-
allData = plutils.mergeData(allData, paramData);
270+
let allData = _.merge(globalData, localData);
271+
allData = _.merge(allData, paramData);
272272

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

core/lib/pattern_assembler.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ const pattern_assembler = function () {
6262
list.push(container.listitems[item]);
6363
}
6464
}
65-
container.listItemArray = plutils.shuffle(list);
65+
container.listItemArray = _.shuffle(list);
6666

6767
for (var i = 1; i <= container.listItemArray.length; i++) {
6868
var tempItems = [];
@@ -149,7 +149,7 @@ const pattern_assembler = function () {
149149
var markdownFileContents = fs.readFileSync(markdownFileName, 'utf8');
150150

151151
var markdownObject = markdown_parser.parse(markdownFileContents);
152-
if (!plutils.isObjectEmpty(markdownObject)) {
152+
if (!_.isEmpty(markdownObject)) {
153153
//set keys and markdown itself
154154
currentPattern.patternDescExists = true;
155155
currentPattern.patternDesc = markdownObject.markdown;

core/lib/patternlab.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ const patternlab_engine = function (config) {
401401
console.log('There was an error parsing JSON for ' + pattern.relPath);
402402
console.log(err);
403403
}
404-
allData = plutils.mergeData(allData, pattern.jsonFileData);
404+
allData = _.merge(allData, pattern.jsonFileData);
405405
allData.cacheBuster = patternlab.cacheBuster;
406406

407407
//re-rendering the headHTML each time allows pattern-specific data to influence the head of the pattern
@@ -452,7 +452,7 @@ const patternlab_engine = function (config) {
452452
console.log('There was an error parsing JSON for ' + pattern.relPath);
453453
console.log(err);
454454
}
455-
allFooterData = plutils.mergeData(allFooterData, pattern.jsonFileData);
455+
allFooterData = _.merge(allFooterData, pattern.jsonFileData);
456456
allFooterData.patternLabFoot = footerPartial;
457457

458458
const footerHTML = pattern_assembler.renderPattern(patternlab.userFoot, allFooterData);

core/lib/pseudopattern_hunter.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
const ch = require('./changes_hunter');
44
const glob = require('glob');
55
const fs = require('fs-extra');
6+
const _ = require('lodash');
67
const lh = require('./lineage_hunter');
78
const Pattern = require('./object_factory').Pattern;
8-
const plutils = require('./utilities');
99
const path = require('path');
1010
const lineage_hunter = new lh();
1111
const changes_hunter = new ch();
@@ -43,7 +43,7 @@ pseudopattern_hunter.prototype.find_pseudopatterns = function (currentPattern, p
4343
}
4444

4545
//extend any existing data with variant data
46-
variantFileData = plutils.mergeData(currentPattern.jsonFileData, variantFileData);
46+
variantFileData = _.merge(currentPattern.jsonFileData, variantFileData);
4747

4848
let variantName = pseudoPatterns[i].substring(pseudoPatterns[i].indexOf('~') + 1).split('.')[0];
4949
let variantFilePath = path.join(currentPattern.subdir, currentPattern.fileName + '~' + variantName + '.json');
@@ -89,7 +89,7 @@ pseudopattern_hunter.prototype.find_pseudopatterns = function (currentPattern, p
8989
}
9090

9191
//extend any existing data with variant data
92-
variantFileData = plutils.mergeData(currentPattern.jsonFileData, variantFileData);
92+
variantFileData = _.merge(currentPattern.jsonFileData, variantFileData);
9393

9494
variantName = pseudoPatterns[i].substring(pseudoPatterns[i].indexOf('~') + 1).split('.')[0];
9595
variantFilePath = path.join(currentPattern.subdir, currentPattern.fileName + '~' + variantName + '.json');

core/lib/starterkit_manager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const starterkit_manager = function (config) {
3131

3232
if (clean) {
3333
console.log('Deleting contents of', paths.source.root, 'prior to starterkit load.');
34-
util.emptyDirectory(paths.source.root);
34+
fs.emptyDirSync(paths.source.root);
3535
} else {
3636
console.log('Overwriting contents of', paths.source.root, 'during starterkit load.');
3737
}

core/lib/utilities.js

Lines changed: 0 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -49,97 +49,6 @@ const warning = log.warning.bind(log);
4949
*/
5050
const error = log.error.bind(log);
5151

52-
/**
53-
* Shuffles an array in place.
54-
* http://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array-in-javascript
55-
*
56-
* @param {Array} o
57-
* @returns {Array} o
58-
*/
59-
const shuffle = function (o) {
60-
/*eslint-disable curly*/
61-
for (var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
62-
return o;
63-
};
64-
65-
/**
66-
* Recursively merge properties of two objects.
67-
*
68-
* @param {Object} obj1 If obj1 has properties obj2 doesn't, add to obj2.
69-
* @param {Object} obj2 This object's properties have priority over obj1.
70-
* @returns {Object} obj2
71-
*/
72-
const mergeData = function (obj1, obj2) {
73-
/*eslint-disable no-param-reassign, guard-for-in*/
74-
if (typeof obj2 === 'undefined') {
75-
obj2 = {};
76-
}
77-
for (var p in obj1) {
78-
try {
79-
// Only recurse if obj1[p] is an object.
80-
if (obj1[p].constructor === Object) {
81-
// Requires 2 objects as params; create obj2[p] if undefined.
82-
if (typeof obj2[p] === 'undefined') {
83-
obj2[p] = {};
84-
}
85-
obj2[p] = mergeData(obj1[p], obj2[p]);
86-
87-
// Pop when recursion meets a non-object. If obj1[p] is a non-object,
88-
// only copy to undefined obj2[p]. This way, obj2 maintains priority.
89-
} else if (typeof obj2[p] === 'undefined') {
90-
obj2[p] = obj1[p];
91-
}
92-
} catch (e) {
93-
// Property in destination object not set; create it and set its value.
94-
if (typeof obj2[p] === 'undefined') {
95-
obj2[p] = obj1[p];
96-
}
97-
}
98-
}
99-
return obj2;
100-
};
101-
102-
/**
103-
* Determines whether or not an object is empty.
104-
*
105-
* @param {Object} obj
106-
* @returns {Boolean}
107-
*/
108-
const isObjectEmpty = function (obj) {
109-
for (var prop in obj) {
110-
if (obj.hasOwnProperty(prop)) { return false; }
111-
}
112-
return true;
113-
};
114-
115-
/**
116-
* Recursively delete the contents of directory.
117-
* Adapted from https://gist.github.com/tkihira/2367067
118-
*
119-
* @param {string} dir - directory to empty
120-
* @param {string} cleanDir - already empty directory
121-
* @returns {undefined}
122-
*/
123-
const emptyDirectory = function (dir, cleanDir) {
124-
var list = fs.readdirSync(dir);
125-
for (var i = 0; i < list.length; i++) {
126-
var filename = path.join(dir, list[i]);
127-
var stat = fs.statSync(filename);
128-
129-
if (filename === "." || filename === "..") {
130-
// pass these files
131-
} else if (stat.isDirectory()) {
132-
this.emptyDirectory(filename);
133-
} else {
134-
// rm fiilename
135-
fs.unlinkSync(filename);
136-
}
137-
}
138-
if (cleanDir) {
139-
fs.rmdirSync(dir);
140-
}
141-
};
142-
14352
/**
14453
* Useful for reporting errors in .catch() on Promises
14554
* @param {string} - a message to report
@@ -158,9 +67,5 @@ module.exports = {
15867
warning,
15968
error,
16069
log,
161-
shuffle,
162-
mergeData,
163-
isObjectEmpty,
164-
emptyDirectory,
16570
reportError
16671
};

0 commit comments

Comments
 (0)