aboutsummaryrefslogtreecommitdiff
path: root/src/modules
diff options
context:
space:
mode:
authorSuyash Gulati <[email protected]>2023-11-27 00:04:38 +0530
committerGitHub <[email protected]>2023-11-26 19:34:38 +0100
commit3c5ceec96d59c4e6c17fdf6e9ffc785a5fb92056 (patch)
tree92af1406f8dd10a187b8c1b708186ee5c4440af7 /src/modules
parent5525b55cc47fa97b55de52fccff7ab51633e639a (diff)
downloadfaker-3c5ceec96d59c4e6c17fdf6e9ffc785a5fb92056.tar.xz
faker-3c5ceec96d59c4e6c17fdf6e9ffc785a5fb92056.zip
refactor(helper): deprecate replaceSymbolWithNumber (#2557)
Diffstat (limited to 'src/modules')
-rw-r--r--src/modules/helpers/index.ts63
-rw-r--r--src/modules/phone/index.ts3
2 files changed, 53 insertions, 13 deletions
diff --git a/src/modules/helpers/index.ts b/src/modules/helpers/index.ts
index aaf0e231..9a681da9 100644
--- a/src/modules/helpers/index.ts
+++ b/src/modules/helpers/index.ts
@@ -159,6 +159,45 @@ 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 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
+ * legacyReplaceSymbolWithNumber(faker) // ''
+ * legacyReplaceSymbolWithNumber(faker, '#####') // '04812'
+ * legacyReplaceSymbolWithNumber(faker, '!####') // '27378'
+ * legacyReplaceSymbolWithNumber(faker, 'Your pin is: !####') // '29841'
+ *
+ * @since 8.4.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).
*/
export class SimpleHelpersModule extends SimpleModuleBase {
@@ -198,6 +237,8 @@ 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'
@@ -205,20 +246,18 @@ export class SimpleHelpersModule extends SimpleModuleBase {
* faker.helpers.replaceSymbolWithNumber('Your pin is: !####') // '29841'
*
* @since 2.0.1
+ *
+ * @deprecated Use `faker.string.numeric()` instead. Example: `value.replace(/#+/g, (m) => faker.string.numeric(m.length));`
*/
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: '8.4',
+ until: '9.0',
+ });
- return str;
+ return legacyReplaceSymbolWithNumber(this.faker, string, symbol);
}
/**
@@ -309,7 +348,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 6747e35d..833938d7 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);
}
/**