Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(helper): deprecate replaceSymbolWithNumber #2557

Merged
62 changes: 49 additions & 13 deletions src/modules/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,43 @@ 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`).
suyashgulati marked this conversation as resolved.
Show resolved Hide resolved
*
* @internal
*
* @param faker Faker instance
suyashgulati marked this conversation as resolved.
Show resolved Hide resolved
* @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'
suyashgulati marked this conversation as resolved.
Show resolved Hide resolved
*
* @since 9.0.0
suyashgulati marked this conversation as resolved.
Show resolved Hide resolved
*/
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).
*/
Expand Down Expand Up @@ -198,27 +235,26 @@ 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'
*
* @since 2.0.1
*
* @deprecated Use `faker.string.numeric()` instead. Example: `string.replace(/#+/g, (m) => faker.string.numeric(m.length));`
suyashgulati marked this conversation as resolved.
Show resolved Hide resolved
*/
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',
suyashgulati marked this conversation as resolved.
Show resolved Hide resolved
});

return str;
return legacyReplaceSymbolWithNumber(this.faker, string, symbol);
}

/**
Expand Down Expand Up @@ -290,7 +326,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()`.
suyashgulati marked this conversation as resolved.
Show resolved Hide resolved
* `L` will be replaced with the appropriate Luhn checksum.
*
* @param string The credit card format pattern. Defaults to `'6453-####-####-####-###L'`.
Expand All @@ -309,7 +345,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));
Expand Down
3 changes: 2 additions & 1 deletion src/modules/phone/index.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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);
}

/**
Expand Down