Skip to content

Commit ca62689

Browse files
committed
feat(helpers): allow empty string in fake
1 parent 75a31f6 commit ca62689

File tree

3 files changed

+13
-15
lines changed

3 files changed

+13
-15
lines changed

src/modules/helpers/index.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -645,10 +645,6 @@ export class HelpersModule {
645645
throw new FakerError('Array of pattern strings cannot be empty.');
646646
}
647647
}
648-
// if incoming str parameter is not provided, return error message
649-
if (pattern.length === 0) {
650-
throw new FakerError('Pattern string cannot be empty.');
651-
}
652648

653649
// find first matching {{ and }}
654650
const start = pattern.search(/{{[a-z]/);
@@ -721,10 +717,6 @@ export class HelpersModule {
721717
const res =
722718
pattern.substring(0, start) + result + pattern.substring(end + 2);
723719

724-
if (res === '') {
725-
return '';
726-
}
727-
728720
// return the response recursively until we are done finding all tags
729721
return this.fake(res);
730722
}

test/__snapshots__/helpers.spec.ts.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ exports[`helpers > 42 > fake > with a dynamic template 1`] = `"my string: Cky2ei
3333

3434
exports[`helpers > 42 > fake > with a static template 1`] = `"my test string"`;
3535

36+
exports[`helpers > 42 > fake > with empty string 1`] = `""`;
37+
3638
exports[`helpers > 42 > fake > with multiple dynamic templates 1`] = `"Sandy"`;
3739

3840
exports[`helpers > 42 > fake > with multiple static templates 1`] = `"B"`;
@@ -216,6 +218,8 @@ exports[`helpers > 1211 > fake > with a dynamic template 1`] = `"my string: wKti
216218

217219
exports[`helpers > 1211 > fake > with a static template 1`] = `"my test string"`;
218220

221+
exports[`helpers > 1211 > fake > with empty string 1`] = `""`;
222+
219223
exports[`helpers > 1211 > fake > with multiple dynamic templates 1`] = `"La Crosse"`;
220224

221225
exports[`helpers > 1211 > fake > with multiple static templates 1`] = `"C"`;
@@ -395,6 +399,8 @@ exports[`helpers > 1337 > fake > with a dynamic template 1`] = `"my string: 9U/4
395399

396400
exports[`helpers > 1337 > fake > with a static template 1`] = `"my test string"`;
397401

402+
exports[`helpers > 1337 > fake > with empty string 1`] = `""`;
403+
398404
exports[`helpers > 1337 > fake > with multiple dynamic templates 1`] = `"U/4:SK$>6Q"`;
399405

400406
exports[`helpers > 1337 > fake > with multiple static templates 1`] = `"A"`;

test/helpers.spec.ts

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

9898
t.describe('fake', (t) => {
99-
t.it('with a static template', 'my test string')
99+
t.it('with empty string', '')
100+
.it('with a static template', 'my test string')
100101
.it('with a dynamic template', 'my string: {{string.sample}}')
101102
.it('with multiple static templates', ['A', 'B', 'C'])
102103
.it('with multiple dynamic templates', [
@@ -555,6 +556,11 @@ describe('helpers', () => {
555556
});
556557

557558
describe('fake()', () => {
559+
it('does allow empty string input', () => {
560+
const actual = faker.helpers.fake('');
561+
expect(actual).toBe('');
562+
});
563+
558564
it('replaces a token with a random value for a method without parentheses', () => {
559565
const actual = faker.helpers.fake('{{string.numeric}}');
560566
expect(actual).toMatch(/^\d$/);
@@ -602,12 +608,6 @@ describe('helpers', () => {
602608
expect(actual).toMatch(/^\d{5}$/);
603609
});
604610

605-
it('does not allow empty string parameters', () => {
606-
expect(() => faker.helpers.fake('')).toThrowError(
607-
new FakerError('Pattern string cannot be empty.')
608-
);
609-
});
610-
611611
it('does not allow empty array parameters', () => {
612612
expect(() => faker.helpers.fake([])).toThrowError(
613613
new FakerError('Array of pattern strings cannot be empty.')

0 commit comments

Comments
 (0)