From 60228af882575696426ce211056cac6e5af7c096 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Sun, 10 Mar 2024 10:26:14 +0100 Subject: [PATCH 1/3] refactor(helpers)!: remove v8 deprecated helpers methods --- src/modules/helpers/index.ts | 67 +---------------- .../__snapshots__/helpers.spec.ts.snap | 36 ---------- test/modules/helpers.spec.ts | 72 ------------------- 3 files changed, 1 insertion(+), 174 deletions(-) diff --git a/src/modules/helpers/index.ts b/src/modules/helpers/index.ts index b86b2cd7d32..f62d90868fb 100644 --- a/src/modules/helpers/index.ts +++ b/src/modules/helpers/index.ts @@ -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'; @@ -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. * @@ -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. * @@ -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) { diff --git a/test/modules/__snapshots__/helpers.spec.ts.snap b/test/modules/__snapshots__/helpers.spec.ts.snap index 8ecb47a8a6e..d8a786024ba 100644 --- a/test/modules/__snapshots__/helpers.spec.ts.snap +++ b/test/modules/__snapshots__/helpers.spec.ts.snap @@ -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"`; @@ -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"`; @@ -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"`; diff --git a/test/modules/helpers.spec.ts b/test/modules/helpers.spec.ts index 5a2294bbc84..e60859efb1b 100644 --- a/test/modules/helpers.spec.ts +++ b/test/modules/helpers.spec.ts @@ -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', '#?*#?*') @@ -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!/) @@ -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(''); @@ -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}/); From b217c264631ce8f89e8c25648290ba66b0d2642b Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Sun, 10 Mar 2024 10:29:03 +0100 Subject: [PATCH 2/3] add m-guide --- docs/guide/upgrading_v9/2729.md | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 docs/guide/upgrading_v9/2729.md diff --git a/docs/guide/upgrading_v9/2729.md b/docs/guide/upgrading_v9/2729.md new file mode 100644 index 00000000000..e003091592f --- /dev/null +++ b/docs/guide/upgrading_v9/2729.md @@ -0,0 +1,8 @@ +### 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` | From 219e0073389575a4022c7852a4ad06eabfb9cb5e Mon Sep 17 00:00:00 2001 From: Matt Mayer Date: Mon, 11 Mar 2024 08:07:10 +0700 Subject: [PATCH 3/3] add better docs for removed methods --- docs/guide/upgrading_v9/2729.md | 40 +++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/docs/guide/upgrading_v9/2729.md b/docs/guide/upgrading_v9/2729.md index e003091592f..0e43183ca6f 100644 --- a/docs/guide/upgrading_v9/2729.md +++ b/docs/guide/upgrading_v9/2729.md @@ -6,3 +6,43 @@ Removed deprecated helpers methods | --------------------------------------- | -------------------------------------------------------------- | | `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 }); +```