From ca62689cb1a4c01496cbafb798079679f51660d2 Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Fri, 23 Dec 2022 13:01:41 +0100 Subject: [PATCH 1/2] feat(helpers): allow empty string in fake --- src/modules/helpers/index.ts | 8 -------- test/__snapshots__/helpers.spec.ts.snap | 6 ++++++ test/helpers.spec.ts | 14 +++++++------- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/modules/helpers/index.ts b/src/modules/helpers/index.ts index cb4f768074b..81e1ba12ab9 100644 --- a/src/modules/helpers/index.ts +++ b/src/modules/helpers/index.ts @@ -645,10 +645,6 @@ export class HelpersModule { throw new FakerError('Array of pattern strings cannot be empty.'); } } - // if incoming str parameter is not provided, return error message - if (pattern.length === 0) { - throw new FakerError('Pattern string cannot be empty.'); - } // find first matching {{ and }} const start = pattern.search(/{{[a-z]/); @@ -721,10 +717,6 @@ export class HelpersModule { const res = pattern.substring(0, start) + result + pattern.substring(end + 2); - if (res === '') { - return ''; - } - // return the response recursively until we are done finding all tags return this.fake(res); } diff --git a/test/__snapshots__/helpers.spec.ts.snap b/test/__snapshots__/helpers.spec.ts.snap index 4ce3183266e..95ac4a03aa8 100644 --- a/test/__snapshots__/helpers.spec.ts.snap +++ b/test/__snapshots__/helpers.spec.ts.snap @@ -33,6 +33,8 @@ exports[`helpers > 42 > fake > with a dynamic template 1`] = `"my string: Cky2ei exports[`helpers > 42 > fake > with a static template 1`] = `"my test string"`; +exports[`helpers > 42 > fake > with empty string 1`] = `""`; + exports[`helpers > 42 > fake > with multiple dynamic templates 1`] = `"Sandy"`; 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 exports[`helpers > 1211 > fake > with a static template 1`] = `"my test string"`; +exports[`helpers > 1211 > fake > with empty string 1`] = `""`; + exports[`helpers > 1211 > fake > with multiple dynamic templates 1`] = `"La Crosse"`; 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 exports[`helpers > 1337 > fake > with a static template 1`] = `"my test string"`; +exports[`helpers > 1337 > fake > with empty string 1`] = `""`; + exports[`helpers > 1337 > fake > with multiple dynamic templates 1`] = `"U/4:SK$>6Q"`; exports[`helpers > 1337 > fake > with multiple static templates 1`] = `"A"`; diff --git a/test/helpers.spec.ts b/test/helpers.spec.ts index 37a1bd02693..2f591fc20ac 100644 --- a/test/helpers.spec.ts +++ b/test/helpers.spec.ts @@ -96,7 +96,8 @@ describe('helpers', () => { }); t.describe('fake', (t) => { - t.it('with a static template', 'my test string') + t.it('with empty string', '') + .it('with a static template', 'my test string') .it('with a dynamic template', 'my string: {{string.sample}}') .it('with multiple static templates', ['A', 'B', 'C']) .it('with multiple dynamic templates', [ @@ -555,6 +556,11 @@ describe('helpers', () => { }); describe('fake()', () => { + it('does allow empty string input', () => { + const actual = faker.helpers.fake(''); + expect(actual).toBe(''); + }); + it('replaces a token with a random value for a method without parentheses', () => { const actual = faker.helpers.fake('{{string.numeric}}'); expect(actual).toMatch(/^\d$/); @@ -602,12 +608,6 @@ describe('helpers', () => { expect(actual).toMatch(/^\d{5}$/); }); - it('does not allow empty string parameters', () => { - expect(() => faker.helpers.fake('')).toThrowError( - new FakerError('Pattern string cannot be empty.') - ); - }); - it('does not allow empty array parameters', () => { expect(() => faker.helpers.fake([])).toThrowError( new FakerError('Array of pattern strings cannot be empty.') From 5c44abc4cbf409d03a2fcae0e133843487f0cd96 Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Sat, 24 Dec 2022 02:34:05 +0100 Subject: [PATCH 2/2] chore: adjust jsdocs --- src/modules/helpers/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/modules/helpers/index.ts b/src/modules/helpers/index.ts index 81e1ba12ab9..2ccc311e21e 100644 --- a/src/modules/helpers/index.ts +++ b/src/modules/helpers/index.ts @@ -526,7 +526,7 @@ export class HelpersModule { * * It is also NOT possible to use any non-faker methods or plain javascript in such patterns. * - * @param pattern The pattern string that will get interpolated. Must not be empty. + * @param pattern The pattern string that will get interpolated. * * @see faker.helpers.mustache() to use custom functions for resolution. * @@ -621,7 +621,7 @@ export class HelpersModule { * * It is also NOT possible to use any non-faker methods or plain javascript in such patterns. * - * @param pattern The pattern string that will get interpolated. Must not be empty. If an array is passed, a random element will be picked and interpolated. + * @param pattern The pattern string that will get interpolated. If an array is passed, a random element will be picked and interpolated. * * @see faker.helpers.mustache() to use custom functions for resolution. *