Skip to content

refactor: remove inconsistent defaults from internet.password() #767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 4, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions src/internet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,8 @@ export class Internet {
*
* @param len The length of the password to generate. Defaults to `15`.
* @param memorable Whether the generated password should be memorable. Defaults to `false`.
* @param pattern The pattern that all chars should match should match. Defaults to `/\w/`.
* @param pattern The pattern that all chars should match should match.
* This option will be ignored, if `memorable` is `true`. Defaults to `/\w/`.
* @param prefix The prefix to use. Defaults to `''`.
*
* @example
Expand All @@ -369,15 +370,11 @@ export class Internet {
* faker.internet.password(20, true, /[A-Z]/, 'Hello ') // 'Hello IREOXTDWPERQSB'
*/
password(
len?: number,
memorable?: boolean,
pattern?: RegExp,
prefix?: string
len: number = 15,
memorable: boolean = false,
pattern: RegExp = /\w/,
prefix: string = ''
): string {
len = len || 15;
if (memorable == null) {
memorable = false;
}
/*
* password-generator ( function )
* Copyright(c) 2011-2013 Bermi Ferrer <[email protected]>
Expand All @@ -386,12 +383,11 @@ export class Internet {
const vowel = /[aeiouAEIOU]$/;
const consonant = /[bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ]$/;
const _password = (
length = 10,
memorable = true,
pattern = /\w/,
prefix = ''
length: number,
memorable: boolean,
pattern: RegExp,
prefix: string
): string => {
let char: string;
if (prefix.length >= length) {
return prefix;
}
Expand All @@ -403,14 +399,14 @@ export class Internet {
}
}
const n = this.faker.datatype.number(94) + 33;
char = String.fromCharCode(n);
let char = String.fromCharCode(n);
if (memorable) {
char = char.toLowerCase();
}
if (!char.match(pattern)) {
return _password(length, memorable, pattern, prefix);
}
return _password(length, memorable, pattern, '' + prefix + char);
return _password(length, memorable, pattern, prefix + char);
};
return _password(len, memorable, pattern, prefix);
}
Expand Down