Skip to content

feat: immutable options in random.alpha methods #790

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 7 commits into from
Apr 10, 2022
Merged
Changes from 2 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
53 changes: 23 additions & 30 deletions src/random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { deprecated } from './internal/deprecated';
* @param values array of characters which should be removed
* @returns new array without banned characters
*/
function arrayRemove<T>(arr: T[], values: T[]): T[] {
function arrayRemove<T>(arr: T[], values: readonly T[]): T[] {
values.forEach((value) => {
arr = arr.filter((ele) => ele !== value);
});
Expand Down Expand Up @@ -400,28 +400,26 @@ export class Random {
*/
// TODO @Shinigami92 2022-02-14: Tests covered `(count, options)`, but they were never typed like that
alpha(
options?:
options:
| number
| { count?: number; upcase?: boolean; bannedChars?: string[] }
| {
count?: number;
upcase?: boolean;
bannedChars?: readonly string[];
} = {
count: 1,
upcase: false,
bannedChars: [],
}
): string {
if (options == null) {
options = {
count: 1,
};
} else if (typeof options === 'number') {
if (typeof options === 'number') {
options = {
count: options,
};
} else if (options.count == null) {
options.count = 1;
}

if (options.upcase == null) {
options.upcase = false;
}
if (options.bannedChars == null) {
options.bannedChars = [];
}
const count = options.count ?? 1;
const upcase = options.upcase ?? false;
const bannedChars = options.bannedChars ?? [];

let wholeString = '';
let charsArray = [
Expand Down Expand Up @@ -452,15 +450,14 @@ export class Random {
'y',
'z',
];
// TODO @Shinigami92 2022-01-11: A default empty array gets assigned above, we should check the length against 0 or not here
if (options.bannedChars) {
charsArray = arrayRemove(charsArray, options.bannedChars);
}
for (let i = 0; i < options.count; i++) {

charsArray = arrayRemove(charsArray, bannedChars);

for (let i = 0; i < count; i++) {
wholeString += this.faker.random.arrayElement(charsArray);
}

return options.upcase ? wholeString.toUpperCase() : wholeString;
return upcase ? wholeString.toUpperCase() : wholeString;
}

/**
Expand All @@ -477,11 +474,9 @@ export class Random {
*/
alphaNumeric(
count: number = 1,
options: { bannedChars?: string[] } = {}
options: { bannedChars?: readonly string[] } = { bannedChars: [] }
): string {
if (options.bannedChars == null) {
options.bannedChars = [];
}
const bannedChars = options.bannedChars ?? [];

let wholeString = '';
let charsArray = [
Expand Down Expand Up @@ -523,9 +518,7 @@ export class Random {
'z',
];

if (options.bannedChars) {
charsArray = arrayRemove(charsArray, options.bannedChars);
}
charsArray = arrayRemove(charsArray, bannedChars);

if (charsArray.length === 0) {
throw new FakerError(
Expand Down