Skip to content

Commit c51fb15

Browse files
fix: alphaNumeric all chars banned (#550)
1 parent 5a15c40 commit c51fb15

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

src/random.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -479,11 +479,17 @@ export class Random {
479479
'y',
480480
'z',
481481
];
482-
if (options) {
483-
if (options.bannedChars) {
484-
charsArray = arrayRemove(charsArray, options.bannedChars);
485-
}
482+
483+
if (options.bannedChars) {
484+
charsArray = arrayRemove(charsArray, options.bannedChars);
486485
}
486+
487+
if (charsArray.length === 0) {
488+
throw new Error(
489+
'Unable to generate string, because all possible characters are banned.'
490+
);
491+
}
492+
487493
for (let i = 0; i < count; i++) {
488494
wholeString += this.faker.random.arrayElement(charsArray);
489495
}

test/random.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ describe('random', () => {
210210
expect(alphaText).match(/[b-oq-z]/);
211211
});
212212

213-
it.todo('should throw if all possible characters being banned', () => {
213+
it('should throw if all possible characters being banned', () => {
214214
const bannedChars = 'abcdefghijklmnopqrstuvwxyz0123456789'.split('');
215215
expect(() =>
216216
faker.random.alphaNumeric(5, {

0 commit comments

Comments
 (0)