From 6cb120eea5a4041a1e668a94ff20a56e5d97afba Mon Sep 17 00:00:00 2001 From: suyashgulati Date: Tue, 21 Nov 2023 22:25:04 +0530 Subject: [PATCH 1/6] deprecate replaceSymbolWithNumber in helper module --- src/modules/helpers/index.ts | 58 ++++++++++++++++++++++++++++-------- src/modules/phone/index.ts | 3 +- 2 files changed, 47 insertions(+), 14 deletions(-) diff --git a/src/modules/helpers/index.ts b/src/modules/helpers/index.ts index 3f121a41233..f98f92cb41c 100644 --- a/src/modules/helpers/index.ts +++ b/src/modules/helpers/index.ts @@ -158,6 +158,39 @@ function legacyRegexpStringParse( return string; } +/** + * Parses the given string symbol by symbol and replaces the placeholders with digits (`0` - `9`). + * `!` will be replaced by digits >=2 (`2` - `9`). + * + * @param faker Faker instance + * @param string The template string to parse. Defaults to `''`. + * @param symbol The symbol to replace with digits. Defaults to `'#'`. + * + * @internal + * + * @example + * faker.helpers.legacyReplaceSymbolWithNumber(this.faker) // '' + * faker.helpers.legacyReplaceSymbolWithNumber(this.faker, '#####') // '04812' + * faker.helpers.legacyReplaceSymbolWithNumber(this.faker, '!####') // '27378' + * faker.helpers.legacyReplaceSymbolWithNumber(this.faker, 'Your pin is: !####') // '29841' + * + * @since 9.0.0 + * */ +export function legacyReplaceSymbolWithNumber(faker: SimpleFaker, string: string = '', symbol: string = '#'): string { + let str = ''; + for (let i = 0; i < string.length; i++) { + if (string.charAt(i) === symbol) { + str += faker.number.int(9); + } else if (string.charAt(i) === '!') { + str += faker.number.int({ min: 2, max: 9 }); + } else { + str += string.charAt(i); + } + } + + return str; +} + /** * Module with various helper methods providing basic (seed-dependent) operations useful for implementing faker methods (without methods requiring localized data). */ @@ -204,21 +237,20 @@ export class SimpleHelpersModule extends SimpleModuleBase { * faker.helpers.replaceSymbolWithNumber('!####') // '27378' * faker.helpers.replaceSymbolWithNumber('Your pin is: !####') // '29841' * + * @see faker.string.numeric() + * + * @deprecated Use `faker.string.numeric()` instead. Example: `string.replace(/#+/g, (m) => faker.string.numeric(m.length));` + * * @since 2.0.1 */ replaceSymbolWithNumber(string: string = '', symbol: string = '#'): string { - let str = ''; - for (let i = 0; i < string.length; i++) { - if (string.charAt(i) === symbol) { - str += this.faker.number.int(9); - } else if (string.charAt(i) === '!') { - str += this.faker.number.int({ min: 2, max: 9 }); - } else { - str += string.charAt(i); - } - } + deprecated({ + deprecated: 'faker.helpers.replaceSymbolWithNumber', + proposed: 'string.replace(/#+/g, (m) => faker.string.numeric(m.length))', + since: '9.0.0', + }); - return str; + return legacyReplaceSymbolWithNumber(this.faker, string, symbol); } /** @@ -290,7 +322,7 @@ export class SimpleHelpersModule extends SimpleModuleBase { /** * Replaces the symbols and patterns in a credit card schema including Luhn checksum. * - * This method supports both range patterns `[4-9]` as well as the patterns used by `replaceSymbolWithNumber()`. + * This method supports both range patterns `[4-9]` as well as the patterns used by `legacyReplaceSymbolWithNumber()`. * `L` will be replaced with the appropriate Luhn checksum. * * @param string The credit card format pattern. Defaults to `'6453-####-####-####-###L'`. @@ -309,7 +341,7 @@ export class SimpleHelpersModule extends SimpleModuleBase { // default values required for calling method without arguments string = legacyRegexpStringParse(this.faker, string); // replace [4-9] with a random number in range etc... - string = this.replaceSymbolWithNumber(string, symbol); // replace ### with random numbers + string = legacyReplaceSymbolWithNumber(this.faker, string, symbol); // replace ### with random numbers const checkNum = luhnCheckValue(string); return string.replace('L', String(checkNum)); diff --git a/src/modules/phone/index.ts b/src/modules/phone/index.ts index 6747e35d10c..833938d71fc 100644 --- a/src/modules/phone/index.ts +++ b/src/modules/phone/index.ts @@ -1,5 +1,6 @@ import { deprecated } from '../../internal/deprecated'; import { ModuleBase } from '../../internal/module-base'; +import { legacyReplaceSymbolWithNumber } from '../helpers'; /** * Module to generate phone-related data. @@ -68,7 +69,7 @@ export class PhoneModule extends ModuleBase { this.faker.helpers.arrayElement( this.faker.definitions.phone_number.formats ); - return this.faker.helpers.replaceSymbolWithNumber(format); + return legacyReplaceSymbolWithNumber(this.faker, format); } /** From e2d30345e6d1886d0feb86caf9d04721dc35eee7 Mon Sep 17 00:00:00 2001 From: suyashgulati Date: Tue, 21 Nov 2023 23:17:12 +0530 Subject: [PATCH 2/6] See tag description --- src/modules/helpers/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/helpers/index.ts b/src/modules/helpers/index.ts index f98f92cb41c..623599da8aa 100644 --- a/src/modules/helpers/index.ts +++ b/src/modules/helpers/index.ts @@ -237,7 +237,7 @@ export class SimpleHelpersModule extends SimpleModuleBase { * faker.helpers.replaceSymbolWithNumber('!####') // '27378' * faker.helpers.replaceSymbolWithNumber('Your pin is: !####') // '29841' * - * @see faker.string.numeric() + * @see faker.string.numeric(): For the replacement method. * * @deprecated Use `faker.string.numeric()` instead. Example: `string.replace(/#+/g, (m) => faker.string.numeric(m.length));` * From 501854e2fecf539297f48336d5da6e86e009ea75 Mon Sep 17 00:00:00 2001 From: suyashgulati Date: Wed, 22 Nov 2023 01:15:31 +0530 Subject: [PATCH 3/6] Lint fixes --- src/modules/helpers/index.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/modules/helpers/index.ts b/src/modules/helpers/index.ts index 623599da8aa..fc695db45c0 100644 --- a/src/modules/helpers/index.ts +++ b/src/modules/helpers/index.ts @@ -162,12 +162,12 @@ function legacyRegexpStringParse( * Parses the given string symbol by symbol and replaces the placeholders with digits (`0` - `9`). * `!` will be replaced by digits >=2 (`2` - `9`). * + * @internal + * * @param faker Faker instance * @param string The template string to parse. Defaults to `''`. * @param symbol The symbol to replace with digits. Defaults to `'#'`. * - * @internal - * * @example * faker.helpers.legacyReplaceSymbolWithNumber(this.faker) // '' * faker.helpers.legacyReplaceSymbolWithNumber(this.faker, '#####') // '04812' @@ -175,8 +175,12 @@ function legacyRegexpStringParse( * faker.helpers.legacyReplaceSymbolWithNumber(this.faker, 'Your pin is: !####') // '29841' * * @since 9.0.0 - * */ -export function legacyReplaceSymbolWithNumber(faker: SimpleFaker, string: string = '', symbol: string = '#'): string { + */ +export function legacyReplaceSymbolWithNumber( + faker: SimpleFaker, + string: string = '', + symbol: string = '#' +): string { let str = ''; for (let i = 0; i < string.length; i++) { if (string.charAt(i) === symbol) { @@ -231,17 +235,17 @@ export class SimpleHelpersModule extends SimpleModuleBase { * @param string The template string to parse. Defaults to `''`. * @param symbol The symbol to replace with digits. Defaults to `'#'`. * + * @see faker.string.numeric(): For the replacement method. + * * @example * faker.helpers.replaceSymbolWithNumber() // '' * faker.helpers.replaceSymbolWithNumber('#####') // '04812' * faker.helpers.replaceSymbolWithNumber('!####') // '27378' * faker.helpers.replaceSymbolWithNumber('Your pin is: !####') // '29841' * - * @see faker.string.numeric(): For the replacement method. + * @since 2.0.1 * * @deprecated Use `faker.string.numeric()` instead. Example: `string.replace(/#+/g, (m) => faker.string.numeric(m.length));` - * - * @since 2.0.1 */ replaceSymbolWithNumber(string: string = '', symbol: string = '#'): string { deprecated({ From 6f8a9a5ac2acf4a8d88d47d945e350870226fc34 Mon Sep 17 00:00:00 2001 From: Suyash Gulati Date: Fri, 24 Nov 2023 13:28:23 +0530 Subject: [PATCH 4/6] Apply suggestions from code review Co-authored-by: ST-DDT --- src/modules/helpers/index.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/modules/helpers/index.ts b/src/modules/helpers/index.ts index fc695db45c0..81a687ec60d 100644 --- a/src/modules/helpers/index.ts +++ b/src/modules/helpers/index.ts @@ -164,7 +164,7 @@ function legacyRegexpStringParse( * * @internal * - * @param faker Faker instance + * @param faker A Faker instance. * @param string The template string to parse. Defaults to `''`. * @param symbol The symbol to replace with digits. Defaults to `'#'`. * @@ -174,7 +174,7 @@ function legacyRegexpStringParse( * faker.helpers.legacyReplaceSymbolWithNumber(this.faker, '!####') // '27378' * faker.helpers.legacyReplaceSymbolWithNumber(this.faker, 'Your pin is: !####') // '29841' * - * @since 9.0.0 + * @since 8.4.0 */ export function legacyReplaceSymbolWithNumber( faker: SimpleFaker, @@ -245,13 +245,14 @@ export class SimpleHelpersModule extends SimpleModuleBase { * * @since 2.0.1 * - * @deprecated Use `faker.string.numeric()` instead. Example: `string.replace(/#+/g, (m) => faker.string.numeric(m.length));` + * @deprecated Use `faker.string.numeric()` instead. Example: `value.replace(/#+/g, (m) => faker.string.numeric(m.length));` */ replaceSymbolWithNumber(string: string = '', symbol: string = '#'): string { deprecated({ deprecated: 'faker.helpers.replaceSymbolWithNumber', proposed: 'string.replace(/#+/g, (m) => faker.string.numeric(m.length))', - since: '9.0.0', + since: '8.4', + until: '9.0', }); return legacyReplaceSymbolWithNumber(this.faker, string, symbol); From 8fda8a029c739f3187764664595f5b5f5ab2b453 Mon Sep 17 00:00:00 2001 From: suyashgulati Date: Fri, 24 Nov 2023 17:37:32 +0530 Subject: [PATCH 5/6] Apply suggestions from code review - Part II --- src/modules/helpers/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/helpers/index.ts b/src/modules/helpers/index.ts index 81a687ec60d..25522f00f57 100644 --- a/src/modules/helpers/index.ts +++ b/src/modules/helpers/index.ts @@ -327,7 +327,7 @@ export class SimpleHelpersModule extends SimpleModuleBase { /** * Replaces the symbols and patterns in a credit card schema including Luhn checksum. * - * This method supports both range patterns `[4-9]` as well as the patterns used by `legacyReplaceSymbolWithNumber()`. + * This method supports both range patterns `[4-9]` as well as the patterns used by `replaceSymbolWithNumber()`. * `L` will be replaced with the appropriate Luhn checksum. * * @param string The credit card format pattern. Defaults to `'6453-####-####-####-###L'`. From 1cdd073f49a143c0d7f08b0991026725bb16aecb Mon Sep 17 00:00:00 2001 From: Suyash Gulati Date: Sat, 25 Nov 2023 18:15:49 +0530 Subject: [PATCH 6/6] Apply suggestions from code review - Part III Co-authored-by: ST-DDT --- src/modules/helpers/index.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/modules/helpers/index.ts b/src/modules/helpers/index.ts index 07e6da29a30..9a681da9641 100644 --- a/src/modules/helpers/index.ts +++ b/src/modules/helpers/index.ts @@ -162,17 +162,19 @@ function legacyRegexpStringParse( * Parses the given string symbol by symbol and replaces the placeholders with digits (`0` - `9`). * `!` will be replaced by digits >=2 (`2` - `9`). * + * Note: This method will be removed in v9. + * * @internal * - * @param faker A Faker instance. + * @param faker The Faker instance to use. * @param string The template string to parse. Defaults to `''`. * @param symbol The symbol to replace with digits. Defaults to `'#'`. * * @example - * faker.helpers.legacyReplaceSymbolWithNumber(this.faker) // '' - * faker.helpers.legacyReplaceSymbolWithNumber(this.faker, '#####') // '04812' - * faker.helpers.legacyReplaceSymbolWithNumber(this.faker, '!####') // '27378' - * faker.helpers.legacyReplaceSymbolWithNumber(this.faker, 'Your pin is: !####') // '29841' + * legacyReplaceSymbolWithNumber(faker) // '' + * legacyReplaceSymbolWithNumber(faker, '#####') // '04812' + * legacyReplaceSymbolWithNumber(faker, '!####') // '27378' + * legacyReplaceSymbolWithNumber(faker, 'Your pin is: !####') // '29841' * * @since 8.4.0 */