diff options
| author | ST-DDT <[email protected]> | 2022-04-10 12:15:35 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-04-10 10:15:35 +0000 |
| commit | dd11846bd9c61cc09917a06ec231592fff3ec653 (patch) | |
| tree | 971d7e58e516296b0549579bbd6ca6d4a4ca62f0 /src | |
| parent | 612fc3885f5a22606d6813fc189f317619570992 (diff) | |
| download | faker-dd11846bd9c61cc09917a06ec231592fff3ec653.tar.xz faker-dd11846bd9c61cc09917a06ec231592fff3ec653.zip | |
feat: immutable options in random.alpha methods (#790)
Diffstat (limited to 'src')
| -rw-r--r-- | src/random.ts | 51 |
1 files changed, 19 insertions, 32 deletions
diff --git a/src/random.ts b/src/random.ts index 667b7c89..96128dd5 100644 --- a/src/random.ts +++ b/src/random.ts @@ -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); }); @@ -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: | 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', @@ -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; } /** @@ -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', @@ -523,9 +511,7 @@ export class Random { 'z', ]; - if (options.bannedChars) { - charsArray = arrayRemove(charsArray, options.bannedChars); - } + charsArray = arrayRemove(charsArray, bannedChars); if (charsArray.length === 0) { throw new FakerError( @@ -533,6 +519,7 @@ export class Random { ); } + let wholeString = ''; for (let i = 0; i < count; i++) { wholeString += this.arrayElement(charsArray); } |
