@@ -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 @Shinigami 92 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 @Shinigami 92 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 }
0 commit comments