Skip to content

Commit b7b2e4f

Browse files
authored
fix: only return word with desirable alpha characters (#654)
1 parent 0924e85 commit b7b2e4f

File tree

3 files changed

+102
-15
lines changed

3 files changed

+102
-15
lines changed

src/random.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,6 @@ export class Random {
242242
* @example
243243
* faker.random.word() // 'Seamless'
244244
*/
245-
// TODO: have ability to return specific type of word? As in: noun, adjective, verb, etc
246245
word(): string {
247246
const wordMethods = [
248247
'commerce.department',
@@ -278,9 +277,36 @@ export class Random {
278277
'name.jobType',
279278
];
280279

281-
// randomly pick from the many faker methods that can generate words
282-
const randomWordMethod = this.faker.random.arrayElement(wordMethods);
283-
const result = this.faker.fake('{{' + randomWordMethod + '}}');
280+
const bannedChars = [
281+
'!',
282+
'#',
283+
'%',
284+
'&',
285+
'*',
286+
')',
287+
'(',
288+
'+',
289+
'=',
290+
'.',
291+
'<',
292+
'>',
293+
'{',
294+
'}',
295+
'[',
296+
']',
297+
':',
298+
';',
299+
"'",
300+
'"',
301+
'_',
302+
'-',
303+
];
304+
let result: string;
305+
do {
306+
// randomly pick from the many faker methods that can generate words
307+
const randomWordMethod = this.faker.random.arrayElement(wordMethods);
308+
result = this.faker.fake('{{' + randomWordMethod + '}}');
309+
} while (bannedChars.some((char) => result.includes(char)));
284310
return this.faker.random.arrayElement(result.split(' '));
285311
}
286312

test/random.spec.ts

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { describe, expect, it, vi } from 'vitest';
1+
import { beforeEach, describe, expect, it, vi } from 'vitest';
22
import { faker } from '../src';
3+
import { times } from './support/times';
34

45
describe('random', () => {
56
describe('arrayElement', () => {
@@ -78,15 +79,72 @@ describe('random', () => {
7879
});
7980

8081
describe('word', () => {
82+
const bannedChars = [
83+
'!',
84+
'#',
85+
'%',
86+
'&',
87+
'*',
88+
')',
89+
'(',
90+
'+',
91+
'=',
92+
'.',
93+
'<',
94+
'>',
95+
'{',
96+
'}',
97+
'[',
98+
']',
99+
':',
100+
';',
101+
"'",
102+
'"',
103+
'_',
104+
'-',
105+
];
106+
107+
beforeEach(() => {
108+
faker.locale = 'en';
109+
});
110+
81111
it('should return a random word', () => {
82112
const actual = faker.random.word();
83113

84114
expect(actual).toBeTruthy();
85115
expect(actual).toBeTypeOf('string');
86116
});
117+
118+
it.each(times(50))(
119+
'should only contain a word without undesirable non-alpha characters (run %i)',
120+
() => {
121+
const actual = faker.random.word();
122+
123+
expect(actual).not.satisfy((word: string) =>
124+
bannedChars.some((char) => word.includes(char))
125+
);
126+
}
127+
);
128+
129+
it.each(times(50))(
130+
'should only contain a word without undesirable non-alpha characters, locale=zh_CN (run %i)',
131+
() => {
132+
faker.locale = 'zh_CN';
133+
134+
const actual = faker.random.word();
135+
136+
expect(actual).not.satisfy((word: string) =>
137+
bannedChars.some((char) => word.includes(char))
138+
);
139+
}
140+
);
87141
});
88142

89143
describe('words', () => {
144+
beforeEach(() => {
145+
faker.locale = 'en';
146+
});
147+
90148
it('should return random words', () => {
91149
const actual = faker.random.words();
92150

test/system.spec.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const seededRuns = [
1616
fileExt: 'chm',
1717
directoryPath: '/opt/bin',
1818
// TODO @prisis 2022-01-25: add a parameter to have the possibility to have one or two ext on file.
19-
filePath: '/opt/bin/directives_multi_byte_table.p10.m21',
19+
filePath: '/opt/bin/directives_savings_computer.qwd.jade',
2020
semver: '3.7.9',
2121
},
2222
},
@@ -39,8 +39,8 @@ const seededRuns = [
3939
{
4040
seed: 1211,
4141
expectations: {
42-
fileName: 'turnpike_cross_platform_handcrafted.mka',
43-
commonFileName: 'turnpike_cross_platform_handcrafted.mp4v',
42+
fileName: 'turnpike_supervisor_chicken.mka',
43+
commonFileName: 'turnpike_supervisor_chicken.mp4v',
4444
mimeType: 'text/vnd.fmi.flexstor',
4545
commonFileType: 'application',
4646
commonFileExt: 'htm',
@@ -98,18 +98,21 @@ describe('system', () => {
9898
it('should return common file types', () => {
9999
const fileExt = faker.system.commonFileExt();
100100
const extList = [
101-
'pdf',
102-
'mpeg',
103-
'wav',
104-
'png',
105-
'jpeg',
106101
'gif',
107-
'mp4v',
108-
'mpeg',
109102
'htm',
103+
'html',
104+
'jpeg',
110105
'm2a',
106+
'm2v',
107+
'm3a',
111108
'mp4',
109+
'mp4v',
110+
'mpeg',
112111
'mpg',
112+
'pdf',
113+
'png',
114+
'shtml',
115+
'wav',
113116
];
114117

115118
expect(

0 commit comments

Comments
 (0)