Skip to content

Commit

Permalink
fix: only return word with desirable alpha characters (#654)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 authored Mar 24, 2022
1 parent 0924e85 commit b7b2e4f
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 15 deletions.
34 changes: 30 additions & 4 deletions src/random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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(' '));
}

Expand Down
60 changes: 59 additions & 1 deletion test/random.spec.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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();

Expand Down
23 changes: 13 additions & 10 deletions test/system.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
},
Expand All @@ -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',
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit b7b2e4f

Please sign in to comment.