@@ -213,6 +213,16 @@ describe('random', () => {
213213 expect ( actual ) . toMatch ( / ^ [ b - o q - z ] { 5 } $ / ) ;
214214 } ) ;
215215
216+ it ( 'should be able to ban some characters via string' , ( ) => {
217+ const actual = faker . random . alpha ( {
218+ count : 5 ,
219+ bannedChars : 'ap' ,
220+ } ) ;
221+
222+ expect ( actual ) . toHaveLength ( 5 ) ;
223+ expect ( actual ) . toMatch ( / ^ [ b - o q - z ] { 5 } $ / ) ;
224+ } ) ;
225+
216226 it ( 'should be able handle mistake in banned characters array' , ( ) => {
217227 const alphaText = faker . random . alpha ( {
218228 count : 5 ,
@@ -296,6 +306,18 @@ describe('random', () => {
296306 }
297307 } ) ;
298308
309+ it ( 'should be able to ban all alphabetic characters via string' , ( ) => {
310+ const bannedChars = 'abcdefghijklmnopqrstuvwxyz' ;
311+ const alphaText = faker . random . alphaNumeric ( 5 , {
312+ bannedChars,
313+ } ) ;
314+
315+ expect ( alphaText ) . toHaveLength ( 5 ) ;
316+ for ( const bannedChar of bannedChars ) {
317+ expect ( alphaText ) . not . includes ( bannedChar ) ;
318+ }
319+ } ) ;
320+
299321 it ( 'should be able to ban all numeric characters' , ( ) => {
300322 const bannedChars = '0123456789' . split ( '' ) ;
301323 const alphaText = faker . random . alphaNumeric ( 5 , {
@@ -308,6 +330,18 @@ describe('random', () => {
308330 }
309331 } ) ;
310332
333+ it ( 'should be able to ban all numeric characters via string' , ( ) => {
334+ const bannedChars = '0123456789' ;
335+ const alphaText = faker . random . alphaNumeric ( 5 , {
336+ bannedChars,
337+ } ) ;
338+
339+ expect ( alphaText ) . toHaveLength ( 5 ) ;
340+ for ( const bannedChar of bannedChars ) {
341+ expect ( alphaText ) . not . includes ( bannedChar ) ;
342+ }
343+ } ) ;
344+
311345 it ( 'should be able to handle mistake in banned characters array' , ( ) => {
312346 const alphaText = faker . random . alphaNumeric ( 5 , {
313347 bannedChars : [ 'a' , 'p' , 'a' ] ,
@@ -330,6 +364,15 @@ describe('random', () => {
330364 ) ;
331365 } ) ;
332366
367+ it ( 'should throw if all possible characters being banned via string' , ( ) => {
368+ const bannedChars = 'abcdefghijklmnopqrstuvwxyz0123456789' ;
369+ expect ( ( ) =>
370+ faker . random . alphaNumeric ( 5 , {
371+ bannedChars,
372+ } )
373+ ) . toThrowError ( ) ;
374+ } ) ;
375+
333376 it ( 'should not mutate the input object' , ( ) => {
334377 const input : {
335378 bannedChars : string [ ] ;
@@ -395,6 +438,15 @@ describe('random', () => {
395438 expect ( actual ) . toBe ( '0000' ) ;
396439 } ) ;
397440
441+ it ( 'should allow leading zeros via option and all other digits banned via string' , ( ) => {
442+ const actual = faker . random . numeric ( 4 , {
443+ allowLeadingZeros : true ,
444+ bannedDigits : '123456789' ,
445+ } ) ;
446+
447+ expect ( actual ) . toBe ( '0000' ) ;
448+ } ) ;
449+
398450 it ( 'should fail on leading zeros via option and all other digits banned' , ( ) => {
399451 expect ( ( ) =>
400452 faker . random . numeric ( 4 , {
@@ -408,6 +460,19 @@ describe('random', () => {
408460 ) ;
409461 } ) ;
410462
463+ it ( 'should fail on leading zeros via option and all other digits banned via string' , ( ) => {
464+ expect ( ( ) =>
465+ faker . random . numeric ( 4 , {
466+ allowLeadingZeros : false ,
467+ bannedDigits : '123456789' ,
468+ } )
469+ ) . toThrowError (
470+ new FakerError (
471+ 'Unable to generate numeric string, because all possible digits are banned.'
472+ )
473+ ) ;
474+ } ) ;
475+
411476 it ( 'should ban all digits passed via bannedDigits' , ( ) => {
412477 const actual = faker . random . numeric ( 1000 , {
413478 bannedDigits : 'c84U1' . split ( '' ) ,
@@ -416,6 +481,15 @@ describe('random', () => {
416481 expect ( actual ) . toHaveLength ( 1000 ) ;
417482 expect ( actual ) . toMatch ( / ^ [ 0 2 3 5 6 7 9 ] { 1000 } $ / ) ;
418483 } ) ;
484+
485+ it ( 'should ban all digits passed via bannedDigits via string' , ( ) => {
486+ const actual = faker . random . numeric ( 1000 , {
487+ bannedDigits : 'c84U1' ,
488+ } ) ;
489+
490+ expect ( actual ) . toHaveLength ( 1000 ) ;
491+ expect ( actual ) . toMatch ( / ^ [ 0 2 3 5 6 7 9 ] { 1000 } $ / ) ;
492+ } ) ;
419493 } ) ;
420494 } ) ;
421495 } ) ;
0 commit comments