Skip to content
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

feat: immutable options in random.alpha methods #790

Merged
merged 7 commits into from
Apr 10, 2022
Merged
Show file tree
Hide file tree
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
51 changes: 19 additions & 32 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,30 +400,21 @@ export class Random {
*/
// TODO @Shinigami92 2022-02-14: Tests covered `(count, options)`, but they were never typed like that
alpha(
options?:
options:
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
| number
| { count?: number; upcase?: boolean; bannedChars?: string[] }
| {
count?: number;
upcase?: boolean;
bannedChars?: readonly string[];
} = {}
): 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 = 1, upcase = false, bannedChars = [] } = options;

let wholeString = '';
let charsArray = [
'a',
'b',
Expand Down Expand Up @@ -452,15 +443,15 @@ 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);

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

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

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

let wholeString = '';
let charsArray = [
'0',
'1',
Expand Down Expand Up @@ -523,16 +511,15 @@ export class Random {
'z',
];

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

if (charsArray.length === 0) {
throw new FakerError(
'Unable to generate string, because all possible characters are banned.'
);
}

let wholeString = '';
for (let i = 0; i < count; i++) {
wholeString += this.arrayElement(charsArray);
}
Expand Down
26 changes: 26 additions & 0 deletions test/random.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,21 @@ describe('random', () => {
expect(alphaText).toHaveLength(5);
expect(alphaText).match(/^[b-oq-z]{5}$/);
});

it('should not mutate the input object', () => {
const input: {
count: number;
upcase: boolean;
bannedChars: string[];
} = Object.freeze({
count: 5,
upcase: true,
bannedChars: ['a', '%'],
});

expect(() => faker.random.alpha(input)).not.toThrow();
expect(input.bannedChars).toEqual(['a', '%']);
});
});

describe('alphaNumeric', () => {
Expand Down Expand Up @@ -276,6 +291,17 @@ describe('random', () => {
})
).toThrowError();
});

it('should not mutate the input object', () => {
const input: {
bannedChars: string[];
} = Object.freeze({
bannedChars: ['a', '0', '%'],
});

expect(() => faker.random.alphaNumeric(5, input)).not.toThrow();
expect(input.bannedChars).toEqual(['a', '0', '%']);
});
});

describe('deprecation warnings', () => {
Expand Down