Replace custom isEmpty with lodash.

This commit is contained in:
Tobias Lidskog 2016-09-29 20:29:46 +02:00
parent 55a5ba4368
commit 187eb3e0dd
10 changed files with 32 additions and 46 deletions

View File

@ -2,7 +2,7 @@
let messageMaker = require('../../support/messageMaker'),
path = require('path'),
isNotEmpty = require('../../support/util').isNotEmpty,
isEmpty = require('lodash.isempty'),
aggregator = require('./aggregator');
const make = messageMaker('assets').make;
@ -25,7 +25,7 @@ module.exports = {
case 'summarize':
{
const summary = aggregator.summarize();
if (isNotEmpty(summary)) {
if (!isEmpty(summary)) {
for (let group of Object.keys(summary.groups)) {
queue.postMessage(make('assets.aggregate', summary.groups[group], {group}));

View File

@ -11,7 +11,7 @@ const browsertime = require('browsertime'),
merge = require('lodash.merge'),
BrowsertimeError = browsertime.errors.BrowsertimeError,
analyzer = require('./analyzer'),
isNotEmpty = require('../../support/util').isNotEmpty;
isEmpty = require('lodash.isempty');
const make = messageMaker('browsertime').make;
@ -70,7 +70,7 @@ module.exports = {
const coachAdvice = run.coach.coachAdvice;
// check if the coach has error(s)
if (isNotEmpty(coachAdvice.errors)) {
if (!isEmpty(coachAdvice.errors)) {
log.error('%s generated the following errors in the coach %:2j', url, coachAdvice.errors);
queue.postMessage(make('error', 'The coach got the following errors: ' + JSON.stringify(coachAdvice.errors), {
url,

View File

@ -4,7 +4,7 @@ const Stats = require('fast-stats').Stats,
urlParser = require('url'),
log = require('intel'),
statsHelpers = require('../../support/statsHelpers'),
isNotEmpty = require('../../support/util').isNotEmpty,
isEmpty = require('lodash.isempty'),
reduce = require('lodash.reduce');
const timingNames = ['blocked', 'dns', 'connect', 'ssl', 'send', 'wait', 'receive'];
@ -80,12 +80,12 @@ module.exports = {
};
const stats = statsHelpers.summarizeStats(domainStats.totalTime);
if (isNotEmpty(stats)) {
if (!isEmpty(stats)) {
domainSummary.totalTime = stats;
}
timingNames.forEach((name) => {
const stats = statsHelpers.summarizeStats(domainStats[name]);
if (isNotEmpty(stats)) {
if (!isEmpty(stats)) {
domainSummary[name] = stats;
}
});

View File

@ -3,7 +3,7 @@
let path = require('path'),
messageMaker = require('../../support/messageMaker'),
filterRegistry = require('../../support/filterRegistry'),
isNotEmpty = require('../../support/util').isNotEmpty,
isEmpty = require('lodash.isempty'),
aggregator = require('./aggregator');
const make = messageMaker('domains').make;
@ -27,7 +27,7 @@ module.exports = {
case 'summarize':
{
const summary = aggregator.summarize();
if (isNotEmpty(summary)) {
if (!isEmpty(summary)) {
queue.postMessage(make('domains.summary', summary));
}
break;

View File

@ -2,7 +2,7 @@
let path = require('path'),
throwIfMissing = require('../../support/util').throwIfMissing,
isEmpty = require('../../support/util').isEmpty,
isEmpty = require('lodash.isempty'),
filterRegistry = require('../../support/filterRegistry'),
Sender = require('./sender'),
log = require('intel'),

View File

@ -2,7 +2,7 @@
let path = require('path'),
throwIfMissing = require('../../support/util').throwIfMissing,
isEmpty = require('../../support/util').isEmpty,
isEmpty = require('lodash.isempty'),
filterRegistry = require('../../support/filterRegistry'),
log = require('intel'),
Sender = require('./sender'),

View File

@ -1,6 +1,6 @@
'use strict';
const isNotEmpty = require('./util').isNotEmpty,
const isEmpty = require('lodash.isempty'),
get = require('lodash.get'),
set = require('lodash.set'),
merge = require('lodash.merge'),
@ -57,7 +57,7 @@ module.exports = {
if (typeof value === 'object') {
const leaf = this.filterMetrics(value, leafPath);
if (isNotEmpty(leaf)) {
if (!isEmpty(leaf)) {
result[key] = leaf;
}
}
@ -74,7 +74,7 @@ module.exports = {
const leafs = Object.keys(branch).reduce((result, key) => {
const leaf = this.filterMetrics(branch[key], leafPath);
if (isNotEmpty(leaf)) {
if (!isEmpty(leaf)) {
result[key] = leaf;
}
return result;
@ -82,7 +82,7 @@ module.exports = {
branch = get(result, branchPath, leafs);
branch = merge(branch, leafs);
if (isNotEmpty(branch)) {
if (!isEmpty(branch)) {
return set(result, branchPath, branch);
}
}

View File

@ -2,11 +2,12 @@
const fs = require('fs-extra'),
Promise = require('bluebird'),
isEmpty = require('lodash.isempty'),
moment = require('moment'),
path = require('path'),
url = require('url'),
crypto = require('crypto'),
urlParser = require('url'),
util = require('./util');
urlParser = require('url');
Promise.promisifyAll(fs);
const mkdirp = Promise.promisify(require('mkdirp'));
@ -17,12 +18,22 @@ function write(dirPath, filename, data) {
fs.writeFileAsync(path.join(dirPath, filename), data, 'utf8'));
}
function getDomainOrFileName(string) {
let domainOrFile = string;
if (domainOrFile.startsWith('http')) {
domainOrFile = url.parse(domainOrFile).hostname;
} else {
domainOrFile = path.basename(domainOrFile).replace(/\./g, '_');
}
return domainOrFile;
}
class StorageManager {
constructor(url, timestamp, options) {
timestamp = timestamp || moment();
options = options || {};
const domainOrFile = util.getDomainOrFileName(url);
const domainOrFile = getDomainOrFileName(url);
this.timestamp = timestamp.format('YYYY-MM-DD-HH-mm-ss');
this.baseDir = options.outputFolder ? path.resolve(process.cwd(), options.outputFolder) :
@ -46,7 +57,7 @@ class StorageManager {
pathSegments.unshift('pages');
if (util.isNotEmpty(parsedUrl.search)) {
if (!isEmpty(parsedUrl.search)) {
const md5 = crypto.createHash('md5'),
hash = md5.update(parsedUrl.search).digest('hex').substring(0, 8);
pathSegments.push('query-' + hash);

View File

@ -1,8 +1,6 @@
'use strict';
const format = require('util').format,
path = require('path'),
url = require('url');
const format = require('util').format;
module.exports = {
throwIfMissing(options, keys, namespace) {
@ -11,29 +9,5 @@ module.exports = {
throw new Error(format('Required option(s) %s need to be specified in namespace "%s"',
missingKeys.map((s) => '"' + s + '"'), namespace));
}
},
isEmpty(o) {
if (o === null || o === undefined)
return true;
if (typeof o === 'object')
return Object.keys(o).length === 0;
if (typeof o === 'string')
return o.length === 0;
return false;
},
isNotEmpty(o) {
return !(module.exports.isEmpty(o));
},
getDomainOrFileName(string) {
let domainOrFile = string;
if (domainOrFile.startsWith('http')) {
domainOrFile = url.parse(domainOrFile).hostname;
} else {
domainOrFile = path.basename(domainOrFile).replace(/\./g, '_');
}
return domainOrFile;
}
};

View File

@ -82,6 +82,7 @@
"lodash.flatten": "4.4.0",
"lodash.foreach": "4.5.0",
"lodash.get": "4.4.2",
"lodash.isempty": "4.4.0",
"lodash.merge": "4.6.0",
"lodash.pullall": "4.2.0",
"lodash.reduce": "4.6.0",