aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeyla Jähnig <[email protected]>2022-03-21 14:09:40 +0100
committerGitHub <[email protected]>2022-03-21 13:09:40 +0000
commitc51fb1570669284e14915916636de97b7f644e17 (patch)
treeeca1459573da807c31dcdf3e2e6c4613064c8696
parent5a15c40ec98fc1d39309d875899b88a0b7684bcf (diff)
downloadfaker-c51fb1570669284e14915916636de97b7f644e17.tar.xz
faker-c51fb1570669284e14915916636de97b7f644e17.zip
fix: alphaNumeric all chars banned (#550)
-rw-r--r--src/random.ts14
-rw-r--r--test/random.spec.ts2
2 files changed, 11 insertions, 5 deletions
diff --git a/src/random.ts b/src/random.ts
index c2d8ecb2..dd58c3ce 100644
--- a/src/random.ts
+++ b/src/random.ts
@@ -479,11 +479,17 @@ export class Random {
'y',
'z',
];
- if (options) {
- if (options.bannedChars) {
- charsArray = arrayRemove(charsArray, options.bannedChars);
- }
+
+ if (options.bannedChars) {
+ charsArray = arrayRemove(charsArray, options.bannedChars);
}
+
+ if (charsArray.length === 0) {
+ throw new Error(
+ 'Unable to generate string, because all possible characters are banned.'
+ );
+ }
+
for (let i = 0; i < count; i++) {
wholeString += this.faker.random.arrayElement(charsArray);
}
diff --git a/test/random.spec.ts b/test/random.spec.ts
index 07b2a0f5..f43cc500 100644
--- a/test/random.spec.ts
+++ b/test/random.spec.ts
@@ -210,7 +210,7 @@ describe('random', () => {
expect(alphaText).match(/[b-oq-z]/);
});
- it.todo('should throw if all possible characters being banned', () => {
+ it('should throw if all possible characters being banned', () => {
const bannedChars = 'abcdefghijklmnopqrstuvwxyz0123456789'.split('');
expect(() =>
faker.random.alphaNumeric(5, {