Skip to content

Commit 9702e32

Browse files
committed
fix(faker-js#113): Adds regex to random.word to trim unwanted characters
1 parent a973ee1 commit 9702e32

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/random.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,15 @@ export class Random {
199199
'name.jobArea',
200200
'name.jobType',
201201
];
202+
// regex statement used to remove unwanted characters from beginning/end of words
203+
const wordSanitizerRegex = /^[\s()\[\]{}.,\-'"]+|[\s()\[\]{}.,\-'"]+$/g;
202204

203205
// randomly pick from the many faker methods that can generate words
204206
const randomWordMethod = this.faker.random.arrayElement(wordMethods);
205207
const result = this.faker.fake('{{' + randomWordMethod + '}}');
206-
return this.faker.random.arrayElement(result.split(' '));
208+
209+
// sanitize and return word
210+
return this.faker.random.arrayElement(result.split(' ')).replace(wordSanitizerRegex, "");
207211
}
208212

209213
readonly randomWord: Random['word'] = this.word.bind(this);

0 commit comments

Comments
 (0)