Skip to content

Commit 08c9e7c

Browse files
committed
Added doc URL to all invalid arg errors
1 parent fe9fdd9 commit 08c9e7c

File tree

1 file changed

+53
-24
lines changed

1 file changed

+53
-24
lines changed

generate-ip/src/generate-ip.js

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,16 @@ const ipv4 = {
2121

2222
generate: function(options = {}) {
2323

24-
const exampleCall = 'ipv4.generate({ verbose: false, qty: 3 })';
24+
const docURL = 'https://github.com/adamlui/js-utils/tree/main/generate-ip#ipv4generateoptions',
25+
exampleCall = 'ipv4.generate({ verbose: false, qty: 3 })';
26+
2527
const defaultOptions = {
2628
verbose: true, // enable logging
2729
qty: 1 // number of IP addresses to generate
2830
};
2931

3032
// Validate/init options
31-
if (!validateOptions(options, defaultOptions, exampleCall)) return;
33+
if (!validateOptions(options, defaultOptions, docURL, exampleCall)) return;
3234
options = { ...defaultOptions, ...options }; // merge validated options w/ missing default ones
3335

3436
// Generate IPv4 address(es)
@@ -59,15 +61,19 @@ const ipv4 = {
5961

6062
validate: function(address, options = {}) {
6163

62-
const exampleCall = 'ipv4.validate(\'0.0.255.255\', { verbose: false })',
64+
const docURL = 'https://github.com/adamlui/js-utils/tree/main/generate-ip#ipv4validateaddress-options',
65+
exampleCall = 'ipv4.validate(\'0.0.255.255\', { verbose: false })',
6366
defaultOptions = { verbose: true /* enable logging */ };
6467

6568
// Validate address as arg
66-
if (typeof address !== 'string') return console.error(
67-
'ipv4.validate() » ERROR: 1st arg <address> must be a string.');
69+
if (typeof address !== 'string') {
70+
console.error('ipv4.validate() » ERROR: 1st arg <address> must be a string.');
71+
console.info('ipv4.validate() » For more help, please visit ' + docURL);
72+
return;
73+
}
6874

6975
// Validate/init options
70-
if (!validateOptions(options, defaultOptions, exampleCall)) return;
76+
if (!validateOptions(options, defaultOptions, docURL, exampleCall)) return;
7177
options = { ...defaultOptions, ...options }; // merge validated options w/ missing default ones
7278

7379
// Validate address as IPv4 address
@@ -93,7 +99,9 @@ const ipv6 = {
9399

94100
generate: function(options = {}) {
95101

96-
const exampleCall = 'ipv6.generate({ leadingZeros: true, qty: 5 })';
102+
const docURL = 'https://github.com/adamlui/js-utils/tree/main/generate-ip#ipv6generateoptions',
103+
exampleCall = 'ipv6.generate({ leadingZeros: true, qty: 5 })';
104+
97105
const defaultOptions = {
98106
verbose: true, // enable logging
99107
qty: 1, // number of IP addresses to generate
@@ -102,7 +110,7 @@ const ipv6 = {
102110
};
103111

104112
// Validate/init options
105-
if (!validateOptions(options, defaultOptions, exampleCall)) return;
113+
if (!validateOptions(options, defaultOptions, docURL, exampleCall)) return;
106114
options = { ...defaultOptions, ...options }; // merge validated options w/ missing default ones
107115

108116
// Generate IPv6 address(es)
@@ -134,22 +142,31 @@ const ipv6 = {
134142
},
135143

136144
format: function(address, options = {}) {
137-
const exampleCall = 'ipv6.format(\'0d::ffff:192.1.56.10/96\', '
145+
146+
const docURL = 'https://github.com/adamlui/js-utils/tree/main/generate-ip#ipv6formataddress-options',
147+
exampleCall = 'ipv6.format(\'0d::ffff:192.1.56.10/96\', '
138148
+ '{ leadingZeros: true, doubleColon: false })';
149+
139150
const defaultOptions = {
140151
verbose: true, // enable logging
141152
leadingZeros: false, // include leading zeros in hex pieces
142153
doubleColon: true // replace series of zeros w/ '::'
143154
};
144155

145156
// Validate address
146-
if (typeof address !== 'string') return console.error(
147-
'ipv6.format() » ERROR: 1st arg <address> must be a string.');
148-
if (!this.validate(address, { verbose: false})) return console.error(
149-
`ipv6.format() » ERROR: ${ address } is not a valid IPv6 address.`);
157+
if (typeof address !== 'string') {
158+
console.error('ipv6.format() » ERROR: 1st arg <address> must be a string.');
159+
console.info('ipv6.format() » For more help, please visit ' + docURL);
160+
return;
161+
}
162+
if (!this.validate(address, { verbose: false})) {
163+
console.error(`ipv6.format() » ERROR: ${ address } is not a valid IPv6 address.`);
164+
console.info('ipv6.format() » For more help, please visit ' + docURL);
165+
return;
166+
}
150167

151168
// Validate/init options
152-
if (!validateOptions(options, defaultOptions, exampleCall)) return;
169+
if (!validateOptions(options, defaultOptions, docURL, exampleCall)) return;
153170
options = { ...defaultOptions, ...options }; // merge validated options w/ missing default ones
154171

155172
// Init formattedAddress
@@ -197,15 +214,19 @@ const ipv6 = {
197214

198215
validate: function(address, options = {}) {
199216

200-
const exampleCall = 'ipv6.validate(\'0:0:0:0:0:ffff:192.1.56.10/96\', { verbose: false })',
217+
const docURL = 'https://github.com/adamlui/js-utils/tree/main/generate-ip#ipv6validateaddress-options',
218+
exampleCall = 'ipv6.validate(\'0:0:0:0:0:ffff:192.1.56.10/96\', { verbose: false })',
201219
defaultOptions = { verbose: true /* enable logging */ };
202220

203221
// Validate address as arg
204-
if (typeof address !== 'string') return console.error(
205-
'ipv6.validate() » ERROR: 1st arg <address> must be a string.');
222+
if (typeof address !== 'string') {
223+
console.error('ipv6.validate() » ERROR: 1st arg <address> must be a string.');
224+
console.info('ipv6.validate() » For more help, please visit ' + docURL);
225+
return;
226+
}
206227

207228
// Validate/init options
208-
if (!validateOptions(options, defaultOptions, exampleCall)) return;
229+
if (!validateOptions(options, defaultOptions, docURL, exampleCall)) return;
209230
options = { ...defaultOptions, ...options }; // merge validated options w/ missing default ones
210231

211232
// Validate address as IPv6 address
@@ -235,38 +256,46 @@ const ipv6 = {
235256

236257
// Define INTERNAL validation function
237258

238-
function validateOptions(options, defaultOptions, exampleCall) {
239-
const logPrefix = ( validateOptions.caller?.name || 'validateOptions' ) + '() » ';
259+
function validateOptions(options, defaultOptions, docURL, exampleCall) {
260+
261+
// Init option strings/types
240262
const strDefaultOptions = JSON.stringify(defaultOptions, null, 2)
241263
.replace(/"([^"]+)":/g, '$1:') // strip quotes from keys
242264
.replace(/"/g, '\'') // replace double quotes w/ single quotes
243265
.replace(/\n\s*/g, ' '); // condense to single line
244266
const strValidOptions = Object.keys(defaultOptions).join(', '),
245267
booleanOptions = Object.keys(defaultOptions).filter(key => typeof defaultOptions[key] === 'boolean'),
246268
integerOptions = Object.keys(defaultOptions).filter(key => Number.isInteger(defaultOptions[key]));
269+
270+
// Define print functions
271+
const logPrefix = ( validateOptions.caller?.name || 'validateOptions' ) + '() » ';
247272
const printValidOptions = () => {
248273
console.info(`${ logPrefix }Valid options: [ ${ strValidOptions } ]`);
249274
console.info(`${ logPrefix }If omitted, default settings are: ${ strDefaultOptions }`);
250275
};
276+
const printDocURL = () => {
277+
console.info(`${ logPrefix }For more help, please visit ${docURL}`); };
278+
279+
// Validate options
251280
if (typeof options != 'object') { // validate as obj
252281
console.error(`${ logPrefix }ERROR: [options] can only be an object of key/values.`);
253282
console.info(`${ logPrefix }Example valid call: ${ exampleCall }`);
254-
printValidOptions(); return false;
283+
printValidOptions(); printDocURL(); return false;
255284
}
256285
for (const key in options) { // validate each key
257286
if (!Object.prototype.hasOwnProperty.call(defaultOptions, key)) {
258287
console.error(
259288
`${ logPrefix }ERROR: \`${ key }\` is an invalid option.`);
260-
printValidOptions(); return false;
289+
printValidOptions(); printDocURL(); return false;
261290
} else if (booleanOptions.includes(key) && typeof options[key] !== 'boolean') {
262291
console.error(
263292
`${ logPrefix }ERROR: [${ key }] option can only be \`true\` or \`false\`.`);
264-
return false;
293+
printDocURL(); return false;
265294
} else if (integerOptions.includes(key)) {
266295
options[key] = parseInt(options[key], 10);
267296
if (isNaN(options[key]) || options[key] < 1) {
268297
console.error(`${ logPrefix }ERROR: [${ key }] option can only be an integer > 0.`);
269-
return false;
298+
printDocURL(); return false;
270299
}
271300
}
272301
}

0 commit comments

Comments
 (0)