Skip to content

Commit 1169a05

Browse files
authored
refactor(helpers)!: remove v8 deprecated helpers methods (#2729)
1 parent 1b1163e commit 1169a05

File tree

4 files changed

+49
-174
lines changed

4 files changed

+49
-174
lines changed

docs/guide/upgrading_v9/2729.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
### Remove deprecated helpers methods
2+
3+
Removed deprecated helpers methods
4+
5+
| old | replacement |
6+
| --------------------------------------- | -------------------------------------------------------------- |
7+
| `faker.helpers.replaceSymbolWithNumber` | `string.replace(/#+/g, (m) => faker.string.numeric(m.length))` |
8+
| `faker.helpers.regexpStyleStringParse` | `faker.helpers.fromRegExp` |
9+
10+
Note these are not exact replacements:
11+
12+
#### `faker.helpers.replaceSymbolWithNumber`
13+
14+
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
15+
16+
```js
17+
// old
18+
faker.helpers.replaceSymbolWithNumber('#####-##'); // '04812-67'
19+
20+
// new
21+
'#####-##'.replace(/#+/g, (m) => faker.string.numeric(m.length));
22+
23+
// old
24+
faker.helpers.replaceSymbolWithNumber('!#####'); // '123152'
25+
26+
// new
27+
'!#####'
28+
.replace(/#+/g, (m) => faker.string.numeric(m.length))
29+
.replace(/!+/g, (m) =>
30+
faker.string.numeric({ length: m.length, exclude: ['0', '1'] })
31+
);
32+
```
33+
34+
#### `faker.helpers.regexpStyleStringParse`
35+
36+
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`.
37+
38+
```js
39+
faker.helpers.regexpStyleStringParse('a{3,6}'); // aaaaa
40+
faker.helpers.fromRegExp('a{3,6}'); // aaaaa
41+
```
42+
43+
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.
44+
45+
```js
46+
faker.helpers.regexpStyleStringParse('a{3,6}[1-100]'); // "aaaa53", etc.
47+
faker.helpers.fromRegExp('a{3,6}') + faker.number.int({ min: 1, max: 100 });
48+
```

src/modules/helpers/index.ts

Lines changed: 1 addition & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { Faker, SimpleFaker } from '../..';
22
import { FakerError } from '../../errors/faker-error';
3-
import { deprecated } from '../../internal/deprecated';
43
import { SimpleModuleBase } from '../../internal/module-base';
54
import { fakeEval } from './eval';
65
import { luhnCheckValue } from './luhn-check';
@@ -224,36 +223,6 @@ export class SimpleHelpersModule extends SimpleModuleBase {
224223
.replaceAll(/[^\w.-]+/g, ''); // removes all non-word characters except for dots and hyphens
225224
}
226225

227-
/**
228-
* Parses the given string symbol by symbol and replaces the placeholders with digits (`0` - `9`).
229-
* `!` will be replaced by digits >=2 (`2` - `9`).
230-
*
231-
* @param string The template string to parse. Defaults to `''`.
232-
* @param symbol The symbol to replace with digits. Defaults to `'#'`.
233-
*
234-
* @see faker.string.numeric(): For the replacement method.
235-
*
236-
* @example
237-
* faker.helpers.replaceSymbolWithNumber() // ''
238-
* faker.helpers.replaceSymbolWithNumber('#####') // '04812'
239-
* faker.helpers.replaceSymbolWithNumber('!####') // '27378'
240-
* faker.helpers.replaceSymbolWithNumber('Your pin is: !####') // '29841'
241-
*
242-
* @since 2.0.1
243-
*
244-
* @deprecated Use `faker.string.numeric()` instead. Example: `value.replace(/#+/g, (m) => faker.string.numeric(m.length));`
245-
*/
246-
replaceSymbolWithNumber(string: string = '', symbol: string = '#'): string {
247-
deprecated({
248-
deprecated: 'faker.helpers.replaceSymbolWithNumber',
249-
proposed: 'string.replace(/#+/g, (m) => faker.string.numeric(m.length))',
250-
since: '8.4',
251-
until: '9.0',
252-
});
253-
254-
return legacyReplaceSymbolWithNumber(this.faker, string, symbol);
255-
}
256-
257226
/**
258227
* Parses the given string symbol by symbols and replaces the placeholder appropriately.
259228
*
@@ -348,40 +317,6 @@ export class SimpleHelpersModule extends SimpleModuleBase {
348317
return string.replace('L', String(checkNum));
349318
}
350319

351-
/**
352-
* Replaces the regex like expressions in the given string with matching values.
353-
*
354-
* Supported patterns:
355-
* - `.{times}` => Repeat the character exactly `times` times.
356-
* - `.{min,max}` => Repeat the character `min` to `max` times.
357-
* - `[min-max]` => Generate a number between min and max (inclusive).
358-
*
359-
* @param string The template string to parse. Defaults to `''`.
360-
*
361-
* @see faker.helpers.fromRegExp(): For generating a string matching the given regex-like expressions.
362-
*
363-
* @example
364-
* faker.helpers.regexpStyleStringParse() // ''
365-
* faker.helpers.regexpStyleStringParse('#{5}') // '#####'
366-
* faker.helpers.regexpStyleStringParse('#{2,9}') // '#######'
367-
* faker.helpers.regexpStyleStringParse('[500-15000]') // '8375'
368-
* faker.helpers.regexpStyleStringParse('#{3}test[1-5]') // '###test3'
369-
*
370-
* @since 5.0.0
371-
*
372-
* @deprecated Use `faker.helpers.fromRegExp()` instead.
373-
*/
374-
regexpStyleStringParse(string: string = ''): string {
375-
deprecated({
376-
deprecated: 'faker.helpers.regexpStyleStringParse',
377-
proposed: 'faker.helpers.fromRegExp',
378-
since: '8.1',
379-
until: '9.0',
380-
});
381-
382-
return legacyRegexpStringParse(this.faker, string);
383-
}
384-
385320
/**
386321
* Generates a string matching the given regex like expressions.
387322
*
@@ -1183,7 +1118,7 @@ export class SimpleHelpersModule extends SimpleModuleBase {
11831118
*
11841119
* 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)).
11851120
*
1186-
* 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).
1121+
* 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).
11871122
*/
11881123
export class HelpersModule extends SimpleHelpersModule {
11891124
constructor(protected readonly faker: Faker) {

test/modules/__snapshots__/helpers.spec.ts.snap

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -133,24 +133,12 @@ exports[`helpers > 42 > rangeToNumber > with number 1`] = `5`;
133133

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

136-
exports[`helpers > 42 > regexpStyleStringParse > noArgs 1`] = `""`;
137-
138-
exports[`helpers > 42 > regexpStyleStringParse > only symbols 1`] = `"###test2"`;
139-
140-
exports[`helpers > 42 > regexpStyleStringParse > some string 1`] = `"Hello !###test2"`;
141-
142136
exports[`helpers > 42 > replaceCreditCardSymbols > noArgs 1`] = `"6453-3975-1108-6709-8213"`;
143137

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

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

148-
exports[`helpers > 42 > replaceSymbolWithNumber > noArgs 1`] = `""`;
149-
150-
exports[`helpers > 42 > replaceSymbolWithNumber > only symbols 1`] = `"49751"`;
151-
152-
exports[`helpers > 42 > replaceSymbolWithNumber > some string 1`] = `"^1234567890ß´°4"§$%&/()=?\`+9*,..-;:_"`;
153-
154142
exports[`helpers > 42 > replaceSymbols > noArgs 1`] = `""`;
155143

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

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

376-
exports[`helpers > 1211 > regexpStyleStringParse > noArgs 1`] = `""`;
377-
378-
exports[`helpers > 1211 > regexpStyleStringParse > only symbols 1`] = `"###test5"`;
379-
380-
exports[`helpers > 1211 > regexpStyleStringParse > some string 1`] = `"Hello !###test5"`;
381-
382364
exports[`helpers > 1211 > replaceCreditCardSymbols > noArgs 1`] = `"6453-9829-6673-6876-8482"`;
383365

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

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

388-
exports[`helpers > 1211 > replaceSymbolWithNumber > noArgs 1`] = `""`;
389-
390-
exports[`helpers > 1211 > replaceSymbolWithNumber > only symbols 1`] = `"98296"`;
391-
392-
exports[`helpers > 1211 > replaceSymbolWithNumber > some string 1`] = `"^1234567890ß´°9"§$%&/()=?\`+8*,..-;:_"`;
393-
394370
exports[`helpers > 1211 > replaceSymbols > noArgs 1`] = `""`;
395371

396372
exports[`helpers > 1211 > replaceSymbols > only symbols 1`] = `"9XZ6R3"`;
@@ -595,24 +571,12 @@ exports[`helpers > 1337 > rangeToNumber > with number 1`] = `5`;
595571
596572
exports[`helpers > 1337 > rangeToNumber > with range 1`] = `3`;
597573
598-
exports[`helpers > 1337 > regexpStyleStringParse > noArgs 1`] = `""`;
599-
600-
exports[`helpers > 1337 > regexpStyleStringParse > only symbols 1`] = `"###test2"`;
601-
602-
exports[`helpers > 1337 > regexpStyleStringParse > some string 1`] = `"Hello !###test2"`;
603-
604574
exports[`helpers > 1337 > replaceCreditCardSymbols > noArgs 1`] = `"6453-2124-3529-7136-1945"`;
605575
606576
exports[`helpers > 1337 > replaceCreditCardSymbols > only symbols 1`] = `"1243-5-5297-1"`;
607577
608578
exports[`helpers > 1337 > replaceCreditCardSymbols > some string 1`] = `"^1234567890ß´°4"§$%&/()=?\`+1*,..-;:_NaN"`;
609579
610-
exports[`helpers > 1337 > replaceSymbolWithNumber > noArgs 1`] = `""`;
611-
612-
exports[`helpers > 1337 > replaceSymbolWithNumber > only symbols 1`] = `"41243"`;
613-
614-
exports[`helpers > 1337 > replaceSymbolWithNumber > some string 1`] = `"^1234567890ß´°4"§$%&/()=?\`+1*,..-;:_"`;
615-
616580
exports[`helpers > 1337 > replaceSymbols > noArgs 1`] = `""`;
617581
618582
exports[`helpers > 1337 > replaceSymbols > only symbols 1`] = `"2EL3NZ"`;

test/modules/helpers.spec.ts

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,6 @@ describe('helpers', () => {
1313
t.it('noArgs').it('some string', 'hello world');
1414
});
1515

16-
t.describe('replaceSymbolWithNumber', (t) => {
17-
t.it('noArgs')
18-
.it('only symbols', '!####')
19-
.it('some string', '^1234567890ß´°!"§$%&/()=?`+#*,..-;:_');
20-
});
21-
2216
t.describe('replaceSymbols', (t) => {
2317
t.it('noArgs')
2418
.it('only symbols', '#?*#?*')
@@ -31,12 +25,6 @@ describe('helpers', () => {
3125
.it('some string', '^1234567890ß´°!"§$%&/()=?`+#*,..-;:_L');
3226
});
3327

34-
t.describe('regexpStyleStringParse', (t) => {
35-
t.it('noArgs')
36-
.it('only symbols', '#{3}test[1-5]')
37-
.it('some string', 'Hello !#{3}test[1-5]');
38-
});
39-
4028
t.describe('fromRegExp', (t) => {
4129
t.it('with static string', 'Hello World!')
4230
.it('with static RegExp', /Hello World!/)
@@ -518,22 +506,6 @@ describe('helpers', () => {
518506
});
519507
});
520508

521-
describe('replaceSymbolWithNumber()', () => {
522-
describe('when no symbol passed in', () => {
523-
it("uses '#' by default", () => {
524-
const num = faker.helpers.replaceSymbolWithNumber('#AB');
525-
expect(num).toMatch(/\dAB/);
526-
});
527-
});
528-
529-
describe('when symbol passed in', () => {
530-
it('replaces that symbol with integers', () => {
531-
const num = faker.helpers.replaceSymbolWithNumber('#AB', 'A');
532-
expect(num).toMatch(/#\dB/);
533-
});
534-
});
535-
});
536-
537509
describe('replaceSymbols()', () => {
538510
it('returns empty string with no arguments', () => {
539511
expect(faker.helpers.replaceSymbols()).toBe('');
@@ -588,50 +560,6 @@ describe('helpers', () => {
588560
});
589561
});
590562

591-
describe('regexpStyleStringParse()', () => {
592-
it('returns an empty string when called without param', () => {
593-
expect(faker.helpers.regexpStyleStringParse()).toBe('');
594-
});
595-
596-
it('deals with range repeat', () => {
597-
const string = faker.helpers.regexpStyleStringParse('#{5,10}');
598-
expect(string.length).toBeLessThanOrEqual(10);
599-
expect(string.length).toBeGreaterThanOrEqual(5);
600-
expect(string).toMatch(/^#{5,10}$/);
601-
});
602-
603-
it('flips the range when min > max', () => {
604-
const string = faker.helpers.regexpStyleStringParse('#{10,5}');
605-
expect(string.length).toBeLessThanOrEqual(10);
606-
expect(string.length).toBeGreaterThanOrEqual(5);
607-
expect(string).toMatch(/^#{5,10}$/);
608-
});
609-
610-
it('repeats string {n} number of times', () => {
611-
expect(faker.helpers.regexpStyleStringParse('%{10}')).toBe(
612-
'%'.repeat(10)
613-
);
614-
expect(faker.helpers.regexpStyleStringParse('%{30}')).toBe(
615-
'%'.repeat(30)
616-
);
617-
expect(faker.helpers.regexpStyleStringParse('%{5}')).toBe(
618-
'%'.repeat(5)
619-
);
620-
});
621-
622-
it('creates a numerical range', () => {
623-
const string = faker.helpers.regexpStyleStringParse('Hello[0-9]');
624-
expect(string).toMatch(/^Hello[0-9]$/);
625-
});
626-
627-
it('deals with multiple tokens in one string', () => {
628-
const string = faker.helpers.regexpStyleStringParse(
629-
'Test#{5}%{2,5}Testing**[1-5]**{10}END'
630-
);
631-
expect(string).toMatch(/^Test#{5}%{2,5}Testing\*\*[1-5]\*\*{10}END$/);
632-
});
633-
});
634-
635563
describe('fromRegExp()', () => {
636564
it('deals with range repeat', () => {
637565
const string = faker.helpers.fromRegExp(/#{5,10}/);

0 commit comments

Comments
 (0)