Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: alphaNumeric all chars banned #550

Merged
merged 4 commits into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion test/random.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down