Skip to content

Commit 1256531

Browse files
committed
Reduced number of lines in validator function
1 parent f5be3a8 commit 1256531

File tree

15 files changed

+415
-339
lines changed

15 files changed

+415
-339
lines changed

HelloWorld/HelloWorld.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"use strict";
44
function serve (req, res) {
55
res.writeHead(200, {
6-
"Content-Type"+": "text/plain"
6+
"Content-Type": "text/plain"
77
});
88
res.end("Hello World\n");
99
}

spider4/spider.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,14 @@ function spider(url, nesting, callback) {
6969
function spiderLinks(currentUrl, body, nesting, callback) {
7070
if (nesting === 0)
7171
return process.nextTick(callback, null, currentUrl, downloaded);
72-
7372
const links = utilities.getPageLinks(currentUrl, body);
7473
if (links.length === 0)
7574
return process.nextTick(callback, null, currentUrl, downloaded);
76-
let completed = 0;
77-
let running = 0;
78-
let index = 0;
79-
let inError = false;
80-
let error = null;
75+
let completed = 0,
76+
running = 0,
77+
index = 0,
78+
inError = false,
79+
error = null;
8180

8281
function done(err) {
8382
if (err) {

spiderparallel/spider.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,16 @@ function spider(url, nesting, callback) {
8585
});
8686
}
8787

88-
if (!validator.validate())
89-
process.exit();
88+
const errors = validator.validate();
89+
if (errors.length || cmdConfig.get("help"))
90+
{
91+
console.log(cmdConfig.usage);
92+
errors.forEach((err) =>
93+
{
94+
console.error(err);
95+
});
96+
process.exit(errors.length);
97+
}
9098

9199
spider(cmdConfig.get("url"), cmdConfig.get("nesting",1), (err) => {
92100
if(err) {

spiderparallel/validator.js

Lines changed: 58 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -6,62 +6,64 @@ const validator = require("validator");
66
const cmdConfig = require("./cmdconfig");
77
const assert = require("assert");
88

9-
module.exports.validate = function()
10-
{
11-
const options = cmdConfig.options;
12-
let assertCount = 0;
13-
function inc()
14-
{
15-
assertCount++;
16-
return assertCount;
17-
}
18-
try{
19-
assert(options._all.url,"No url specified");
20-
}
21-
catch(err)
22-
{
23-
inc();
24-
console.error(err.message);
25-
}
26-
if (options._all.url)
27-
{
28-
try{
29-
assert(validator.isURL(options._all.url,{protocols:["http","https"],require_host: true, require_valid_protocol:true,require_protocols:true}),options._all.url + " is invalid.");
30-
}
31-
catch(err)
32-
{
33-
inc();
34-
console.error(err.message);
35-
}
36-
}
37-
if (options._all.concurrency !== undefined)
38-
try
39-
{
40-
assert(validator.isInt(options._all.concurrency.toString(),{min:1}),"Concurrency must be greater than 0");
41-
}
42-
catch(err)
43-
{
44-
inc();
45-
console.error(err.message);
46-
}
9+
function validateEmptyURL(url, errors) {
10+
try {
11+
assert(url, "No url specified");
12+
} catch (err) {
13+
errors.push(err.message);
14+
}
15+
return errors;
16+
}
4717

48-
if (options._all.nesting !== undefined)
49-
{
50-
try {
51-
assert(validator.isInt(options._all.nesting.toString(),{gt:0}),"Nesting must be greater than 0");
52-
}
53-
catch(err)
54-
{
55-
inc();
56-
console.error(err.message);
57-
}
58-
}
18+
function validateURLFormat(url, errors) {
19+
if (url) {
20+
try {
21+
assert(validator.isURL(url, {
22+
protocols: ["http", "https"],
23+
require_host: true,
24+
require_valid_protocol: true,
25+
require_protocols: true
26+
}), url + " is invalid.");
27+
} catch (err) {
28+
errors.push(err.message);
29+
}
30+
}
31+
return errors;
32+
}
5933

60-
if (assertCount || options._all.help)
61-
{
62-
console.error(cmdConfig.usage);
63-
return false;
64-
}
65-
66-
return true;
34+
function validateConcurrency(concurrency, errors) {
35+
if (concurrency !== undefined) {
36+
try {
37+
assert(validator.isInt(concurrency.toString(), {
38+
min: 1
39+
}), "Concurrency must be greater than 0");
40+
} catch (err) {
41+
errors.push(err.message);
42+
}
43+
}
44+
return errors;
45+
}
46+
47+
function validateNesting(nesting, errors) {
48+
if (nesting !== undefined) {
49+
try {
50+
assert(validator.isInt(nesting.toString(), {
51+
gt: 0
52+
}), "Nesting must be greater than 0");
53+
} catch (err) {
54+
errors.push(err.message);
55+
}
56+
}
57+
return errors;
58+
}
59+
60+
module.exports.validate = function() {
61+
const options = cmdConfig.options;
62+
let errors = [];
63+
errors = validateEmptyURL(options._all.url, errors);
64+
errors = validateURLFormat(options._all.url, errors);
65+
errors = validateConcurrency(options._all.concurrency, errors);
66+
errors = validateNesting(options._all.nesting, errors);
67+
return errors;
6768
};
69+

spiderpromises/spider.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,16 @@ function spider( url, nesting)
6868
);
6969
}
7070

71-
if (!validator.validate())
72-
process.exit();
71+
const errors = validator.validate();
72+
if (errors.length || cmdConfig.get("help"))
73+
{
74+
console.log(cmdConfig.usage);
75+
errors.forEach((err) =>
76+
{
77+
console.error(err);
78+
});
79+
process.exit(errors.length);
80+
}
7381

7482
spider(cmdConfig.get("url"),cmdConfig.get("nesting",1)).
7583
then( function() {

spiderpromises/validator.js

Lines changed: 58 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,52 +6,64 @@ const validator = require("validator");
66
const cmdConfig = require("./cmdconfig");
77
const assert = require("assert");
88

9-
module.exports.validate = function()
10-
{
11-
const options = cmdConfig.options;
12-
let assertCount = 0;
13-
function inc()
14-
{
15-
assertCount++;
16-
return assertCount;
17-
}
18-
try{
19-
assert(options._all.url,"No url specified");
20-
}
21-
catch(err)
22-
{
23-
inc();
24-
console.error(err.message);
25-
}
26-
if (options._all.url)
27-
{
28-
try{
29-
assert(validator.isURL(options._all.url,{protocols:["http","https"],require_host: true, require_valid_protocol:true,require_protocols:true}),options._all.url + " is invalid.");
30-
}
31-
catch(err)
32-
{
33-
inc();
34-
console.error(err.message);
35-
}
36-
}
9+
function validateEmptyURL(url, errors) {
10+
try {
11+
assert(url, "No url specified");
12+
} catch (err) {
13+
errors.push(err.message);
14+
}
15+
return errors;
16+
}
3717

38-
if (options._all.nesting !== undefined)
39-
{
40-
try {
41-
assert(validator.isInt(options._all.nesting.toString(),{gt:0}),"Nesting must be greater than 0");
42-
}
43-
catch(err)
44-
{
45-
inc();
46-
console.error(err.message);
47-
}
48-
}
18+
function validateURLFormat(url, errors) {
19+
if (url) {
20+
try {
21+
assert(validator.isURL(url, {
22+
protocols: ["http", "https"],
23+
require_host: true,
24+
require_valid_protocol: true,
25+
require_protocols: true
26+
}), url + " is invalid.");
27+
} catch (err) {
28+
errors.push(err.message);
29+
}
30+
}
31+
return errors;
32+
}
4933

50-
if (assertCount || options._all.help)
51-
{
52-
console.error(cmdConfig.usage);
53-
return false;
54-
}
55-
56-
return true;
34+
function validateConcurrency(concurrency, errors) {
35+
if (concurrency !== undefined) {
36+
try {
37+
assert(validator.isInt(concurrency.toString(), {
38+
min: 1
39+
}), "Concurrency must be greater than 0");
40+
} catch (err) {
41+
errors.push(err.message);
42+
}
43+
}
44+
return errors;
45+
}
46+
47+
function validateNesting(nesting, errors) {
48+
if (nesting !== undefined) {
49+
try {
50+
assert(validator.isInt(nesting.toString(), {
51+
gt: 0
52+
}), "Nesting must be greater than 0");
53+
} catch (err) {
54+
errors.push(err.message);
55+
}
56+
}
57+
return errors;
58+
}
59+
60+
module.exports.validate = function() {
61+
const options = cmdConfig.options;
62+
let errors = [];
63+
errors = validateEmptyURL(options._all.url, errors);
64+
errors = validateURLFormat(options._all.url, errors);
65+
errors = validateConcurrency(options._all.concurrency, errors);
66+
errors = validateNesting(options._all.nesting, errors);
67+
return errors;
5768
};
69+

spiderpromises2/spider.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,16 @@ function spider( url, nesting)
6363
);
6464
}
6565

66-
if (!validator.validate())
67-
process.exit();
66+
const errors = validator.validate();
67+
if (errors.length || cmdConfig.get("help"))
68+
{
69+
console.log(cmdConfig.usage);
70+
errors.forEach((err) =>
71+
{
72+
console.error(err);
73+
});
74+
process.exit(errors.length);
75+
}
6876

6977
spider(cmdConfig.get("url"),cmdConfig.get("nesting",1)).
7078
then( function() {

0 commit comments

Comments
 (0)