aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShinigami <[email protected]>2022-03-24 12:39:37 +0100
committerGitHub <[email protected]>2022-03-24 11:39:37 +0000
commitb7b2e4f8dbd40b6bb2678fb60ee95e198838d08a (patch)
tree2fa52212f1520d5d0610ff1ddd92784489c4b270
parent0924e852d300e012c80d74fb94b57168965827bf (diff)
downloadfaker-b7b2e4f8dbd40b6bb2678fb60ee95e198838d08a.tar.xz
faker-b7b2e4f8dbd40b6bb2678fb60ee95e198838d08a.zip
fix: only return word with desirable alpha characters (#654)
-rw-r--r--src/random.ts34
-rw-r--r--test/random.spec.ts60
-rw-r--r--test/system.spec.ts23
3 files changed, 102 insertions, 15 deletions
diff --git a/src/random.ts b/src/random.ts
index b84a2ece..0658cdd5 100644
--- a/src/random.ts
+++ b/src/random.ts
@@ -242,7 +242,6 @@ export class Random {
* @example
* faker.random.word() // 'Seamless'
*/
- // TODO: have ability to return specific type of word? As in: noun, adjective, verb, etc
word(): string {
const wordMethods = [
'commerce.department',
@@ -278,9 +277,36 @@ export class Random {
'name.jobType',
];
- // randomly pick from the many faker methods that can generate words
- const randomWordMethod = this.faker.random.arrayElement(wordMethods);
- const result = this.faker.fake('{{' + randomWordMethod + '}}');
+ const bannedChars = [
+ '!',
+ '#',
+ '%',
+ '&',
+ '*',
+ ')',
+ '(',
+ '+',
+ '=',
+ '.',
+ '<',
+ '>',
+ '{',
+ '}',
+ '[',
+ ']',
+ ':',
+ ';',
+ "'",
+ '"',
+ '_',
+ '-',
+ ];
+ let result: string;
+ do {
+ // randomly pick from the many faker methods that can generate words
+ const randomWordMethod = this.faker.random.arrayElement(wordMethods);
+ result = this.faker.fake('{{' + randomWordMethod + '}}');
+ } while (bannedChars.some((char) => result.includes(char)));
return this.faker.random.arrayElement(result.split(' '));
}
diff --git a/test/random.spec.ts b/test/random.spec.ts
index f43cc500..ceb454cc 100644
--- a/test/random.spec.ts
+++ b/test/random.spec.ts
@@ -1,5 +1,6 @@
-import { describe, expect, it, vi } from 'vitest';
+import { beforeEach, describe, expect, it, vi } from 'vitest';
import { faker } from '../src';
+import { times } from './support/times';
describe('random', () => {
describe('arrayElement', () => {
@@ -78,15 +79,72 @@ describe('random', () => {
});
describe('word', () => {
+ const bannedChars = [
+ '!',
+ '#',
+ '%',
+ '&',
+ '*',
+ ')',
+ '(',
+ '+',
+ '=',
+ '.',
+ '<',
+ '>',
+ '{',
+ '}',
+ '[',
+ ']',
+ ':',
+ ';',
+ "'",
+ '"',
+ '_',
+ '-',
+ ];
+
+ beforeEach(() => {
+ faker.locale = 'en';
+ });
+
it('should return a random word', () => {
const actual = faker.random.word();
expect(actual).toBeTruthy();
expect(actual).toBeTypeOf('string');
});
+
+ it.each(times(50))(
+ 'should only contain a word without undesirable non-alpha characters (run %i)',
+ () => {
+ const actual = faker.random.word();
+
+ expect(actual).not.satisfy((word: string) =>
+ bannedChars.some((char) => word.includes(char))
+ );
+ }
+ );
+
+ it.each(times(50))(
+ 'should only contain a word without undesirable non-alpha characters, locale=zh_CN (run %i)',
+ () => {
+ faker.locale = 'zh_CN';
+
+ const actual = faker.random.word();
+
+ expect(actual).not.satisfy((word: string) =>
+ bannedChars.some((char) => word.includes(char))
+ );
+ }
+ );
});
describe('words', () => {
+ beforeEach(() => {
+ faker.locale = 'en';
+ });
+
it('should return random words', () => {
const actual = faker.random.words();
diff --git a/test/system.spec.ts b/test/system.spec.ts
index 7c6e7760..b9d36edc 100644
--- a/test/system.spec.ts
+++ b/test/system.spec.ts
@@ -16,7 +16,7 @@ const seededRuns = [
fileExt: 'chm',
directoryPath: '/opt/bin',
// TODO @prisis 2022-01-25: add a parameter to have the possibility to have one or two ext on file.
- filePath: '/opt/bin/directives_multi_byte_table.p10.m21',
+ filePath: '/opt/bin/directives_savings_computer.qwd.jade',
semver: '3.7.9',
},
},
@@ -39,8 +39,8 @@ const seededRuns = [
{
seed: 1211,
expectations: {
- fileName: 'turnpike_cross_platform_handcrafted.mka',
- commonFileName: 'turnpike_cross_platform_handcrafted.mp4v',
+ fileName: 'turnpike_supervisor_chicken.mka',
+ commonFileName: 'turnpike_supervisor_chicken.mp4v',
mimeType: 'text/vnd.fmi.flexstor',
commonFileType: 'application',
commonFileExt: 'htm',
@@ -98,18 +98,21 @@ describe('system', () => {
it('should return common file types', () => {
const fileExt = faker.system.commonFileExt();
const extList = [
- 'pdf',
- 'mpeg',
- 'wav',
- 'png',
- 'jpeg',
'gif',
- 'mp4v',
- 'mpeg',
'htm',
+ 'html',
+ 'jpeg',
'm2a',
+ 'm2v',
+ 'm3a',
'mp4',
+ 'mp4v',
+ 'mpeg',
'mpg',
+ 'pdf',
+ 'png',
+ 'shtml',
+ 'wav',
];
expect(