Skip to content
This repository has been archived by the owner on Nov 15, 2018. It is now read-only.

Commit

Permalink
style: Using a static get property for Dep class.
Browse files Browse the repository at this point in the history
let dep = new Dep('foo', '0.0.1');

then call like this:

dep.name
dep.version
  • Loading branch information
helio-frota committed Jan 12, 2017
2 parents e8b790a + b6f1d19 commit c7c6911
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 19 deletions.
14 changes: 9 additions & 5 deletions lib/dep.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
'use strict';

/**
* This class is just hold name and version of the dependency.
*/
class Dep {

constructor (name, version) {
this.name = name;
this.version = version;
constructor (n, v) {
this.name = n;
this.version = v;
}

getName () {
static get name () {
return this.name;
}

getVersion () {
static get version () {
return this.version;
}

}

module.exports = Dep;
10 changes: 5 additions & 5 deletions lib/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ function unused (declarations, dependencies) {
dependencies.forEach(dependency => {
declarations.forEach(declaration => {
if (declaration) {
if (declaration.toUpperCase().includes(dependency.getName().toUpperCase())) {
unused.add(dependency.getName());
if (declaration.toUpperCase().includes(dependency.name.toUpperCase())) {
unused.add(dependency.name);
}
}
});
Expand All @@ -94,7 +94,7 @@ function unused (declarations, dependencies) {
});
}
const unusedArray = Array.from(unused);
const diff = dependencies.filter(d => unusedArray.indexOf(d.getName()) < 0);
const diff = dependencies.filter(d => unusedArray.indexOf(d.name) < 0);
return diff.length ? diff : 'None.';
}

Expand All @@ -119,7 +119,7 @@ function jsonReporter (result, dependencies, requires) {
function licenseReporter (dependencies) {
return Promise.resolve().then(() => {
const licensePromises = dependencies.map(d => {
return roi.get({'endpoint': `http://registry.npmjs.org/${d.getName()}/${d.getVersion()}`});
return roi.get({'endpoint': `http://registry.npmjs.org/${d.name}/${d.version}`});
});

return Promise.all(licensePromises).then((results) => {
Expand Down Expand Up @@ -153,7 +153,7 @@ function consoleReport (jsonReport, options) {
header('[ Unused dependencies ]');
if (Array.isArray(jsonReport.unused)) {
jsonReport.unused.forEach(d => {
log.red(d.getName());
log.red(d.name);
});
} else {
log.red(jsonReport.unused);
Expand Down
6 changes: 3 additions & 3 deletions lib/searcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function searchDeclarations (lines, dependencies) {
const declarations = [];
dependencies.forEach(d => {
lines.forEach(l => {
if (l.includes(d.getName())) {
if (l.includes(d.name)) {
let extractedRequire = getRequireFromLine(l);
if (extractedRequire) {
let extractedVarName = getTextFromRequire(l);
Expand Down Expand Up @@ -120,10 +120,10 @@ function searchMissingDependencies (lines, dependencies) {
missing = getTextFromRequire(missing).trim();

let deps = dependencies[0].find(d => {
return d.getName() === missing;
return d.name === missing;
});
let devDeps = dependencies[1].find(d => {
return d.getName() === missing;
return d.name === missing;
});
let core = dependencies[2].find(d => {
return d === missing;
Expand Down
2 changes: 1 addition & 1 deletion test/szero-api-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ test('should return unused Array with 1 Dep Object', (t) => {
szero.report(dir, options).then((jsonReport) => {
t.true(jsonReport.unused, 'should have a unused object');
t.equal(Array.isArray(jsonReport.unused), true, 'should be an array');
t.equal(jsonReport.unused[0].getName(), 'swapi-node', 'should be an array');
t.equal(jsonReport.unused[0].name, 'swapi-node', 'should be an array');
t.end();
});
});
Expand Down
4 changes: 2 additions & 2 deletions test/szero-cli-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ test('Should run a summary report', (t) => {
cli(path.join(__dirname, '/fixtures'), {
summary: true
}).then((report) => {
t.deepEqual(report.dependencies.map(d => d.getName()), [ 'roi', 'fidelity', 'request', 'ramda' ]);
t.deepEqual(report.unused.map(d => d.getName()), [ 'request' ]);
t.deepEqual(report.dependencies.map(d => d.name), [ 'roi', 'fidelity', 'request', 'ramda' ]);
t.deepEqual(report.unused.map(d => d.name), [ 'request' ]);
t.deepEqual(report.devDependencies, []);
t.pass();
t.end();
Expand Down
6 changes: 3 additions & 3 deletions test/szero-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ test('Should find javascript files.', t => {
test('Should search for dependencies.', t => {
const lines = reader.read(path.join(__dirname, '/fixtures/package.json'));
const dependencies = searcher.searchDependencies(lines, true);
t.equal(dependencies[0][0].getName() === 'roi', true);
t.equal(dependencies[0][0].name === 'roi', true);
t.end();
});

Expand Down Expand Up @@ -123,7 +123,7 @@ test('Should show unused dependencies from report.', t => {
const javascriptLines = reader.read(path.join(__dirname, '/fixtures/foo/x.js'));
const declarations = searcher.searchDeclarations(javascriptLines, dependencies[0]);
const unused = reporter.unused(declarations, dependencies[0]);
let names = unused.map(u => u.getName());
let names = unused.map(u => u.name);
t.equal(names.toString(), 'fidelity,request');
t.end();
});
Expand All @@ -144,7 +144,7 @@ test('Should show all unused dependencies.', t => {
const javascriptLines = reader.read(path.join(__dirname, '/fixtures/bar/all-unused.js'));
const declarations = searcher.searchDeclarations(javascriptLines, dependencies[0]);
const unused = reporter.unused(declarations, dependencies[0]);
let names = unused.map(u => u.getName());
let names = unused.map(u => u.name);
t.equal(names.toString(), 'roi');
t.end();
});

0 comments on commit c7c6911

Please sign in to comment.