Skip to content

Commit dd11846

Browse files
authored
feat: immutable options in random.alpha methods (#790)
1 parent 612fc38 commit dd11846

2 files changed

Lines changed: 45 additions & 32 deletions

File tree

src/random.ts

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { deprecated } from './internal/deprecated';
99
* @param values array of characters which should be removed
1010
* @returns new array without banned characters
1111
*/
12-
function arrayRemove<T>(arr: T[], values: T[]): T[] {
12+
function arrayRemove<T>(arr: T[], values: readonly T[]): T[] {
1313
values.forEach((value) => {
1414
arr = arr.filter((ele) => ele !== value);
1515
});
@@ -400,30 +400,21 @@ export class Random {
400400
*/
401401
// TODO @Shinigami92 2022-02-14: Tests covered `(count, options)`, but they were never typed like that
402402
alpha(
403-
options?:
403+
options:
404404
| number
405-
| { count?: number; upcase?: boolean; bannedChars?: string[] }
405+
| {
406+
count?: number;
407+
upcase?: boolean;
408+
bannedChars?: readonly string[];
409+
} = {}
406410
): string {
407-
if (options == null) {
408-
options = {
409-
count: 1,
410-
};
411-
} else if (typeof options === 'number') {
411+
if (typeof options === 'number') {
412412
options = {
413413
count: options,
414414
};
415-
} else if (options.count == null) {
416-
options.count = 1;
417-
}
418-
419-
if (options.upcase == null) {
420-
options.upcase = false;
421-
}
422-
if (options.bannedChars == null) {
423-
options.bannedChars = [];
424415
}
416+
const { count = 1, upcase = false, bannedChars = [] } = options;
425417

426-
let wholeString = '';
427418
let charsArray = [
428419
'a',
429420
'b',
@@ -452,15 +443,15 @@ export class Random {
452443
'y',
453444
'z',
454445
];
455-
// TODO @Shinigami92 2022-01-11: A default empty array gets assigned above, we should check the length against 0 or not here
456-
if (options.bannedChars) {
457-
charsArray = arrayRemove(charsArray, options.bannedChars);
458-
}
459-
for (let i = 0; i < options.count; i++) {
446+
447+
charsArray = arrayRemove(charsArray, bannedChars);
448+
449+
let wholeString = '';
450+
for (let i = 0; i < count; i++) {
460451
wholeString += this.arrayElement(charsArray);
461452
}
462453

463-
return options.upcase ? wholeString.toUpperCase() : wholeString;
454+
return upcase ? wholeString.toUpperCase() : wholeString;
464455
}
465456

466457
/**
@@ -477,13 +468,10 @@ export class Random {
477468
*/
478469
alphaNumeric(
479470
count: number = 1,
480-
options: { bannedChars?: string[] } = {}
471+
options: { bannedChars?: readonly string[] } = {}
481472
): string {
482-
if (options.bannedChars == null) {
483-
options.bannedChars = [];
484-
}
473+
const { bannedChars = [] } = options;
485474

486-
let wholeString = '';
487475
let charsArray = [
488476
'0',
489477
'1',
@@ -523,16 +511,15 @@ export class Random {
523511
'z',
524512
];
525513

526-
if (options.bannedChars) {
527-
charsArray = arrayRemove(charsArray, options.bannedChars);
528-
}
514+
charsArray = arrayRemove(charsArray, bannedChars);
529515

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

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

test/random.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,21 @@ describe('random', () => {
220220
expect(alphaText).toHaveLength(5);
221221
expect(alphaText).match(/^[b-oq-z]{5}$/);
222222
});
223+
224+
it('should not mutate the input object', () => {
225+
const input: {
226+
count: number;
227+
upcase: boolean;
228+
bannedChars: string[];
229+
} = Object.freeze({
230+
count: 5,
231+
upcase: true,
232+
bannedChars: ['a', '%'],
233+
});
234+
235+
expect(() => faker.random.alpha(input)).not.toThrow();
236+
expect(input.bannedChars).toEqual(['a', '%']);
237+
});
223238
});
224239

225240
describe('alphaNumeric', () => {
@@ -276,6 +291,17 @@ describe('random', () => {
276291
})
277292
).toThrowError();
278293
});
294+
295+
it('should not mutate the input object', () => {
296+
const input: {
297+
bannedChars: string[];
298+
} = Object.freeze({
299+
bannedChars: ['a', '0', '%'],
300+
});
301+
302+
expect(() => faker.random.alphaNumeric(5, input)).not.toThrow();
303+
expect(input.bannedChars).toEqual(['a', '0', '%']);
304+
});
279305
});
280306

281307
describe('deprecation warnings', () => {

0 commit comments

Comments
 (0)