Skip to content

Commit

Permalink
refactor(helpers)!: remove v8 deprecated helpers methods (#2729)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 committed Mar 13, 2024
1 parent 1b1163e commit 1169a05
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 174 deletions.
48 changes: 48 additions & 0 deletions docs/guide/upgrading_v9/2729.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
### Remove deprecated helpers methods

Removed deprecated helpers methods

| old | replacement |
| --------------------------------------- | -------------------------------------------------------------- |
| `faker.helpers.replaceSymbolWithNumber` | `string.replace(/#+/g, (m) => faker.string.numeric(m.length))` |
| `faker.helpers.regexpStyleStringParse` | `faker.helpers.fromRegExp` |

Note these are not exact replacements:

#### `faker.helpers.replaceSymbolWithNumber`

The `replaceSymbolWithNumber` method was deprecated in Faker 8.4 and removed in 9.0. The method parsed the given string symbol by symbol and replaces the `#` symbol with digits (`0` - `9`) and the `!` symbol with digits >=2 (`2` - `9`). This was primarily used internally by Faker for generating phone numbers. If needed, you can use a simple string replace combined with `faker.string.numeric` to replace this

```js
// old
faker.helpers.replaceSymbolWithNumber('#####-##'); // '04812-67'

// new
'#####-##'.replace(/#+/g, (m) => faker.string.numeric(m.length));

// old
faker.helpers.replaceSymbolWithNumber('!#####'); // '123152'

// new
'!#####'
.replace(/#+/g, (m) => faker.string.numeric(m.length))
.replace(/!+/g, (m) =>
faker.string.numeric({ length: m.length, exclude: ['0', '1'] })
);
```

#### `faker.helpers.regexpStyleStringParse`

The `regexpStyleStringParse` method in `faker.helpers` was deprecated in Faker 8.1 and removed in 9.0. A likely replacement is the more powerful `faker.helpers.fromRegExp`.

```js
faker.helpers.regexpStyleStringParse('a{3,6}'); // aaaaa
faker.helpers.fromRegExp('a{3,6}'); // aaaaa
```

However, please note that `faker.helpers.fromRegExp` is not an exact replacement for `faker.helpers.regexpStyleStringParse` as `fromRegExp` cannot handle numeric ranges. This will now need to be handled separately.

```js
faker.helpers.regexpStyleStringParse('a{3,6}[1-100]'); // "aaaa53", etc.
faker.helpers.fromRegExp('a{3,6}') + faker.number.int({ min: 1, max: 100 });
```
67 changes: 1 addition & 66 deletions src/modules/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { Faker, SimpleFaker } from '../..';
import { FakerError } from '../../errors/faker-error';
import { deprecated } from '../../internal/deprecated';
import { SimpleModuleBase } from '../../internal/module-base';
import { fakeEval } from './eval';
import { luhnCheckValue } from './luhn-check';
Expand Down Expand Up @@ -224,36 +223,6 @@ export class SimpleHelpersModule extends SimpleModuleBase {
.replaceAll(/[^\w.-]+/g, ''); // removes all non-word characters except for dots and hyphens
}

/**
* Parses the given string symbol by symbol and replaces the placeholders with digits (`0` - `9`).
* `!` will be replaced by digits >=2 (`2` - `9`).
*
* @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: `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: '8.4',
until: '9.0',
});

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

/**
* Parses the given string symbol by symbols and replaces the placeholder appropriately.
*
Expand Down Expand Up @@ -348,40 +317,6 @@ export class SimpleHelpersModule extends SimpleModuleBase {
return string.replace('L', String(checkNum));
}

/**
* Replaces the regex like expressions in the given string with matching values.
*
* Supported patterns:
* - `.{times}` => Repeat the character exactly `times` times.
* - `.{min,max}` => Repeat the character `min` to `max` times.
* - `[min-max]` => Generate a number between min and max (inclusive).
*
* @param string The template string to parse. Defaults to `''`.
*
* @see faker.helpers.fromRegExp(): For generating a string matching the given regex-like expressions.
*
* @example
* faker.helpers.regexpStyleStringParse() // ''
* faker.helpers.regexpStyleStringParse('#{5}') // '#####'
* faker.helpers.regexpStyleStringParse('#{2,9}') // '#######'
* faker.helpers.regexpStyleStringParse('[500-15000]') // '8375'
* faker.helpers.regexpStyleStringParse('#{3}test[1-5]') // '###test3'
*
* @since 5.0.0
*
* @deprecated Use `faker.helpers.fromRegExp()` instead.
*/
regexpStyleStringParse(string: string = ''): string {
deprecated({
deprecated: 'faker.helpers.regexpStyleStringParse',
proposed: 'faker.helpers.fromRegExp',
since: '8.1',
until: '9.0',
});

return legacyRegexpStringParse(this.faker, string);
}

/**
* Generates a string matching the given regex like expressions.
*
Expand Down Expand Up @@ -1183,7 +1118,7 @@ export class SimpleHelpersModule extends SimpleModuleBase {
*
* There are alternatives of this method for objects ([`objectKey()`](https://fakerjs.dev/api/helpers.html#objectkey) and [`objectValue()`](https://fakerjs.dev/api/helpers.html#objectvalue)) and enums ([`enumValue()`](https://fakerjs.dev/api/helpers.html#enumvalue)). You can also return multiple elements ([`arrayElements()`](https://fakerjs.dev/api/helpers.html#arrayelements)) or elements according to a weighting ([`weightedArrayElement()`](https://fakerjs.dev/api/helpers.html#weightedarrayelement)).
*
* A number of methods can generate strings according to various patterns: [`replaceSymbols()`](https://fakerjs.dev/api/helpers.html#replacesymbols), [`replaceSymbolWithNumber()`](https://fakerjs.dev/api/helpers.html#replacesymbolwithnumber), and [`fromRegExp()`](https://fakerjs.dev/api/helpers.html#fromregexp).
* A number of methods can generate strings according to various patterns: [`replaceSymbols()`](https://fakerjs.dev/api/helpers.html#replacesymbols) and [`fromRegExp()`](https://fakerjs.dev/api/helpers.html#fromregexp).
*/
export class HelpersModule extends SimpleHelpersModule {
constructor(protected readonly faker: Faker) {
Expand Down
36 changes: 0 additions & 36 deletions test/modules/__snapshots__/helpers.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -133,24 +133,12 @@ exports[`helpers > 42 > rangeToNumber > with number 1`] = `5`;

exports[`helpers > 42 > rangeToNumber > with range 1`] = `4`;

exports[`helpers > 42 > regexpStyleStringParse > noArgs 1`] = `""`;

exports[`helpers > 42 > regexpStyleStringParse > only symbols 1`] = `"###test2"`;

exports[`helpers > 42 > regexpStyleStringParse > some string 1`] = `"Hello !###test2"`;

exports[`helpers > 42 > replaceCreditCardSymbols > noArgs 1`] = `"6453-3975-1108-6709-8213"`;

exports[`helpers > 42 > replaceCreditCardSymbols > only symbols 1`] = `"9751-6-1086-7"`;

exports[`helpers > 42 > replaceCreditCardSymbols > some string 1`] = `"^1234567890ß´°4"§$%&/()=?\`+9*,..-;:_NaN"`;

exports[`helpers > 42 > replaceSymbolWithNumber > noArgs 1`] = `""`;

exports[`helpers > 42 > replaceSymbolWithNumber > only symbols 1`] = `"49751"`;

exports[`helpers > 42 > replaceSymbolWithNumber > some string 1`] = `"^1234567890ß´°4"§$%&/()=?\`+9*,..-;:_"`;

exports[`helpers > 42 > replaceSymbols > noArgs 1`] = `""`;

exports[`helpers > 42 > replaceSymbols > only symbols 1`] = `"3Y51EW"`;
Expand Down Expand Up @@ -373,24 +361,12 @@ exports[`helpers > 1211 > rangeToNumber > with number 1`] = `5`;

exports[`helpers > 1211 > rangeToNumber > with range 1`] = `10`;

exports[`helpers > 1211 > regexpStyleStringParse > noArgs 1`] = `""`;

exports[`helpers > 1211 > regexpStyleStringParse > only symbols 1`] = `"###test5"`;

exports[`helpers > 1211 > regexpStyleStringParse > some string 1`] = `"Hello !###test5"`;

exports[`helpers > 1211 > replaceCreditCardSymbols > noArgs 1`] = `"6453-9829-6673-6876-8482"`;

exports[`helpers > 1211 > replaceCreditCardSymbols > only symbols 1`] = `"8296-9-6747-7"`;

exports[`helpers > 1211 > replaceCreditCardSymbols > some string 1`] = `"^1234567890ß´°9"§$%&/()=?\`+8*,..-;:_NaN"`;

exports[`helpers > 1211 > replaceSymbolWithNumber > noArgs 1`] = `""`;

exports[`helpers > 1211 > replaceSymbolWithNumber > only symbols 1`] = `"98296"`;

exports[`helpers > 1211 > replaceSymbolWithNumber > some string 1`] = `"^1234567890ß´°9"§$%&/()=?\`+8*,..-;:_"`;

exports[`helpers > 1211 > replaceSymbols > noArgs 1`] = `""`;

exports[`helpers > 1211 > replaceSymbols > only symbols 1`] = `"9XZ6R3"`;
Expand Down Expand Up @@ -595,24 +571,12 @@ exports[`helpers > 1337 > rangeToNumber > with number 1`] = `5`;
exports[`helpers > 1337 > rangeToNumber > with range 1`] = `3`;
exports[`helpers > 1337 > regexpStyleStringParse > noArgs 1`] = `""`;
exports[`helpers > 1337 > regexpStyleStringParse > only symbols 1`] = `"###test2"`;
exports[`helpers > 1337 > regexpStyleStringParse > some string 1`] = `"Hello !###test2"`;
exports[`helpers > 1337 > replaceCreditCardSymbols > noArgs 1`] = `"6453-2124-3529-7136-1945"`;
exports[`helpers > 1337 > replaceCreditCardSymbols > only symbols 1`] = `"1243-5-5297-1"`;
exports[`helpers > 1337 > replaceCreditCardSymbols > some string 1`] = `"^1234567890ß´°4"§$%&/()=?\`+1*,..-;:_NaN"`;
exports[`helpers > 1337 > replaceSymbolWithNumber > noArgs 1`] = `""`;
exports[`helpers > 1337 > replaceSymbolWithNumber > only symbols 1`] = `"41243"`;
exports[`helpers > 1337 > replaceSymbolWithNumber > some string 1`] = `"^1234567890ß´°4"§$%&/()=?\`+1*,..-;:_"`;
exports[`helpers > 1337 > replaceSymbols > noArgs 1`] = `""`;
exports[`helpers > 1337 > replaceSymbols > only symbols 1`] = `"2EL3NZ"`;
Expand Down
72 changes: 0 additions & 72 deletions test/modules/helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ describe('helpers', () => {
t.it('noArgs').it('some string', 'hello world');
});

t.describe('replaceSymbolWithNumber', (t) => {
t.it('noArgs')
.it('only symbols', '!####')
.it('some string', '^1234567890ß´°!"§$%&/()=?`+#*,..-;:_');
});

t.describe('replaceSymbols', (t) => {
t.it('noArgs')
.it('only symbols', '#?*#?*')
Expand All @@ -31,12 +25,6 @@ describe('helpers', () => {
.it('some string', '^1234567890ß´°!"§$%&/()=?`+#*,..-;:_L');
});

t.describe('regexpStyleStringParse', (t) => {
t.it('noArgs')
.it('only symbols', '#{3}test[1-5]')
.it('some string', 'Hello !#{3}test[1-5]');
});

t.describe('fromRegExp', (t) => {
t.it('with static string', 'Hello World!')
.it('with static RegExp', /Hello World!/)
Expand Down Expand Up @@ -518,22 +506,6 @@ describe('helpers', () => {
});
});

describe('replaceSymbolWithNumber()', () => {
describe('when no symbol passed in', () => {
it("uses '#' by default", () => {
const num = faker.helpers.replaceSymbolWithNumber('#AB');
expect(num).toMatch(/\dAB/);
});
});

describe('when symbol passed in', () => {
it('replaces that symbol with integers', () => {
const num = faker.helpers.replaceSymbolWithNumber('#AB', 'A');
expect(num).toMatch(/#\dB/);
});
});
});

describe('replaceSymbols()', () => {
it('returns empty string with no arguments', () => {
expect(faker.helpers.replaceSymbols()).toBe('');
Expand Down Expand Up @@ -588,50 +560,6 @@ describe('helpers', () => {
});
});

describe('regexpStyleStringParse()', () => {
it('returns an empty string when called without param', () => {
expect(faker.helpers.regexpStyleStringParse()).toBe('');
});

it('deals with range repeat', () => {
const string = faker.helpers.regexpStyleStringParse('#{5,10}');
expect(string.length).toBeLessThanOrEqual(10);
expect(string.length).toBeGreaterThanOrEqual(5);
expect(string).toMatch(/^#{5,10}$/);
});

it('flips the range when min > max', () => {
const string = faker.helpers.regexpStyleStringParse('#{10,5}');
expect(string.length).toBeLessThanOrEqual(10);
expect(string.length).toBeGreaterThanOrEqual(5);
expect(string).toMatch(/^#{5,10}$/);
});

it('repeats string {n} number of times', () => {
expect(faker.helpers.regexpStyleStringParse('%{10}')).toBe(
'%'.repeat(10)
);
expect(faker.helpers.regexpStyleStringParse('%{30}')).toBe(
'%'.repeat(30)
);
expect(faker.helpers.regexpStyleStringParse('%{5}')).toBe(
'%'.repeat(5)
);
});

it('creates a numerical range', () => {
const string = faker.helpers.regexpStyleStringParse('Hello[0-9]');
expect(string).toMatch(/^Hello[0-9]$/);
});

it('deals with multiple tokens in one string', () => {
const string = faker.helpers.regexpStyleStringParse(
'Test#{5}%{2,5}Testing**[1-5]**{10}END'
);
expect(string).toMatch(/^Test#{5}%{2,5}Testing\*\*[1-5]\*\*{10}END$/);
});
});

describe('fromRegExp()', () => {
it('deals with range repeat', () => {
const string = faker.helpers.fromRegExp(/#{5,10}/);
Expand Down

0 comments on commit 1169a05

Please sign in to comment.