Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

some minor changes

• use the `in` operator more frequently (it's much nicer than `typeof
foo !== "undefined"` + is more readable)
• removed some garbage
• added some comments
  • Loading branch information...
commit f74cfd3e5ef8c71634936a270c512f951e4d4bda 1 parent b12e72d
@fb55 fb55 authored
Showing with 42 additions and 35 deletions.
  1. +42 −35 lib/require-analyzer.js
View
77 lib/require-analyzer.js
@@ -148,8 +148,8 @@ analyzer.npmAnalyze = function (deps, options, callback) {
if (result.devDependencies && pkg in result.devDependencies) return;
if (result.bundleDependencies && pkg in result.bundleDependencies) return;
if (!Array.isArray(deps)) {
- if (deps[pkg] === '*' || typeof deps[pkg] === 'undefined') {
- pkgs[pkg] = result.dependencies[pkg]
+ if (deps[pkg] === '*' || !(pkg in deps) ) {
+ pkgs[pkg] = pkg in result.dependencies
? result.dependencies[pkg]['version']
: deps[pkg];
}
@@ -168,13 +168,13 @@ analyzer.npmAnalyze = function (deps, options, callback) {
return callback(null, pkgs);
}
- var reduced = analyzer.merge({}, pkgs),
+ var reduced = analyzer.clone(pkgs),
suspect = {};
Object.keys(deps).forEach(function (dep) {
- if (pkgs[dep] && pkgs[dep].dependencies) {
+ if (dep in pkgs && pkgs[dep].dependencies) {
Object.keys(pkgs[dep].dependencies).forEach(function (cdep) {
- if (reduced[cdep]) {
+ if (cdep in reduced) {
suspect[cdep] = pkgs[cdep];
delete reduced[cdep];
}
@@ -345,7 +345,7 @@ analyzer.package = function (options, callback) {
});
}
- var scripts = options.hasOwnProperty('scripts') ? options.scripts : ["test","prestart"];
+ var scripts = 'scripts' in options ? options.scripts : ["test","prestart"];
scripts = scripts.map(function (item) {
return pkg.scripts && pkg.scripts[item];
@@ -387,7 +387,7 @@ analyzer.package = function (options, callback) {
}
function checkFile(file) {
- exists(file, fileExists)
+ exists(file, fileExists);
}
function fileExists(exists) {
@@ -415,8 +415,8 @@ analyzer.package = function (options, callback) {
}
// add logic to default to app.js or server.js for main if main is not present.
- if (typeof pkg.main === 'undefined' || pkg.main === '') {
- var files = ["app.js", "server.js", "index.js"]
+ if ( !("main" in pkg) || pkg.main === '') {
+ var files = ["app.js", "server.js", "index.js"];
setMain(files, pkg, newoptions, setTarget);
}
else {
@@ -576,24 +576,24 @@ analyzer.file = function(options, callback){
analyzer.findModulesDir = function (target, callback) {
fs.stat(target, function (err, stats) {
if (err) {
- return callback(err);
+ callback(err);
}
- if (stats.isDirectory()) {
- return fs.readdir(target, function (err, files) {
+ else if (stats.isDirectory()) {
+ fs.readdir(target, function (err, files) {
if (err) {
- return callback(err);
+ callback(err);
}
-
- if (files.indexOf('node_modules') !== -1 || files.indexOf('package.json') !== -1) {
- return callback(null, target);
+ //TODO behave differently when a node_modules dir is present
+ else if (files.indexOf('node_modules') !== -1 || files.indexOf('package.json') !== -1) {
+ callback(null, target);
}
else {
- return callback(null, target);
+ callback(null, target);
}
});
}
else if (stats.isFile()) {
- return analyzer.findModulesDir(path.dirname(target), callback);
+ analyzer.findModulesDir(path.dirname(target), callback);
}
});
};
@@ -604,11 +604,13 @@ analyzer.findModulesDir = function (target, callback) {
// Merges all properties in `arg1 ... argn`
// into the `target` object.
//
+// TODO remove this as it isn't used anymore
+//
analyzer.merge = function (target) {
var objs = Array.prototype.slice.call(arguments, 1);
objs.forEach(function (o) {
Object.keys(o).forEach(function (attr) {
- if (! o.__lookupGetter__(attr)) {
+ if ( !("get" in Object.getOwnPropertyDescriptor(o, attr)) ) {
target[attr] = o[attr];
}
});
@@ -654,7 +656,7 @@ analyzer.extractVersions = function (dependencies) {
parse = semver.expressions.parse.exec(raw.trim()),
version = parse ? parse.slice(1) : raw,
build = version ? version[3] || version[4] : null;
- if (!/^[v\d]+/.test(raw)) {
+ if (!/^[v\d]/.test(raw)) {
all[pkg] = raw;
}
else if (typeof version === 'string') {
@@ -682,17 +684,15 @@ analyzer.extractVersions = function (dependencies) {
// updated: { /* Union of updated / current with new versions */ }
// }
//
+var cleanVersion = /\<|\>|\=|\s/ig;
+
analyzer.updates = function (current, updated) {
var updates = {
- added: {},
+ added: !current && updated || {},
updated: {}
};
- if (!current) {
- updates.updated = updated || {};
- return updates;
- }
- else if (!updated) {
+ if (!current || !updated) {
return updates;
}
@@ -700,7 +700,7 @@ analyzer.updates = function (current, updated) {
// Get the list of all added dependencies
//
Object.keys(updated).filter(function (key) {
- return !current[key];
+ return !(key in current);
}).forEach(function (key) {
updates.added[key] = updated[key];
});
@@ -709,17 +709,17 @@ analyzer.updates = function (current, updated) {
// Get the list of all dependencies that have been updated
//
Object.keys(updated).filter(function (key) {
- if (!current[key]) {
+ if ( !(key in current) ) {
return false;
}
- var left = updated[key].replace(/\<|\>|\=|\s/ig, ''),
- right = current[key].replace(/\<|\>|\=|\s/ig, '');
+ var left = updated[key].replace(cleanVersion, ''),
+ right = current[key].replace(cleanVersion, '');
return semver.gt(left, right);
}).forEach(function (key) {
updates.updated[key] = updated[key];
- })
+ });
return updates;
};
@@ -730,16 +730,23 @@ analyzer.updates = function (current, updated) {
// Check if `module` is a native module (like `net` or `tty`).
//
// TODO use the resolve module for this
+// (faster & doesn't depend on the node version)
//
analyzer.isNative = function (module) {
try {
- return require.resolve(module) == module;
+ return require.resolve(module) === module;
}
catch (err) {
return false;
}
};
+//
+// ### function resolve (file, base)
+// #### @file {string} filename
+// #### @base {string} the root from which the file should be searched
+// Check if `module` is a native module (like `net` or `tty`).
+//
analyzer.resolve = function(file, base){
try {
return resolve.sync(file, {
@@ -755,12 +762,12 @@ function mergeDependencies(err, deps, pkgDeps, devDeps, bndlDeps, callback) {
function removeDevDeps(deps) {
var obj = analyzer.clone(deps), dep;
for (dep in devDeps) {
- if (typeof obj[dep] !== 'undefined') {
+ if (dep in obj) {
delete obj[dep];
}
}
for (dep in bndlDeps) {
- if (typeof obj[dep] !== 'undefined') {
+ if (dep in obj) {
delete obj[dep];
}
}
@@ -792,7 +799,7 @@ function mergeDependencies(err, deps, pkgDeps, devDeps, bndlDeps, callback) {
});
Object.keys(pkgDeps).forEach(function (d) {
- if (typeof merged[d] === 'undefined') {
+ if ( !(d in merged[d]) ) {
merged[d] = pkgDeps[d];
}
});
Please sign in to comment.
Something went wrong with that request. Please try again.