Skip to content

Commit 00a7075

Browse files
committed
Added doc URL to all invalid arg errors
1 parent 45f1d5c commit 00a7075

File tree

1 file changed

+44
-20
lines changed

1 file changed

+44
-20
lines changed

generate-pw/src/generate-pw.js

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ const charsets = {
2727

2828
function generatePassword(options = {}) {
2929

30-
const exampleCall = 'generatePassword({ verbose: false, numbers: true })';
30+
const docURL = 'https://github.com/adamlui/js-utils/tree/main/generate-pw#generatepasswordoptions',
31+
exampleCall = 'generatePassword({ verbose: false, numbers: true })';
32+
3133
const defaultOptions = {
3234
verbose: true, // enable logging
3335
length: 8, // length of password
@@ -42,7 +44,7 @@ function generatePassword(options = {}) {
4244
};
4345

4446
// Validate/init options
45-
if (!validateOptions(options, defaultOptions, exampleCall)) return;
47+
if (!validateOptions(options, defaultOptions, docURL, exampleCall)) return;
4648
options = { ...defaultOptions, ...options }; // merge validated options w/ missing default ones
4749

4850
if (options.qty > 1) { // generate/return array of [qty] password strings
@@ -98,7 +100,9 @@ function generatePassword(options = {}) {
98100

99101
function generatePasswords(qty, options = {}) {
100102

101-
const exampleCall = 'generatePasswords(3, { verbose: false, symbols: true })';
103+
const docURL = 'https://github.com/adamlui/js-utils/tree/main/generate-pw#generatepasswordsqty-options',
104+
exampleCall = 'generatePasswords(3, { verbose: false, symbols: true })';
105+
102106
const defaultOptions = {
103107
verbose: true, // enable logging
104108
length: 8, // length of password
@@ -113,11 +117,14 @@ function generatePasswords(qty, options = {}) {
113117

114118
// Validate qty
115119
qty = parseInt(qty, 10);
116-
if (isNaN(qty) || qty < 1) return console.error(
117-
'generatePasswords() » ERROR: 1st arg <qty> can only be an integer > 0.');
120+
if (isNaN(qty) || qty < 1) {
121+
console.error('generatePasswords() » ERROR: 1st arg <qty> can only be an integer > 0.');
122+
console.info('generatePasswords() » For more help, please visit ' + docURL);
123+
return;
124+
}
118125

119126
// Validate/init options
120-
if (!validateOptions(options, defaultOptions, exampleCall)) return;
127+
if (!validateOptions(options, defaultOptions, docURL, exampleCall)) return;
121128
options = { ...defaultOptions, ...options }; // merge validated options w/ missing default ones
122129

123130
// Generate passwords
@@ -135,12 +142,16 @@ function generatePasswords(qty, options = {}) {
135142

136143
function strictify(password, requiredCharTypes = ['number', 'symbol', 'lower', 'upper'], options = {}) {
137144

138-
const exampleCall = 'strictify(\'pa55word\', [\'symbol\', \'upper\'], { verbose: false })',
145+
const docURL = 'https://github.com/adamlui/js-utils/tree/main/generate-pw#strictifypassword-requiredchartypes-options',
146+
exampleCall = 'strictify(\'pa55word\', [\'symbol\', \'upper\'], { verbose: false })',
139147
defaultOptions = { verbose: true /* enable logging */ };
140148

141149
// Validate password
142-
if (typeof password !== 'string') return console.error(
143-
'strictify() » ERROR: 1st arg <password> must be a string.');
150+
if (typeof password !== 'string') {
151+
console.error('strictify() » ERROR: 1st arg <password> must be a string.');
152+
console.info('strictify() » For more help, please visit ' + docURL);
153+
return;
154+
}
144155

145156
// Validate requiredCharTypes
146157
const validCharTypes = ['number', 'symbol', 'lower', 'upper'];
@@ -151,11 +162,12 @@ function strictify(password, requiredCharTypes = ['number', 'symbol', 'lower', '
151162
console.error(`strictify() » ERROR: 2nd arg \`${ charType }\` is an invalid character type.`);
152163
console.info(`strictify() » Valid character types: [ ${ validCharTypes.map(type => `'${ type }'`).join(', ') } ]`);
153164
console.info('strictify() » Pass one as a string or more as an array, or all types will be required.');
165+
console.info('strictify() » For more help, please visit ' + docURL);
154166
return;
155167
}}
156168

157169
// Validate/init options
158-
if (!validateOptions(options, defaultOptions, exampleCall)) return;
170+
if (!validateOptions(options, defaultOptions, docURL, exampleCall)) return;
159171
options = { ...defaultOptions, ...options }; // merge validated options w/ missing default ones
160172

161173
// Init mod flags
@@ -201,16 +213,20 @@ function strictify(password, requiredCharTypes = ['number', 'symbol', 'lower', '
201213

202214
function validateStrength(password, options = {}) {
203215

204-
const exampleCall = 'validateStrength(\'pa55word\', { verbose: false })',
216+
const docURL = 'https://github.com/adamlui/js-utils/tree/main/generate-pw#validatestrengthpassword-options',
217+
exampleCall = 'validateStrength(\'pa55word\', { verbose: false })',
205218
strengthCriteria = { minLength: 8, minLower: 1, minUpper: 1, minNumber: 1, minSymbol: 1 },
206219
defaultOptions = { verbose: true /* enable logging */ };
207220

208221
// Validate password
209-
if (typeof password !== 'string') return console.error(
210-
'validateStrength() » ERROR: 1st arg <password> must be a string.');
222+
if (typeof password !== 'string') {
223+
console.error('validateStrength() » ERROR: 1st arg <password> must be a string.');
224+
console.info('validateStrength() » For more help, please visit ' + docURL);
225+
return;
226+
}
211227

212228
// Validate/init options
213-
if (!validateOptions(options, defaultOptions, exampleCall)) return;
229+
if (!validateOptions(options, defaultOptions, docURL, exampleCall)) return;
214230
options = { ...defaultOptions, ...options }; // merge validated options w/ missing default ones
215231

216232
if (options.verbose) console.info('validateStrength() » Validating password strength...');
@@ -249,38 +265,46 @@ function validateStrength(password, options = {}) {
249265

250266
// Define INTERNAL validation function
251267

252-
function validateOptions(options, defaultOptions, exampleCall) {
253-
const logPrefix = ( validateOptions.caller?.name || 'validateOptions' ) + '() » ';
268+
function validateOptions(options, defaultOptions, docURL, exampleCall) {
269+
270+
// Init option strings/types
254271
const strDefaultOptions = JSON.stringify(defaultOptions, null, 2)
255272
.replace(/"([^"]+)":/g, '$1:') // strip quotes from keys
256273
.replace(/"/g, '\'') // replace double quotes w/ single quotes
257274
.replace(/\n\s*/g, ' '); // condense to single line
258275
const strValidOptions = Object.keys(defaultOptions).join(', '),
259276
booleanOptions = Object.keys(defaultOptions).filter(key => typeof defaultOptions[key] === 'boolean'),
260277
integerOptions = Object.keys(defaultOptions).filter(key => Number.isInteger(defaultOptions[key]));
278+
279+
// Define print functions
280+
const logPrefix = ( validateOptions.caller?.name || 'validateOptions' ) + '() » ';
261281
const printValidOptions = () => {
262282
console.info(`${ logPrefix }Valid options: [ ${ strValidOptions } ]`);
263283
console.info(`${ logPrefix }If omitted, default settings are: ${ strDefaultOptions }`);
264284
};
285+
const printDocURL = () => {
286+
console.info(`${ logPrefix }For more help, please visit ${docURL}`); };
287+
288+
// Validate options
265289
if (typeof options != 'object') { // validate as obj
266290
console.error(`${ logPrefix }ERROR: [options] can only be an object of key/values.`);
267291
console.info(`${ logPrefix }Example valid call: ${ exampleCall }`);
268-
printValidOptions(); return false;
292+
printValidOptions(); printDocURL(); return false;
269293
}
270294
for (const key in options) { // validate each key
271295
if (!Object.prototype.hasOwnProperty.call(defaultOptions, key)) {
272296
console.error(
273297
`${ logPrefix }ERROR: \`${ key }\` is an invalid option.`);
274-
printValidOptions(); return false;
298+
printValidOptions(); printDocURL(); return false;
275299
} else if (booleanOptions.includes(key) && typeof options[key] !== 'boolean') {
276300
console.error(
277301
`${ logPrefix }ERROR: [${ key }] option can only be \`true\` or \`false\`.`);
278-
return false;
302+
printDocURL(); return false;
279303
} else if (integerOptions.includes(key)) {
280304
options[key] = parseInt(options[key], 10);
281305
if (isNaN(options[key]) || options[key] < 1) {
282306
console.error(`${ logPrefix }ERROR: [${ key }] option can only be an integer > 0.`);
283-
return false;
307+
printDocURL(); return false;
284308
}
285309
}
286310
}

0 commit comments

Comments
 (0)