Skip to content

Commit d0635eb

Browse files
committed
feat(helpers): allow empty string in fake
1 parent 4ce8e98 commit d0635eb

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

src/modules/helpers/index.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ export class HelpersModule {
543543
*/
544544
fake(str: string): string {
545545
// if incoming str parameter is not provided, return error message
546-
if (typeof str !== 'string' || str.length === 0) {
546+
if (typeof str !== 'string') {
547547
throw new FakerError('string parameter is required!');
548548
}
549549

@@ -617,10 +617,6 @@ export class HelpersModule {
617617
// We cannot use string.replace here because the result might contain evaluated characters
618618
const res = str.substring(0, start) + result + str.substring(end + 2);
619619

620-
if (res === '') {
621-
return '';
622-
}
623-
624620
// return the response recursively until we are done finding all tags
625621
return this.fake(res);
626622
}

test/__snapshots__/helpers.spec.ts.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ exports[`helpers > 42 > arrayElements > with array and count 1`] = `
3131

3232
exports[`helpers > 42 > fake > with args 1`] = `"my string: Cky2eiXX/J"`;
3333

34+
exports[`helpers > 42 > fake > with empty string 1`] = `""`;
35+
3436
exports[`helpers > 42 > fake > with plain string 1`] = `"my test string"`;
3537

3638
exports[`helpers > 42 > maybe > with only value 1`] = `"Hello World!"`;
@@ -210,6 +212,8 @@ exports[`helpers > 1211 > arrayElements > with array and count 1`] = `
210212

211213
exports[`helpers > 1211 > fake > with args 1`] = `"my string: wKti5-}$_/"`;
212214

215+
exports[`helpers > 1211 > fake > with empty string 1`] = `""`;
216+
213217
exports[`helpers > 1211 > fake > with plain string 1`] = `"my test string"`;
214218

215219
exports[`helpers > 1211 > maybe > with only value 1`] = `undefined`;
@@ -385,6 +389,8 @@ exports[`helpers > 1337 > arrayElements > with array and count 1`] = `
385389

386390
exports[`helpers > 1337 > fake > with args 1`] = `"my string: 9U/4:SK$>6"`;
387391

392+
exports[`helpers > 1337 > fake > with empty string 1`] = `""`;
393+
388394
exports[`helpers > 1337 > fake > with plain string 1`] = `"my test string"`;
389395

390396
exports[`helpers > 1337 > maybe > with only value 1`] = `"Hello World!"`;

test/helpers.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ describe('helpers', () => {
9696
});
9797

9898
t.describe('fake', (t) => {
99+
t.it('with empty string', '');
99100
t.it('with plain string', 'my test string').it(
100101
'with args',
101102
'my string: {{datatype.string}}'
@@ -551,6 +552,11 @@ describe('helpers', () => {
551552
});
552553

553554
describe('fake()', () => {
555+
it('should take an empty string', () => {
556+
const actual = faker.helpers.fake('');
557+
expect(actual).toBe('');
558+
});
559+
554560
it('replaces a token with a random value for a method without parentheses', () => {
555561
const actual = faker.helpers.fake('{{string.numeric}}');
556562
expect(actual).toMatch(/^\d$/);

0 commit comments

Comments
 (0)