From 7c09bc645fb4db73327a18357af01e6c1fe0ad87 Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Wed, 13 Jul 2022 23:13:27 +0200 Subject: [PATCH 01/48] feat(modules): StringModule --- src/faker.ts | 2 ++ src/index.ts | 1 + src/modules/string/index.ts | 16 ++++++++++++++++ 3 files changed, 19 insertions(+) create mode 100644 src/modules/string/index.ts diff --git a/src/faker.ts b/src/faker.ts index 0283bd6dd6e..f80b3fbf0a4 100644 --- a/src/faker.ts +++ b/src/faker.ts @@ -23,6 +23,7 @@ import { NameModule } from './modules/name'; import { PhoneModule } from './modules/phone'; import { RandomModule } from './modules/random'; import { ScienceModule } from './modules/science'; +import { StringModule } from './modules/string'; import { SystemModule } from './modules/system'; import { UniqueModule } from './modules/unique'; import { VehicleModule } from './modules/vehicle'; @@ -103,6 +104,7 @@ export class Faker { readonly name: NameModule = new NameModule(this); readonly phone: PhoneModule = new PhoneModule(this); readonly science: ScienceModule = new ScienceModule(this); + readonly string: StringModule = new StringModule(this); readonly system: SystemModule = new SystemModule(this); readonly vehicle: VehicleModule = new VehicleModule(this); readonly word: WordModule = new WordModule(this); diff --git a/src/index.ts b/src/index.ts index 984a8c758d5..a5f03746043 100644 --- a/src/index.ts +++ b/src/index.ts @@ -60,6 +60,7 @@ export type { GenderType, NameModule, SexType } from './modules/name'; export type { PhoneModule } from './modules/phone'; export type { RandomModule } from './modules/random'; export type { ChemicalElement, ScienceModule, Unit } from './modules/science'; +export type { StringModule } from './modules/string'; export type { SystemModule } from './modules/system'; export type { UniqueModule } from './modules/unique'; export type { VehicleModule } from './modules/vehicle'; diff --git a/src/modules/string/index.ts b/src/modules/string/index.ts new file mode 100644 index 00000000000..6899d8ef5a6 --- /dev/null +++ b/src/modules/string/index.ts @@ -0,0 +1,16 @@ +import type { Faker } from '../..'; + +/** + * Module to generate string related entries. + */ +export class StringModule { + constructor(private readonly faker: Faker) { + // Bind `this` so namespaced is working correctly + for (const name of Object.getOwnPropertyNames(StringModule.prototype)) { + if (name === 'constructor' || typeof this[name] !== 'function') { + continue; + } + this[name] = this[name].bind(this); + } + } +} From dd349175f7b1aee15d13039b6ad788f1ecc2f8ff Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Wed, 13 Jul 2022 23:13:58 +0200 Subject: [PATCH 02/48] feat(StringModule): uuid --- src/modules/string/index.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/modules/string/index.ts b/src/modules/string/index.ts index 6899d8ef5a6..ca472cda33d 100644 --- a/src/modules/string/index.ts +++ b/src/modules/string/index.ts @@ -13,4 +13,20 @@ export class StringModule { this[name] = this[name].bind(this); } } + + /** + * Returns a UUID v4 ([Universally Unique Identifier](https://en.wikipedia.org/wiki/Universally_unique_identifier)). + * + * @example + * faker.string.uuid() // '4136cd0b-d90b-4af7-b485-5d1ded8db252' + */ + uuid(): string { + const RFC4122_TEMPLATE = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'; + const replacePlaceholders = (placeholder: string) => { + const random = this.faker.datatype.number({ min: 0, max: 15 }); + const value = placeholder === 'x' ? random : (random & 0x3) | 0x8; + return value.toString(16); + }; + return RFC4122_TEMPLATE.replace(/[xy]/g, replacePlaceholders); + } } From ca54d0f161aa962db279c21b73bf7e213832cba6 Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Wed, 13 Jul 2022 23:18:42 +0200 Subject: [PATCH 03/48] test(StringModule): uuid --- test/__snapshots__/string.spec.ts.snap | 7 ++++ test/string.spec.ts | 46 ++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 test/__snapshots__/string.spec.ts.snap create mode 100644 test/string.spec.ts diff --git a/test/__snapshots__/string.spec.ts.snap b/test/__snapshots__/string.spec.ts.snap new file mode 100644 index 00000000000..7f10579c592 --- /dev/null +++ b/test/__snapshots__/string.spec.ts.snap @@ -0,0 +1,7 @@ +// Vitest Snapshot v1 + +exports[`string > seed: 42 > uuid() 1`] = `"5cf2bc99-2721-407d-992b-a00fbdf302f2"`; + +exports[`string > seed: 1211 > uuid() 1`] = `"e7ec32f0-a2a3-4c65-abbd-0caabde64dfd"`; + +exports[`string > seed: 1337 > uuid() 1`] = `"48234870-5389-445f-8b41-c61a52bf27dc"`; diff --git a/test/string.spec.ts b/test/string.spec.ts new file mode 100644 index 00000000000..d3fc2067c3b --- /dev/null +++ b/test/string.spec.ts @@ -0,0 +1,46 @@ +import { afterEach, describe, expect, it } from 'vitest'; +import { faker } from '../src'; +import type { StringModule } from '../src/modules/string'; +import { seededRuns } from './support/seededRuns'; + +const NON_SEEDED_BASED_RUN = 5; + +const functionNames: (keyof StringModule)[] = ['uuid']; + +describe('string', () => { + afterEach(() => { + faker.locale = 'en'; + }); + + for (const seed of seededRuns) { + describe(`seed: ${seed}`, () => { + for (const functionName of functionNames) { + it(`${functionName}()`, () => { + faker.seed(seed); + + const actual = faker.string[functionName](); + + expect(actual).toMatchSnapshot(); + }); + } + }); + } + + // Create and log-back the seed for debug purposes + faker.seed(Math.ceil(Math.random() * 1_000_000_000)); + + describe(`random seeded tests for seed ${JSON.stringify( + faker.seed() + )}`, () => { + for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) { + describe(`uuid()`, () => { + it('generates a valid UUID', () => { + const UUID = faker.string.uuid(); + const RFC4122 = + /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/; + expect(UUID).toMatch(RFC4122); + }); + }); + } + }); +}); From 36532a96214f0cc35934882082beca0d8b1e6d9e Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Wed, 13 Jul 2022 23:22:04 +0200 Subject: [PATCH 04/48] refactor(Datatype): deprecate uuid --- src/modules/datatype/index.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/modules/datatype/index.ts b/src/modules/datatype/index.ts index 9991175d55d..1fb2ff2d457 100644 --- a/src/modules/datatype/index.ts +++ b/src/modules/datatype/index.ts @@ -170,19 +170,23 @@ export class DatatypeModule { /** * Returns a UUID v4 ([Universally Unique Identifier](https://en.wikipedia.org/wiki/Universally_unique_identifier)). * + * @see faker.string.uuid() + * * @example * faker.datatype.uuid() // '4136cd0b-d90b-4af7-b485-5d1ded8db252' * * @since 5.5.0 + * + * @deprecated Use faker.string.uuid() instead. */ uuid(): string { - const RFC4122_TEMPLATE = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'; - const replacePlaceholders = (placeholder) => { - const random = this.number({ min: 0, max: 15 }); - const value = placeholder === 'x' ? random : (random & 0x3) | 0x8; - return value.toString(16); - }; - return RFC4122_TEMPLATE.replace(/[xy]/g, replacePlaceholders); + deprecated({ + deprecated: 'faker.datatype.uuid()', + proposed: 'faker.string.uuid()', + since: '8.0', + until: '9.0', + }); + return this.faker.string.uuid(); } /** From 6a2a9d8332dd1b310a49b21e6e8112707a254a4d Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Wed, 13 Jul 2022 23:26:08 +0200 Subject: [PATCH 05/48] feat(StringModule): hexadecimal --- src/modules/string/index.ts | 42 +++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/modules/string/index.ts b/src/modules/string/index.ts index ca472cda33d..6f29edbf9f1 100644 --- a/src/modules/string/index.ts +++ b/src/modules/string/index.ts @@ -29,4 +29,46 @@ export class StringModule { }; return RFC4122_TEMPLATE.replace(/[xy]/g, replacePlaceholders); } + + /** + * Returns a [hexadecimal](https://en.wikipedia.org/wiki/Hexadecimal) string. + * + * @param length Length of the generated string. Defaults to `1`. + * + * @example + * faker.string.hexadecimal() // 'b' + * faker.string.hexadecimal(10) // 'aE13F044fb' + */ + hexadecimal(length = 1): string { + let wholeString = ''; + + while (wholeString.length < length) { + wholeString += this.faker.helpers.arrayElement([ + '0', + '1', + '2', + '3', + '4', + '5', + '6', + '7', + '8', + '9', + 'a', + 'b', + 'c', + 'd', + 'e', + 'f', + 'A', + 'B', + 'C', + 'D', + 'E', + 'F', + ]); + } + + return wholeString; + } } From 1dbad5edb5022e4cfadee23b088a4b7a95b70d2d Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Wed, 13 Jul 2022 23:29:21 +0200 Subject: [PATCH 06/48] test(StringModule): hexadecimal --- test/__snapshots__/string.spec.ts.snap | 12 ++++++++++++ test/string.spec.ts | 25 ++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/test/__snapshots__/string.spec.ts.snap b/test/__snapshots__/string.spec.ts.snap index 7f10579c592..4270aaf2c32 100644 --- a/test/__snapshots__/string.spec.ts.snap +++ b/test/__snapshots__/string.spec.ts.snap @@ -1,7 +1,19 @@ // Vitest Snapshot v1 +exports[`string > seed: 42 > hexadecimal() > should return a deterministic hex of given length 1`] = `"8BE4ABdd39321aD7d3fe01FfCE404F4d6db0906bd8"`; + +exports[`string > seed: 42 > hexadecimal() 1`] = `"8"`; + exports[`string > seed: 42 > uuid() 1`] = `"5cf2bc99-2721-407d-992b-a00fbdf302f2"`; +exports[`string > seed: 1211 > hexadecimal() > should return a deterministic hex of given length 1`] = `"EaDB42F0e3f4A973fAB0AeefCE96DFCF49cD438dF9"`; + +exports[`string > seed: 1211 > hexadecimal() 1`] = `"E"`; + exports[`string > seed: 1211 > uuid() 1`] = `"e7ec32f0-a2a3-4c65-abbd-0caabde64dfd"`; +exports[`string > seed: 1337 > hexadecimal() > should return a deterministic hex of given length 1`] = `"5c346ba075bd57F5A62B82d72AF39CBBB07a98cbA8"`; + +exports[`string > seed: 1337 > hexadecimal() 1`] = `"5"`; + exports[`string > seed: 1337 > uuid() 1`] = `"48234870-5389-445f-8b41-c61a52bf27dc"`; diff --git a/test/string.spec.ts b/test/string.spec.ts index d3fc2067c3b..a93c7e43123 100644 --- a/test/string.spec.ts +++ b/test/string.spec.ts @@ -5,7 +5,7 @@ import { seededRuns } from './support/seededRuns'; const NON_SEEDED_BASED_RUN = 5; -const functionNames: (keyof StringModule)[] = ['uuid']; +const functionNames: (keyof StringModule)[] = ['uuid', 'hexadecimal']; describe('string', () => { afterEach(() => { @@ -23,6 +23,15 @@ describe('string', () => { expect(actual).toMatchSnapshot(); }); } + + describe('hexadecimal()', () => { + it('should return a deterministic hex of given length', () => { + faker.seed(seed); + + const actual = faker.string.hexadecimal(42); + expect(actual).toMatchSnapshot(); + }); + }); }); } @@ -41,6 +50,20 @@ describe('string', () => { expect(UUID).toMatch(RFC4122); }); }); + + describe(`hexadecimal()`, () => { + it('generates single hex character when no additional argument was provided', () => { + const hex = faker.string.hexadecimal(); + expect(hex).toMatch(/^[0-9a-f]*$/i); + expect(hex).toHaveLength(1); + }); + + it('generates a random hex string', () => { + const hex = faker.string.hexadecimal(5); + expect(hex).toMatch(/^[0-9a-f]*$/i); + expect(hex).toHaveLength(5); + }); + }); } }); }); From 8ce8c774d84420500c4eee79a3e1533bcb6a70db Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Wed, 13 Jul 2022 23:33:52 +0200 Subject: [PATCH 07/48] refactor(Datatype): deprecate hexadecimal --- src/modules/color/index.ts | 5 +--- src/modules/database/index.ts | 6 +---- src/modules/datatype/index.ts | 45 +++++++++-------------------------- src/modules/finance/index.ts | 8 +++---- src/modules/git/index.ts | 12 ++-------- test/datatype.spec.ts | 8 +++---- 6 files changed, 22 insertions(+), 62 deletions(-) diff --git a/src/modules/color/index.ts b/src/modules/color/index.ts index c6dbb0a476e..f14f4d2b1ed 100644 --- a/src/modules/color/index.ts +++ b/src/modules/color/index.ts @@ -313,10 +313,7 @@ export class ColorModule { let color: string | number[]; let cssFunction: CSSFunction = 'rgb'; if (format === 'hex') { - color = this.faker.datatype.hexadecimal({ - length: includeAlpha ? 8 : 6, - prefix: '', - }); + color = this.faker.string.hexadecimal(includeAlpha ? 8 : 6); color = formatHexColor(color, options); return color; } diff --git a/src/modules/database/index.ts b/src/modules/database/index.ts index 8a4291f9ceb..0166e2fbef5 100644 --- a/src/modules/database/index.ts +++ b/src/modules/database/index.ts @@ -79,10 +79,6 @@ export class DatabaseModule { * @since 6.2.0 */ mongodbObjectId(): string { - return this.faker.datatype.hexadecimal({ - length: 24, - case: 'lower', - prefix: '', - }); + return this.faker.string.hexadecimal(24).toLowerCase(); } } diff --git a/src/modules/datatype/index.ts b/src/modules/datatype/index.ts index 1fb2ff2d457..e3f39936e58 100644 --- a/src/modules/datatype/index.ts +++ b/src/modules/datatype/index.ts @@ -209,6 +209,8 @@ export class DatatypeModule { * @param options.prefix Prefix for the generated number. Defaults to `'0x'`. * @param options.case Case of the generated number. Defaults to `'mixed'`. * + * @see faker.string.hexadecimal() + * * @example * faker.datatype.hexadecimal() // '0xB' * faker.datatype.hexadecimal({ length: 10 }) // '0xaE13d044cB' @@ -220,19 +222,21 @@ export class DatatypeModule { * faker.datatype.hexadecimal({ length: 10, prefix: '0x', case: 'mixed' }) // '0xAdE330a4D1' * * @since 6.1.2 + * + * @deprecated */ hexadecimal( options: | { length?: number; prefix?: string; case?: 'lower' | 'upper' | 'mixed' } | number = {} ): string { + deprecated({ + deprecated: 'faker.datatype.hexadecimal()', + proposed: 'faker.string.hexadecimal()', + since: '8.0', + until: '9.0', + }); if (typeof options === 'number') { - deprecated({ - deprecated: 'faker.datatype.hexadecimal(length)', - proposed: 'faker.datatype.hexadecimal({ length })', - since: '7.5', - until: '8.0', - }); options = { length: options, }; @@ -240,34 +244,7 @@ export class DatatypeModule { const { length = 1, prefix = '0x', case: letterCase = 'mixed' } = options; - let wholeString = ''; - - for (let i = 0; i < length; i++) { - wholeString += this.faker.helpers.arrayElement([ - '0', - '1', - '2', - '3', - '4', - '5', - '6', - '7', - '8', - '9', - 'a', - 'b', - 'c', - 'd', - 'e', - 'f', - 'A', - 'B', - 'C', - 'D', - 'E', - 'F', - ]); - } + let wholeString = this.faker.string.hexadecimal(length); if (letterCase === 'upper') { wholeString = wholeString.toUpperCase(); diff --git a/src/modules/finance/index.ts b/src/modules/finance/index.ts index d1580603e24..905c60689b9 100644 --- a/src/modules/finance/index.ts +++ b/src/modules/finance/index.ts @@ -353,11 +353,9 @@ export class FinanceModule { * @since 5.0.0 */ ethereumAddress(): string { - const address = this.faker.datatype.hexadecimal({ - length: 40, - case: 'lower', - }); - return address; + const addressPrefix = '0x'; + const address = this.faker.string.hexadecimal(40).toLowerCase(); + return addressPrefix + address; } /** diff --git a/src/modules/git/index.ts b/src/modules/git/index.ts index d92e34540c8..41b0ca56503 100644 --- a/src/modules/git/index.ts +++ b/src/modules/git/index.ts @@ -100,11 +100,7 @@ export class GitModule { * @since 5.0.0 */ commitSha(): string { - return this.faker.datatype.hexadecimal({ - length: 40, - case: 'lower', - prefix: '', - }); + return this.faker.string.hexadecimal(40).toLowerCase(); } /** @@ -116,10 +112,6 @@ export class GitModule { * @since 5.0.0 */ shortSha(): string { - return this.faker.datatype.hexadecimal({ - length: 7, - case: 'lower', - prefix: '', - }); + return this.faker.string.hexadecimal(7).toLowerCase(); } } diff --git a/test/datatype.spec.ts b/test/datatype.spec.ts index 25847e07415..bbfcb8c8e99 100644 --- a/test/datatype.spec.ts +++ b/test/datatype.spec.ts @@ -331,8 +331,8 @@ describe('datatype', () => { describe('hexadecimal', () => { it('generates single hex character when no additional argument was provided', () => { const hex = faker.datatype.hexadecimal(); - expect(hex).toMatch(/^(0x)[0-9a-f]{1}$/i); - expect(hex.substring(2)).toHaveLength(1); + expect(hex).toMatch(/^[0-9a-f]{1}$/i); + expect(hex).toHaveLength(1); }); it('generates a hex string with a provided prefix', () => { @@ -343,8 +343,8 @@ describe('datatype', () => { it('generates a random hex string with a provided length', () => { const hex = faker.datatype.hexadecimal({ length: 5 }); - expect(hex).toMatch(/^(0x)[0-9a-f]+$/i); - expect(hex.substring(2)).toHaveLength(5); + expect(hex).toMatch(/^[0-9a-f]+$/i); + expect(hex).toHaveLength(5); }); it('generates a hex string with a provided casing', () => { From 197d385b33b888f5a3e63404b955fb2ba784a0a4 Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Wed, 13 Jul 2022 23:49:37 +0200 Subject: [PATCH 08/48] feat(StringModule): random --- src/modules/string/index.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/modules/string/index.ts b/src/modules/string/index.ts index 6f29edbf9f1..eca1510ba3e 100644 --- a/src/modules/string/index.ts +++ b/src/modules/string/index.ts @@ -14,6 +14,37 @@ export class StringModule { } } + /** + * Returns a string containing UTF-16 chars between 33 and 125 (`!` to `}`). + * + * @param length Length of the generated string. Max length is `2^20`. Defaults to `10`. + * + * @example + * faker.string.random() // 'Zo!.:*e>wR' + * faker.string.random(5) // '6Bye8' + */ + random(length = 10): string { + const maxLength = Math.pow(2, 20); + if (length >= maxLength) { + length = maxLength; + } + + const charCodeOption = { + min: 33, + max: 125, + }; + + let returnString = ''; + + while (returnString.length < length) { + returnString += String.fromCharCode( + this.faker.datatype.number(charCodeOption) + ); + } + + return returnString; + } + /** * Returns a UUID v4 ([Universally Unique Identifier](https://en.wikipedia.org/wiki/Universally_unique_identifier)). * From 2735d9046610d7bd0dfaa78275140f42c0879c66 Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Thu, 14 Jul 2022 00:00:30 +0200 Subject: [PATCH 09/48] test(StringModule): random --- test/__snapshots__/string.spec.ts.snap | 12 ++++++++ test/string.spec.ts | 38 +++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/test/__snapshots__/string.spec.ts.snap b/test/__snapshots__/string.spec.ts.snap index 4270aaf2c32..90ba1a858ec 100644 --- a/test/__snapshots__/string.spec.ts.snap +++ b/test/__snapshots__/string.spec.ts.snap @@ -1,19 +1,31 @@ // Vitest Snapshot v1 +exports[`string > random() > should return a deterministic string of given length 1`] = `"Cky2eiXX/J/*&Kq@X.b]\\"&{dnx4!1}2Z=YQ!I# random() > should return a deterministic string of given length 2`] = `"9U/4:SK$>6QX9@{:e=+kD)[B,e|/Jqjjj!BLGDWQgC"`; + +exports[`string > random() > should return a deterministic string of given length 3`] = `"wKti5-}$_/\`4hHA0afl\\"h^]dnwI seed: 42 > hexadecimal() > should return a deterministic hex of given length 1`] = `"8BE4ABdd39321aD7d3fe01FfCE404F4d6db0906bd8"`; exports[`string > seed: 42 > hexadecimal() 1`] = `"8"`; +exports[`string > seed: 42 > random() 1`] = `"Cky2eiXX/J"`; + exports[`string > seed: 42 > uuid() 1`] = `"5cf2bc99-2721-407d-992b-a00fbdf302f2"`; exports[`string > seed: 1211 > hexadecimal() > should return a deterministic hex of given length 1`] = `"EaDB42F0e3f4A973fAB0AeefCE96DFCF49cD438dF9"`; exports[`string > seed: 1211 > hexadecimal() 1`] = `"E"`; +exports[`string > seed: 1211 > random() 1`] = `"wKti5-}$_/"`; + exports[`string > seed: 1211 > uuid() 1`] = `"e7ec32f0-a2a3-4c65-abbd-0caabde64dfd"`; exports[`string > seed: 1337 > hexadecimal() > should return a deterministic hex of given length 1`] = `"5c346ba075bd57F5A62B82d72AF39CBBB07a98cbA8"`; exports[`string > seed: 1337 > hexadecimal() 1`] = `"5"`; +exports[`string > seed: 1337 > random() 1`] = `"9U/4:SK$>6"`; + exports[`string > seed: 1337 > uuid() 1`] = `"48234870-5389-445f-8b41-c61a52bf27dc"`; diff --git a/test/string.spec.ts b/test/string.spec.ts index a93c7e43123..cf3596fc1a6 100644 --- a/test/string.spec.ts +++ b/test/string.spec.ts @@ -5,7 +5,7 @@ import { seededRuns } from './support/seededRuns'; const NON_SEEDED_BASED_RUN = 5; -const functionNames: (keyof StringModule)[] = ['uuid', 'hexadecimal']; +const functionNames: (keyof StringModule)[] = ['uuid', 'hexadecimal', 'random']; describe('string', () => { afterEach(() => { @@ -33,6 +33,15 @@ describe('string', () => { }); }); }); + + describe('random()', () => { + it('should return a deterministic string of given length', () => { + faker.seed(seed); + + const actual = faker.string.random(42); + expect(actual).toMatchSnapshot(); + }); + }); } // Create and log-back the seed for debug purposes @@ -42,6 +51,33 @@ describe('string', () => { faker.seed() )}`, () => { for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) { + describe('random()', () => { + it('should generate a string value', () => { + const generatedString = faker.string.random(); + expect(generatedString).toBeTypeOf('string'); + expect(generatedString).toHaveLength(10); + }); + + it('should return empty string if negative length is passed', () => { + const negativeValue = faker.datatype.number({ min: -1000, max: -1 }); + const generatedString = faker.string.random(negativeValue); + expect(generatedString).toBe(''); + expect(generatedString).toHaveLength(0); + }); + + it('should return string with length of 2^20 if bigger length value is passed', () => { + const overMaxValue = Math.pow(2, 28); + const generatedString = faker.string.random(overMaxValue); + expect(generatedString).toHaveLength(Math.pow(2, 20)); + }); + + it('should return string with a specific length', () => { + const length = 1337; + const generatedString = faker.string.random(length); + expect(generatedString).toHaveLength(length); + }); + }); + describe(`uuid()`, () => { it('generates a valid UUID', () => { const UUID = faker.string.uuid(); From 0967750aff34c0e9c472be3a9acbb96503319676 Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Thu, 14 Jul 2022 00:02:05 +0200 Subject: [PATCH 10/48] refactor(Datatype): deprecate string --- src/modules/datatype/index.ts | 34 +++++++++++++++------------------- test/helpers.spec.ts | 4 ++-- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/src/modules/datatype/index.ts b/src/modules/datatype/index.ts index e3f39936e58..5f0229e1df3 100644 --- a/src/modules/datatype/index.ts +++ b/src/modules/datatype/index.ts @@ -141,30 +141,24 @@ export class DatatypeModule { * * @param length Length of the generated string. Max length is `2^20`. Defaults to `10`. * + * @see faker.string.random() + * * @example * faker.datatype.string() // 'Zo!.:*e>wR' * faker.datatype.string(5) // '6Bye8' * * @since 5.5.0 + * + * @deprecated Use faker.string.random() instead. */ string(length = 10): string { - const maxLength = Math.pow(2, 20); - if (length >= maxLength) { - length = maxLength; - } - - const charCodeOption = { - min: 33, - max: 125, - }; - - let returnString = ''; - - for (let i = 0; i < length; i++) { - returnString += String.fromCharCode(this.number(charCodeOption)); - } - - return returnString; + deprecated({ + deprecated: 'faker.datatype.string()', + proposed: 'faker.string.random()', + since: '8.0', + until: '9.0', + }); + return this.faker.string.random(length); } /** @@ -268,7 +262,9 @@ export class DatatypeModule { const returnObject: Record = {}; properties.forEach((prop) => { - returnObject[prop] = this.boolean() ? this.string() : this.number(); + returnObject[prop] = this.boolean() + ? this.faker.string.random() + : this.number(); }); return JSON.stringify(returnObject); @@ -287,7 +283,7 @@ export class DatatypeModule { */ array(length = 10): Array { return Array.from({ length }).map(() => - this.boolean() ? this.string() : this.number() + this.boolean() ? this.faker.string.random() : this.number() ); } diff --git a/test/helpers.spec.ts b/test/helpers.spec.ts index ce8fb73387e..74803a1781a 100644 --- a/test/helpers.spec.ts +++ b/test/helpers.spec.ts @@ -408,7 +408,7 @@ describe('helpers', () => { it('supports function replace values faker values', () => { const actual = faker.helpers.mustache('1{{value}}3', { - value: faker.datatype.string(2), + value: faker.string.random(2), }); expect(actual).toHaveLength(4); @@ -416,7 +416,7 @@ describe('helpers', () => { it('supports function replace values faker function', () => { const actual = faker.helpers.mustache('1{{value}}3', { - value: () => faker.datatype.string(3), + value: () => faker.string.random(3), }); expect(actual).toHaveLength(5); From 578b79a32b39e7d7da424215f9878fc642a08da0 Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Thu, 14 Jul 2022 14:46:55 +0200 Subject: [PATCH 11/48] feat(StringModule): alpha --- src/modules/string/index.ts | 145 ++++++++++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) diff --git a/src/modules/string/index.ts b/src/modules/string/index.ts index eca1510ba3e..393b80b9df9 100644 --- a/src/modules/string/index.ts +++ b/src/modules/string/index.ts @@ -1,4 +1,82 @@ import type { Faker } from '../..'; +import { FakerError } from '../../errors/faker-error'; +import type { LiteralUnion } from '../../utils/types'; + +export type Casing = 'upper' | 'lower' | 'mixed'; + +const UPPER_CHARS: readonly string[] = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(''); +const LOWER_CHARS: readonly string[] = 'abcdefghijklmnopqrstuvwxyz'.split(''); + +export type LowerAlphaChar = + | 'a' + | 'b' + | 'c' + | 'd' + | 'e' + | 'f' + | 'g' + | 'h' + | 'i' + | 'j' + | 'k' + | 'l' + | 'm' + | 'n' + | 'o' + | 'p' + | 'q' + | 'r' + | 's' + | 't' + | 'u' + | 'v' + | 'w' + | 'x' + | 'y' + | 'z'; + +export type UpperAlphaChar = + | 'A' + | 'B' + | 'C' + | 'D' + | 'E' + | 'F' + | 'G' + | 'H' + | 'I' + | 'J' + | 'K' + | 'L' + | 'M' + | 'N' + | 'O' + | 'P' + | 'Q' + | 'R' + | 'S' + | 'T' + | 'U' + | 'V' + | 'W' + | 'X' + | 'Y' + | 'Z'; + +export type NumericChar = + | '0' + | '1' + | '2' + | '3' + | '4' + | '5' + | '6' + | '7' + | '8' + | '9'; + +export type AlphaChar = LowerAlphaChar | UpperAlphaChar; +export type AlphaNumericChar = AlphaChar | NumericChar; /** * Module to generate string related entries. @@ -45,6 +123,73 @@ export class StringModule { return returnString; } + /** + * Generating a string consisting of letters in the English alphabet. + * + * @param options Either the number of characters or an options instance. Defaults to `{ count: 1, casing: 'lower', bannedChars: [] }`. + * @param options.count The number of characters to generate. Defaults to `1`. + * @param options.casing The casing of the characters. Defaults to `'lower'`. + * @param options.upcase Deprecated, use `casing: 'upper'` instead. + * @param options.bannedChars An array with characters to exclude. Defaults to `[]`. + * + * @example + * faker.string.alpha() // 'b' + * faker.string.alpha(10) // 'qccrabobaf' + * faker.string.alpha({ count: 5, casing: 'upper', bannedChars: ['A'] }) // 'DTCIC' + */ + alpha( + options: + | number + | { + count?: number; + casing?: Casing; + bannedChars?: readonly LiteralUnion[] | string; + } = {} + ): string { + if (typeof options === 'number') { + options = { + count: options, + }; + } + + const { count = 1, casing = 'mixed' } = options; + let { bannedChars = [] } = options; + + if (typeof bannedChars === 'string') { + bannedChars = bannedChars.split(''); + } + + if (count <= 0) { + return ''; + } + + let charsArray: string[]; + switch (casing) { + case 'upper': + charsArray = [...UPPER_CHARS]; + break; + case 'lower': + charsArray = [...LOWER_CHARS]; + break; + case 'mixed': + default: + charsArray = [...LOWER_CHARS, ...UPPER_CHARS]; + break; + } + + charsArray = charsArray.filter((elem) => !bannedChars.includes(elem)); + + if (charsArray.length === 0) { + throw new FakerError( + 'Unable to generate string, because all possible characters are banned.' + ); + } + + return Array.from({ length: count }, () => + this.faker.helpers.arrayElement(charsArray) + ).join(''); + } + /** * Returns a UUID v4 ([Universally Unique Identifier](https://en.wikipedia.org/wiki/Universally_unique_identifier)). * From 63c3ff46246fa8e884476c1478549a1c5d8ed455 Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Thu, 14 Jul 2022 15:00:51 +0200 Subject: [PATCH 12/48] test(StringModule): alpha --- test/__snapshots__/string.spec.ts.snap | 6 ++ test/string.spec.ts | 110 ++++++++++++++++++++++++- 2 files changed, 114 insertions(+), 2 deletions(-) diff --git a/test/__snapshots__/string.spec.ts.snap b/test/__snapshots__/string.spec.ts.snap index 90ba1a858ec..c3de3c7575e 100644 --- a/test/__snapshots__/string.spec.ts.snap +++ b/test/__snapshots__/string.spec.ts.snap @@ -6,6 +6,8 @@ exports[`string > random() > should return a deterministic string of given lengt exports[`string > random() > should return a deterministic string of given length 3`] = `"wKti5-}$_/\`4hHA0afl\\"h^]dnwI seed: 42 > alpha() 1`] = `"t"`; + exports[`string > seed: 42 > hexadecimal() > should return a deterministic hex of given length 1`] = `"8BE4ABdd39321aD7d3fe01FfCE404F4d6db0906bd8"`; exports[`string > seed: 42 > hexadecimal() 1`] = `"8"`; @@ -14,6 +16,8 @@ exports[`string > seed: 42 > random() 1`] = `"Cky2eiXX/J"`; exports[`string > seed: 42 > uuid() 1`] = `"5cf2bc99-2721-407d-992b-a00fbdf302f2"`; +exports[`string > seed: 1211 > alpha() 1`] = `"W"`; + exports[`string > seed: 1211 > hexadecimal() > should return a deterministic hex of given length 1`] = `"EaDB42F0e3f4A973fAB0AeefCE96DFCF49cD438dF9"`; exports[`string > seed: 1211 > hexadecimal() 1`] = `"E"`; @@ -22,6 +26,8 @@ exports[`string > seed: 1211 > random() 1`] = `"wKti5-}$_/"`; exports[`string > seed: 1211 > uuid() 1`] = `"e7ec32f0-a2a3-4c65-abbd-0caabde64dfd"`; +exports[`string > seed: 1337 > alpha() 1`] = `"n"`; + exports[`string > seed: 1337 > hexadecimal() > should return a deterministic hex of given length 1`] = `"5c346ba075bd57F5A62B82d72AF39CBBB07a98cbA8"`; exports[`string > seed: 1337 > hexadecimal() 1`] = `"5"`; diff --git a/test/string.spec.ts b/test/string.spec.ts index cf3596fc1a6..72bce415328 100644 --- a/test/string.spec.ts +++ b/test/string.spec.ts @@ -1,11 +1,16 @@ import { afterEach, describe, expect, it } from 'vitest'; -import { faker } from '../src'; +import { faker, FakerError } from '../src'; import type { StringModule } from '../src/modules/string'; import { seededRuns } from './support/seededRuns'; const NON_SEEDED_BASED_RUN = 5; -const functionNames: (keyof StringModule)[] = ['uuid', 'hexadecimal', 'random']; +const functionNames: (keyof StringModule)[] = [ + 'uuid', + 'hexadecimal', + 'random', + 'alpha', +]; describe('string', () => { afterEach(() => { @@ -100,6 +105,107 @@ describe('string', () => { expect(hex).toHaveLength(5); }); }); + + describe('alpha()', () => { + it('should return single letter when no count provided', () => { + const actual = faker.string.alpha(); + + expect(actual).toHaveLength(1); + }); + + it('should return any letters when no option is provided', () => { + const actual = faker.string.alpha(); + + expect(actual).toMatch(/^[a-zA-Z]$/); + }); + + it.each([ + ['upper', /^[A-Z]{250}$/], + ['lower', /^[a-z]{250}$/], + ['mixed', /^[a-zA-Z]{250}$/], + ] as const)('should return %s-case', (casing, pattern) => { + const actual = faker.string.alpha({ count: 250, casing }); + expect(actual).toMatch(pattern); + }); + + it('should generate many random letters', () => { + const actual = faker.string.alpha(5); + + expect(actual).toHaveLength(5); + }); + + it.each([0, -1, -100])( + 'should return empty string when length is <= 0', + (length) => { + const actual = faker.string.alpha(length); + + expect(actual).toBe(''); + } + ); + + it('should be able to ban some characters', () => { + const actual = faker.string.alpha({ + count: 5, + bannedChars: ['a', 'p'], + casing: 'lower', + }); + + expect(actual).toHaveLength(5); + expect(actual).toMatch(/^[b-oq-z]{5}$/); + }); + + it('should be able to ban some characters via string', () => { + const actual = faker.string.alpha({ + count: 5, + bannedChars: 'ap', + casing: 'lower', + }); + + expect(actual).toHaveLength(5); + expect(actual).toMatch(/^[b-oq-z]{5}$/); + }); + + it('should be able handle mistake in banned characters array', () => { + const alphaText = faker.string.alpha({ + count: 5, + bannedChars: ['a', 'a', 'p'], + casing: 'lower', + }); + + expect(alphaText).toHaveLength(5); + expect(alphaText).toMatch(/^[b-oq-z]{5}$/); + }); + + it('should throw if all possible characters being banned', () => { + const bannedChars = 'abcdefghijklmnopqrstuvwxyz'.split(''); + expect(() => + faker.string.alpha({ + count: 5, + bannedChars, + casing: 'lower', + }) + ).toThrowError( + new FakerError( + 'Unable to generate string, because all possible characters are banned.' + ) + ); + }); + + it('should not mutate the input object', () => { + const input: { + count: number; + casing: 'mixed'; + bannedChars: string[]; + } = Object.freeze({ + count: 5, + casing: 'mixed', + bannedChars: ['a', '%'], + }); + + expect(() => faker.string.alpha(input)).not.toThrow(); + expect(input.bannedChars).toEqual(['a', '%']); + }); + }); } }); }); From f0020b1de072cac515f7aab6c25281d5ce82db47 Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Thu, 14 Jul 2022 21:55:18 +0200 Subject: [PATCH 13/48] refactor(Random): deprecate alpha --- src/modules/random/index.ts | 72 ++++---------------------- src/modules/vehicle/index.ts | 6 +-- test/__snapshots__/random.spec.ts.snap | 12 ++--- test/fake.spec.ts | 6 ++- test/random.spec.ts | 8 ++- 5 files changed, 30 insertions(+), 74 deletions(-) diff --git a/src/modules/random/index.ts b/src/modules/random/index.ts index b4034343d88..3f0e5ce9996 100644 --- a/src/modules/random/index.ts +++ b/src/modules/random/index.ts @@ -266,85 +266,35 @@ export class RandomModule { * @param options Either the number of characters or an options instance. Defaults to `{ count: 1, casing: 'lower', bannedChars: [] }`. * @param options.count The number of characters to generate. Defaults to `1`. * @param options.casing The casing of the characters. Defaults to `'lower'`. - * @param options.upcase Deprecated, use `casing: 'upper'` instead. * @param options.bannedChars An array with characters to exclude. Defaults to `[]`. * + * @see faker.string.alpha() + * * @example * faker.random.alpha() // 'b' * faker.random.alpha(10) // 'qccrabobaf' * faker.random.alpha({ count: 5, casing: 'upper', bannedChars: ['A'] }) // 'DTCIC' * * @since 5.0.0 + * + * @deprecated Use faker.string.alpha() instead. */ alpha( options: | number | { count?: number; - /** - * @deprecated Use `casing` instead. - */ - upcase?: boolean; casing?: Casing; bannedChars?: readonly LiteralUnion[] | string; } = {} ): string { - if (typeof options === 'number') { - options = { - count: options, - }; - } - - const { count = 1, upcase } = options; - let { bannedChars = [] } = options; - - if (typeof bannedChars === 'string') { - bannedChars = bannedChars.split(''); - } - - if (count <= 0) { - return ''; - } - - const { - // Switch to 'mixed' with v8.0 - casing = upcase ? 'upper' : 'lower', - } = options; - - if (upcase != null) { - deprecated({ - deprecated: 'faker.random.alpha({ upcase: true })', - proposed: "faker.random.alpha({ casing: 'upper' })", - since: '7.0', - until: '8.0', - }); - } - - let charsArray: string[]; - switch (casing) { - case 'upper': - charsArray = [...UPPER_CHARS]; - break; - case 'lower': - charsArray = [...LOWER_CHARS]; - break; - case 'mixed': - default: - charsArray = [...LOWER_CHARS, ...UPPER_CHARS]; - break; - } - - charsArray = arrayRemove(charsArray, bannedChars); - - if (charsArray.length === 0) { - throw new FakerError( - 'Unable to generate string, because all possible characters are banned.' - ); - } - - return Array.from({ length: count }, () => - this.faker.helpers.arrayElement(charsArray) - ).join(''); + deprecated({ + deprecated: 'faker.random.alpha()', + proposed: 'faker.string.alpha()', + since: '8.0', + until: '9.0', + }); + return this.faker.string.alpha(options); } /** diff --git a/src/modules/vehicle/index.ts b/src/modules/vehicle/index.ts index cd01ece30c9..3db92a9c929 100644 --- a/src/modules/vehicle/index.ts +++ b/src/modules/vehicle/index.ts @@ -91,7 +91,7 @@ export class VehicleModule { return `${this.faker.random.alphaNumeric(10, { casing: 'upper', bannedChars, - })}${this.faker.random.alpha({ + })}${this.faker.string.alpha({ count: 1, casing: 'upper', bannedChars, @@ -123,7 +123,7 @@ export class VehicleModule { * @since 5.4.0 */ vrm(): string { - return `${this.faker.random.alpha({ + return `${this.faker.string.alpha({ count: 2, casing: 'upper', })}${this.faker.datatype.number({ @@ -132,7 +132,7 @@ export class VehicleModule { })}${this.faker.datatype.number({ min: 0, max: 9, - })}${this.faker.random.alpha({ count: 3, casing: 'upper' })}`.toUpperCase(); + })}${this.faker.string.alpha({ count: 3, casing: 'upper' })}`.toUpperCase(); } /** diff --git a/test/__snapshots__/random.spec.ts.snap b/test/__snapshots__/random.spec.ts.snap index ce472e9719f..b50ab66b7ce 100644 --- a/test/__snapshots__/random.spec.ts.snap +++ b/test/__snapshots__/random.spec.ts.snap @@ -1,8 +1,8 @@ // Vitest Snapshot v1 -exports[`random > 42 > alpha > noArgs 1`] = `"j"`; +exports[`random > 42 > alpha > noArgs 1`] = `"t"`; -exports[`random > 42 > alpha > withLength 1`] = `"juyet"`; +exports[`random > 42 > alpha > withLength 1`] = `"tPXjM"`; exports[`random > 42 > alphaNumeric > noArgs 1`] = `"d"`; @@ -20,9 +20,9 @@ exports[`random > 42 > words > noArgs 1`] = `"lavender Shoes"`; exports[`random > 42 > words > withLength 1`] = `"responsive comeback Neptunium Hip deposit"`; -exports[`random > 1211 > alpha > noArgs 1`] = `"y"`; +exports[`random > 1211 > alpha > noArgs 1`] = `"W"`; -exports[`random > 1211 > alpha > withLength 1`] = `"ylxuf"`; +exports[`random > 1211 > alpha > withLength 1`] = `"WxUOl"`; exports[`random > 1211 > alphaNumeric > noArgs 1`] = `"x"`; @@ -40,9 +40,9 @@ exports[`random > 1211 > words > noArgs 1`] = `"invoice Cyclocross assault"`; exports[`random > 1211 > words > withLength 1`] = `"gah strictly Rustic assault Manager"`; -exports[`random > 1337 > alpha > noArgs 1`] = `"g"`; +exports[`random > 1337 > alpha > noArgs 1`] = `"n"`; -exports[`random > 1337 > alpha > withLength 1`] = `"goefh"`; +exports[`random > 1337 > alpha > withLength 1`] = `"nDilo"`; exports[`random > 1337 > alphaNumeric > noArgs 1`] = `"9"`; diff --git a/test/fake.spec.ts b/test/fake.spec.ts index 8c0a0a8bf29..816d8fcb166 100644 --- a/test/fake.spec.ts +++ b/test/fake.spec.ts @@ -99,11 +99,13 @@ describe('fake', () => { }); it('should be able to handle random }} brackets', () => { - expect(faker.fake('}}hello{{random.alpha}}')).toMatch(/^}}hello[a-z]$/); + expect(faker.fake('}}hello{{string.alpha}}')).toMatch( + /^}}hello[a-zA-Z]$/ + ); }); it('should be able to handle connected brackets', () => { - expect(faker.fake('{{{random.alpha}}}')).toMatch(/^{[a-z]}$/); + expect(faker.fake('{{{string.alpha}}}')).toMatch(/^{[a-zA-Z]}$/); }); it('should be able to handle empty brackets', () => { diff --git a/test/random.spec.ts b/test/random.spec.ts index 6e64f5f1ee8..ea634dfd32c 100644 --- a/test/random.spec.ts +++ b/test/random.spec.ts @@ -127,10 +127,10 @@ describe('random', () => { expect(actual).toHaveLength(1); }); - it('should return lowercase letter when no upcase option provided', () => { + it('should return mixed letter when no option provided', () => { const actual = faker.random.alpha(); - expect(actual).toMatch(/^[a-z]$/); + expect(actual).toMatch(/^[a-zA-Z]$/); }); it.each([ @@ -161,6 +161,7 @@ describe('random', () => { const actual = faker.random.alpha({ count: 5, bannedChars: ['a', 'p'], + casing: 'lower', }); expect(actual).toHaveLength(5); @@ -171,6 +172,7 @@ describe('random', () => { const actual = faker.random.alpha({ count: 5, bannedChars: 'ap', + casing: 'lower', }); expect(actual).toHaveLength(5); @@ -181,6 +183,7 @@ describe('random', () => { const alphaText = faker.random.alpha({ count: 5, bannedChars: ['a', 'a', 'p'], + casing: 'lower', }); expect(alphaText).toHaveLength(5); @@ -193,6 +196,7 @@ describe('random', () => { faker.random.alpha({ count: 5, bannedChars, + casing: 'lower', }) ).toThrowError( new FakerError( From ce9889e934d00696f7b22e18398a06156c471935 Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Thu, 14 Jul 2022 23:12:30 +0200 Subject: [PATCH 14/48] feat(StringModule): alphaNumeric --- src/modules/string/index.ts | 69 +++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/src/modules/string/index.ts b/src/modules/string/index.ts index 393b80b9df9..90aedc50ac4 100644 --- a/src/modules/string/index.ts +++ b/src/modules/string/index.ts @@ -6,6 +6,7 @@ export type Casing = 'upper' | 'lower' | 'mixed'; const UPPER_CHARS: readonly string[] = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(''); const LOWER_CHARS: readonly string[] = 'abcdefghijklmnopqrstuvwxyz'.split(''); +const DIGIT_CHARS: readonly string[] = '0123456789'.split(''); export type LowerAlphaChar = | 'a' @@ -190,6 +191,74 @@ export class StringModule { ).join(''); } + /** + * Generating a string consisting of alpha characters and digits. + * + * @param options Either the number of characters or an options instance. Defaults to `{ count: 1, casing: 'mixed', bannedChars: [] }`. + * @param options.count The number of characters and digits to generate. Defaults to `1`. + * @param options.casing The casing of the characters. Defaults to `'mixed'`. + * @param options.bannedChars An array of characters and digits which should be banned in the generated string. Defaults to `[]`. + * + * @example + * faker.string.alphaNumeric() // '2' + * faker.string.alphaNumeric(5) // '3e5v7' + * faker.string.alphaNumeric(5, { bannedChars: ["a"] }) // 'xszlm' + */ + alphaNumeric( + options: + | number + | { + count?: number; + casing?: Casing; + bannedChars?: readonly LiteralUnion[] | string; + } = {} + ): string { + if (typeof options === 'number') { + options = { + count: options, + }; + } + + const { casing = 'mixed', count = 1 } = options; + + if (count <= 0) { + return ''; + } + + let { bannedChars = [] } = options; + + if (typeof bannedChars === 'string') { + bannedChars = bannedChars.split(''); + } + + let charsArray = [...DIGIT_CHARS]; + + switch (casing) { + case 'upper': + charsArray.push(...UPPER_CHARS); + break; + case 'lower': + charsArray.push(...LOWER_CHARS); + break; + case 'mixed': + default: + charsArray.push(...LOWER_CHARS, ...UPPER_CHARS); + break; + } + + charsArray = charsArray.filter((elem) => !bannedChars.includes(elem)); + + if (charsArray.length === 0) { + throw new FakerError( + 'Unable to generate string, because all possible characters are banned.' + ); + } + + return Array.from({ length: count }, () => + this.faker.helpers.arrayElement(charsArray) + ).join(''); + } + /** * Returns a UUID v4 ([Universally Unique Identifier](https://en.wikipedia.org/wiki/Universally_unique_identifier)). * From b470b1190045f12292688994acbfef8d59ba3b36 Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Thu, 14 Jul 2022 23:23:45 +0200 Subject: [PATCH 15/48] test(StringModule): alphaNumeric --- test/__snapshots__/string.spec.ts.snap | 12 +++ test/string.spec.ts | 137 +++++++++++++++++++++++++ 2 files changed, 149 insertions(+) diff --git a/test/__snapshots__/string.spec.ts.snap b/test/__snapshots__/string.spec.ts.snap index c3de3c7575e..d1babb7b34a 100644 --- a/test/__snapshots__/string.spec.ts.snap +++ b/test/__snapshots__/string.spec.ts.snap @@ -8,6 +8,8 @@ exports[`string > random() > should return a deterministic string of given lengt exports[`string > seed: 42 > alpha() 1`] = `"t"`; +exports[`string > seed: 42 > alphaNumeric() 1`] = `"n"`; + exports[`string > seed: 42 > hexadecimal() > should return a deterministic hex of given length 1`] = `"8BE4ABdd39321aD7d3fe01FfCE404F4d6db0906bd8"`; exports[`string > seed: 42 > hexadecimal() 1`] = `"8"`; @@ -16,8 +18,12 @@ exports[`string > seed: 42 > random() 1`] = `"Cky2eiXX/J"`; exports[`string > seed: 42 > uuid() 1`] = `"5cf2bc99-2721-407d-992b-a00fbdf302f2"`; +exports[`string > seed: 42 > uuid() 2`] = `"5cf2bc99-2721-407d-992b-a00fbdf302f2"`; + exports[`string > seed: 1211 > alpha() 1`] = `"W"`; +exports[`string > seed: 1211 > alphaNumeric() 1`] = `"V"`; + exports[`string > seed: 1211 > hexadecimal() > should return a deterministic hex of given length 1`] = `"EaDB42F0e3f4A973fAB0AeefCE96DFCF49cD438dF9"`; exports[`string > seed: 1211 > hexadecimal() 1`] = `"E"`; @@ -26,8 +32,12 @@ exports[`string > seed: 1211 > random() 1`] = `"wKti5-}$_/"`; exports[`string > seed: 1211 > uuid() 1`] = `"e7ec32f0-a2a3-4c65-abbd-0caabde64dfd"`; +exports[`string > seed: 1211 > uuid() 2`] = `"e7ec32f0-a2a3-4c65-abbd-0caabde64dfd"`; + exports[`string > seed: 1337 > alpha() 1`] = `"n"`; +exports[`string > seed: 1337 > alphaNumeric() 1`] = `"g"`; + exports[`string > seed: 1337 > hexadecimal() > should return a deterministic hex of given length 1`] = `"5c346ba075bd57F5A62B82d72AF39CBBB07a98cbA8"`; exports[`string > seed: 1337 > hexadecimal() 1`] = `"5"`; @@ -35,3 +45,5 @@ exports[`string > seed: 1337 > hexadecimal() 1`] = `"5"`; exports[`string > seed: 1337 > random() 1`] = `"9U/4:SK$>6"`; exports[`string > seed: 1337 > uuid() 1`] = `"48234870-5389-445f-8b41-c61a52bf27dc"`; + +exports[`string > seed: 1337 > uuid() 2`] = `"48234870-5389-445f-8b41-c61a52bf27dc"`; diff --git a/test/string.spec.ts b/test/string.spec.ts index 72bce415328..9a3317c9172 100644 --- a/test/string.spec.ts +++ b/test/string.spec.ts @@ -10,6 +10,7 @@ const functionNames: (keyof StringModule)[] = [ 'hexadecimal', 'random', 'alpha', + 'alphaNumeric', ]; describe('string', () => { @@ -206,6 +207,142 @@ describe('string', () => { expect(input.bannedChars).toEqual(['a', '%']); }); }); + + describe('alphaNumeric', () => { + it('should generate single character when no additional argument was provided', () => { + const actual = faker.string.alphaNumeric(); + + expect(actual).toHaveLength(1); + }); + + it.each([ + ['upper', /^[A-Z0-9]{250}$/], + ['lower', /^[a-z0-9]{250}$/], + ['mixed', /^[a-zA-Z0-9]{250}$/], + ] as const)('should return %s-case', (casing, pattern) => { + const actual = faker.string.alphaNumeric({ count: 250, casing }); + expect(actual).toMatch(pattern); + }); + + it('should generate many random characters', () => { + const actual = faker.string.alphaNumeric(5); + + expect(actual).toHaveLength(5); + }); + + it.each([0, -1, -100])( + 'should return empty string when length is <= 0', + (length) => { + const actual = faker.string.alphaNumeric(length); + + expect(actual).toBe(''); + } + ); + + it('should be able to ban all alphabetic characters', () => { + const bannedChars = 'abcdefghijklmnopqrstuvwxyz'.split(''); + const alphaText = faker.string.alphaNumeric({ + count: 5, + bannedChars, + casing: 'lower', + }); + + expect(alphaText).toHaveLength(5); + for (const bannedChar of bannedChars) { + expect(alphaText).not.includes(bannedChar); + } + }); + + it('should be able to ban all alphabetic characters via string', () => { + const bannedChars = 'abcdefghijklmnopqrstuvwxyz'; + const alphaText = faker.string.alphaNumeric({ + count: 5, + bannedChars, + casing: 'lower', + }); + + expect(alphaText).toHaveLength(5); + for (const bannedChar of bannedChars) { + expect(alphaText).not.includes(bannedChar); + } + }); + + it('should be able to ban all numeric characters', () => { + const bannedChars = '0123456789'.split(''); + const alphaText = faker.string.alphaNumeric({ + count: 5, + bannedChars, + }); + + expect(alphaText).toHaveLength(5); + for (const bannedChar of bannedChars) { + expect(alphaText).not.includes(bannedChar); + } + }); + + it('should be able to ban all numeric characters via string', () => { + const bannedChars = '0123456789'; + const alphaText = faker.string.alphaNumeric({ + count: 5, + bannedChars, + }); + + expect(alphaText).toHaveLength(5); + for (const bannedChar of bannedChars) { + expect(alphaText).not.includes(bannedChar); + } + }); + + it('should be able to handle mistake in banned characters array', () => { + const alphaText = faker.string.alphaNumeric({ + count: 5, + bannedChars: ['a', 'p', 'a'], + casing: 'lower', + }); + + expect(alphaText).toHaveLength(5); + expect(alphaText).toMatch(/^[0-9b-oq-z]{5}$/); + }); + + it('should throw if all possible characters being banned', () => { + const bannedChars = 'abcdefghijklmnopqrstuvwxyz0123456789'.split(''); + expect(() => + faker.string.alphaNumeric({ + count: 5, + bannedChars, + casing: 'lower', + }) + ).toThrowError( + new FakerError( + 'Unable to generate string, because all possible characters are banned.' + ) + ); + }); + + it('should throw if all possible characters being banned via string', () => { + const bannedChars = 'abcdefghijklmnopqrstuvwxyz0123456789'; + expect(() => + faker.string.alphaNumeric({ + count: 5, + bannedChars, + casing: 'lower', + }) + ).toThrowError(); + }); + + it('should not mutate the input object', () => { + const input: { + bannedChars: string[]; + count: number; + } = Object.freeze({ + bannedChars: ['a', '0', '%'], + count: 5, + }); + + expect(() => faker.string.alphaNumeric(input)).not.toThrow(); + expect(input.bannedChars).toEqual(['a', '0', '%']); + }); + }); } }); }); From d810794b75e687b383df9cda2fc2ce8c86543364 Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Fri, 15 Jul 2022 20:44:55 +0200 Subject: [PATCH 16/48] refactor(Random): deprecate alphaNumeric --- src/modules/random/index.ts | 71 ++++++-------------------- src/modules/vehicle/index.ts | 6 ++- test/__snapshots__/random.spec.ts.snap | 12 ++--- test/__snapshots__/string.spec.ts.snap | 6 --- test/random.spec.ts | 7 +++ 5 files changed, 32 insertions(+), 70 deletions(-) diff --git a/src/modules/random/index.ts b/src/modules/random/index.ts index 3f0e5ce9996..6caf9f37902 100644 --- a/src/modules/random/index.ts +++ b/src/modules/random/index.ts @@ -5,8 +5,6 @@ import type { LiteralUnion } from '../../utils/types'; export type Casing = 'upper' | 'lower' | 'mixed'; -const UPPER_CHARS: readonly string[] = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(''); -const LOWER_CHARS: readonly string[] = 'abcdefghijklmnopqrstuvwxyz'.split(''); const DIGIT_CHARS: readonly string[] = '0123456789'.split(''); export type LowerAlphaChar = @@ -80,20 +78,6 @@ export type NumericChar = export type AlphaChar = LowerAlphaChar | UpperAlphaChar; export type AlphaNumericChar = AlphaChar | NumericChar; -/** - * Method to reduce array of characters. - * - * @param arr existing array of characters - * @param values array of characters which should be removed - * @returns new array without banned characters - */ -function arrayRemove(arr: T[], values: readonly T[]): T[] { - values.forEach((value) => { - arr = arr.filter((ele) => ele !== value); - }); - return arr; -} - /** * Generates random values of different kinds. */ @@ -305,12 +289,16 @@ export class RandomModule { * @param options.casing The casing of the characters. Defaults to `'lower'`. * @param options.bannedChars An array of characters and digits which should be banned in the generated string. Defaults to `[]`. * + * @see faker.string.alphaNumeric() + * * @example * faker.random.alphaNumeric() // '2' * faker.random.alphaNumeric(5) // '3e5v7' * faker.random.alphaNumeric(5, { bannedChars: ["a"] }) // 'xszlm' * * @since 3.1.0 + * + * @deprecated Use faker.string.alphaNumeric() instead. */ alphaNumeric( count: number = 1, @@ -319,46 +307,17 @@ export class RandomModule { bannedChars?: readonly LiteralUnion[] | string; } = {} ): string { - if (count <= 0) { - return ''; - } - - const { - // Switch to 'mixed' with v8.0 - casing = 'lower', - } = options; - let { bannedChars = [] } = options; - - if (typeof bannedChars === 'string') { - bannedChars = bannedChars.split(''); - } - - let charsArray = [...DIGIT_CHARS]; - - switch (casing) { - case 'upper': - charsArray.push(...UPPER_CHARS); - break; - case 'lower': - charsArray.push(...LOWER_CHARS); - break; - case 'mixed': - default: - charsArray.push(...LOWER_CHARS, ...UPPER_CHARS); - break; - } - - charsArray = arrayRemove(charsArray, bannedChars); - - if (charsArray.length === 0) { - throw new FakerError( - 'Unable to generate string, because all possible characters are banned.' - ); - } - - return Array.from({ length: count }, () => - this.faker.helpers.arrayElement(charsArray) - ).join(''); + deprecated({ + deprecated: 'faker.random.alphaNumeric()', + proposed: 'faker.string.alphaNumeric()', + since: '8.0', + until: '9.0', + }); + return this.faker.string.alphaNumeric({ + bannedChars: options.bannedChars, + casing: options.casing, + count, + }); } /** diff --git a/src/modules/vehicle/index.ts b/src/modules/vehicle/index.ts index 3db92a9c929..c6a0545d7ec 100644 --- a/src/modules/vehicle/index.ts +++ b/src/modules/vehicle/index.ts @@ -88,14 +88,16 @@ export class VehicleModule { */ vin(): string { const bannedChars = ['o', 'i', 'q', 'O', 'I', 'Q']; - return `${this.faker.random.alphaNumeric(10, { + return `${this.faker.string.alphaNumeric({ + count: 10, casing: 'upper', bannedChars, })}${this.faker.string.alpha({ count: 1, casing: 'upper', bannedChars, - })}${this.faker.random.alphaNumeric(1, { + })}${this.faker.string.alphaNumeric({ + count: 1, casing: 'upper', bannedChars, })}${this.faker.datatype.number({ min: 10000, max: 99999 })}` // return five digit # diff --git a/test/__snapshots__/random.spec.ts.snap b/test/__snapshots__/random.spec.ts.snap index b50ab66b7ce..921c56a8597 100644 --- a/test/__snapshots__/random.spec.ts.snap +++ b/test/__snapshots__/random.spec.ts.snap @@ -4,9 +4,9 @@ exports[`random > 42 > alpha > noArgs 1`] = `"t"`; exports[`random > 42 > alpha > withLength 1`] = `"tPXjM"`; -exports[`random > 42 > alphaNumeric > noArgs 1`] = `"d"`; +exports[`random > 42 > alphaNumeric > noArgs 1`] = `"n"`; -exports[`random > 42 > alphaNumeric > withLength 1`] = `"dsy6q"`; +exports[`random > 42 > alphaNumeric > withLength 1`] = `"nNWbJ"`; exports[`random > 42 > locale 1`] = `"es_MX"`; @@ -24,9 +24,9 @@ exports[`random > 1211 > alpha > noArgs 1`] = `"W"`; exports[`random > 1211 > alpha > withLength 1`] = `"WxUOl"`; -exports[`random > 1211 > alphaNumeric > noArgs 1`] = `"x"`; +exports[`random > 1211 > alphaNumeric > noArgs 1`] = `"V"`; -exports[`random > 1211 > alphaNumeric > withLength 1`] = `"xgws8"`; +exports[`random > 1211 > alphaNumeric > withLength 1`] = `"VsTMd"`; exports[`random > 1211 > locale 1`] = `"ur"`; @@ -44,9 +44,9 @@ exports[`random > 1337 > alpha > noArgs 1`] = `"n"`; exports[`random > 1337 > alpha > withLength 1`] = `"nDilo"`; -exports[`random > 1337 > alphaNumeric > noArgs 1`] = `"9"`; +exports[`random > 1337 > alphaNumeric > noArgs 1`] = `"g"`; -exports[`random > 1337 > alphaNumeric > withLength 1`] = `"9k57a"`; +exports[`random > 1337 > alphaNumeric > withLength 1`] = `"gy9dh"`; exports[`random > 1337 > locale 1`] = `"en_GH"`; diff --git a/test/__snapshots__/string.spec.ts.snap b/test/__snapshots__/string.spec.ts.snap index d1babb7b34a..fc69f338461 100644 --- a/test/__snapshots__/string.spec.ts.snap +++ b/test/__snapshots__/string.spec.ts.snap @@ -18,8 +18,6 @@ exports[`string > seed: 42 > random() 1`] = `"Cky2eiXX/J"`; exports[`string > seed: 42 > uuid() 1`] = `"5cf2bc99-2721-407d-992b-a00fbdf302f2"`; -exports[`string > seed: 42 > uuid() 2`] = `"5cf2bc99-2721-407d-992b-a00fbdf302f2"`; - exports[`string > seed: 1211 > alpha() 1`] = `"W"`; exports[`string > seed: 1211 > alphaNumeric() 1`] = `"V"`; @@ -32,8 +30,6 @@ exports[`string > seed: 1211 > random() 1`] = `"wKti5-}$_/"`; exports[`string > seed: 1211 > uuid() 1`] = `"e7ec32f0-a2a3-4c65-abbd-0caabde64dfd"`; -exports[`string > seed: 1211 > uuid() 2`] = `"e7ec32f0-a2a3-4c65-abbd-0caabde64dfd"`; - exports[`string > seed: 1337 > alpha() 1`] = `"n"`; exports[`string > seed: 1337 > alphaNumeric() 1`] = `"g"`; @@ -45,5 +41,3 @@ exports[`string > seed: 1337 > hexadecimal() 1`] = `"5"`; exports[`string > seed: 1337 > random() 1`] = `"9U/4:SK$>6"`; exports[`string > seed: 1337 > uuid() 1`] = `"48234870-5389-445f-8b41-c61a52bf27dc"`; - -exports[`string > seed: 1337 > uuid() 2`] = `"48234870-5389-445f-8b41-c61a52bf27dc"`; diff --git a/test/random.spec.ts b/test/random.spec.ts index ea634dfd32c..20ccde6924c 100644 --- a/test/random.spec.ts +++ b/test/random.spec.ts @@ -256,6 +256,7 @@ describe('random', () => { const bannedChars = 'abcdefghijklmnopqrstuvwxyz'.split(''); const alphaText = faker.random.alphaNumeric(5, { bannedChars, + casing: 'lower', }); expect(alphaText).toHaveLength(5); @@ -268,6 +269,7 @@ describe('random', () => { const bannedChars = 'abcdefghijklmnopqrstuvwxyz'; const alphaText = faker.random.alphaNumeric(5, { bannedChars, + casing: 'lower', }); expect(alphaText).toHaveLength(5); @@ -280,6 +282,7 @@ describe('random', () => { const bannedChars = '0123456789'.split(''); const alphaText = faker.random.alphaNumeric(5, { bannedChars, + casing: 'lower', }); expect(alphaText).toHaveLength(5); @@ -292,6 +295,7 @@ describe('random', () => { const bannedChars = '0123456789'; const alphaText = faker.random.alphaNumeric(5, { bannedChars, + casing: 'lower', }); expect(alphaText).toHaveLength(5); @@ -303,6 +307,7 @@ describe('random', () => { it('should be able to handle mistake in banned characters array', () => { const alphaText = faker.random.alphaNumeric(5, { bannedChars: ['a', 'p', 'a'], + casing: 'lower', }); expect(alphaText).toHaveLength(5); @@ -314,6 +319,7 @@ describe('random', () => { expect(() => faker.random.alphaNumeric(5, { bannedChars, + casing: 'lower', }) ).toThrowError( new FakerError( @@ -327,6 +333,7 @@ describe('random', () => { expect(() => faker.random.alphaNumeric(5, { bannedChars, + casing: 'lower', }) ).toThrowError(); }); From cd8cec2d541a6bcb0a192a276960968945bafc5e Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Fri, 15 Jul 2022 22:20:13 +0200 Subject: [PATCH 17/48] feat(StringModule): numeric --- src/modules/string/index.ts | 71 +++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/src/modules/string/index.ts b/src/modules/string/index.ts index 90aedc50ac4..aa0781d9a35 100644 --- a/src/modules/string/index.ts +++ b/src/modules/string/index.ts @@ -259,6 +259,77 @@ export class StringModule { ).join(''); } + /** + * Generates a given length string of digits. + * + * @param options Either the number of characters or the options to use. Defaults to `{ length: 1, allowLeadingZeros = false, bannedDigits = [] }`. + * @param options.length The number of digits to generate. Defaults to `1`. + * @param options.allowLeadingZeros If true, leading zeros will be allowed. Defaults to `false`. + * @param options.bannedDigits An array of digits which should be banned in the generated string. Defaults to `[]`. + * + * @example + * faker.string.numeric() // '2' + * faker.string.numeric(5) // '31507' + * faker.string.numeric(42) // '56434563150765416546479875435481513188548' + * faker.string.numeric({ allowLeadingZeros: true, length: 42 }) // '00564846278453876543517840713421451546115' + * faker.string.numeric({ bannedDigits: ['0'], length: 6 }) // '943228' + */ + numeric( + options: + | number + | { + length?: number; + allowLeadingZeros?: boolean; + bannedDigits?: readonly LiteralUnion[] | string; + } = {} + ): string { + if (typeof options === 'number') { + options = { + length: options, + }; + } + + const { allowLeadingZeros = false, length = 1 } = options; + if (length <= 0) { + return ''; + } + + let { bannedDigits = [] } = options; + + if (typeof bannedDigits === 'string') { + bannedDigits = bannedDigits.split(''); + } + + const allowedDigits = DIGIT_CHARS.filter( + (digit) => !bannedDigits.includes(digit) + ); + + if ( + allowedDigits.length === 0 || + (allowedDigits.length === 1 && + !allowLeadingZeros && + allowedDigits[0] === '0') + ) { + throw new FakerError( + 'Unable to generate numeric string, because all possible digits are banned.' + ); + } + + let result = ''; + + if (!allowLeadingZeros && !bannedDigits.includes('0')) { + result += this.faker.helpers.arrayElement( + allowedDigits.filter((digit) => digit !== '0') + ); + } + + while (result.length < length) { + result += this.faker.helpers.arrayElement(allowedDigits); + } + + return result; + } + /** * Returns a UUID v4 ([Universally Unique Identifier](https://en.wikipedia.org/wiki/Universally_unique_identifier)). * From 4560f65bebbf44ee117a4c8b2e7ae60fdaee9551 Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Fri, 15 Jul 2022 23:10:30 +0200 Subject: [PATCH 18/48] test(StringModule): numeric --- test/__snapshots__/string.spec.ts.snap | 6 ++ test/string.spec.ts | 118 +++++++++++++++++++++++++ 2 files changed, 124 insertions(+) diff --git a/test/__snapshots__/string.spec.ts.snap b/test/__snapshots__/string.spec.ts.snap index fc69f338461..89b0c02994f 100644 --- a/test/__snapshots__/string.spec.ts.snap +++ b/test/__snapshots__/string.spec.ts.snap @@ -14,6 +14,8 @@ exports[`string > seed: 42 > hexadecimal() > should return a deterministic hex o exports[`string > seed: 42 > hexadecimal() 1`] = `"8"`; +exports[`string > seed: 42 > numeric() 1`] = `"4"`; + exports[`string > seed: 42 > random() 1`] = `"Cky2eiXX/J"`; exports[`string > seed: 42 > uuid() 1`] = `"5cf2bc99-2721-407d-992b-a00fbdf302f2"`; @@ -26,6 +28,8 @@ exports[`string > seed: 1211 > hexadecimal() > should return a deterministic hex exports[`string > seed: 1211 > hexadecimal() 1`] = `"E"`; +exports[`string > seed: 1211 > numeric() 1`] = `"9"`; + exports[`string > seed: 1211 > random() 1`] = `"wKti5-}$_/"`; exports[`string > seed: 1211 > uuid() 1`] = `"e7ec32f0-a2a3-4c65-abbd-0caabde64dfd"`; @@ -38,6 +42,8 @@ exports[`string > seed: 1337 > hexadecimal() > should return a deterministic hex exports[`string > seed: 1337 > hexadecimal() 1`] = `"5"`; +exports[`string > seed: 1337 > numeric() 1`] = `"3"`; + exports[`string > seed: 1337 > random() 1`] = `"9U/4:SK$>6"`; exports[`string > seed: 1337 > uuid() 1`] = `"48234870-5389-445f-8b41-c61a52bf27dc"`; diff --git a/test/string.spec.ts b/test/string.spec.ts index 9a3317c9172..4c61e1ca9e6 100644 --- a/test/string.spec.ts +++ b/test/string.spec.ts @@ -2,6 +2,7 @@ import { afterEach, describe, expect, it } from 'vitest'; import { faker, FakerError } from '../src'; import type { StringModule } from '../src/modules/string'; import { seededRuns } from './support/seededRuns'; +import { times } from './support/times'; const NON_SEEDED_BASED_RUN = 5; @@ -11,6 +12,7 @@ const functionNames: (keyof StringModule)[] = [ 'random', 'alpha', 'alphaNumeric', + 'numeric', ]; describe('string', () => { @@ -343,6 +345,122 @@ describe('string', () => { expect(input.bannedChars).toEqual(['a', '0', '%']); }); }); + + describe('numeric', () => { + it('should return single digit when no length provided', () => { + const actual = faker.string.numeric(); + + expect(actual).toHaveLength(1); + expect(actual).toMatch(/^[1-9]$/); + }); + + it.each(times(100))( + 'should generate random value with a length of %s', + (length) => { + const actual = faker.string.numeric(length); + + expect(actual).toHaveLength(length); + expect(actual).toMatch(/^[1-9][0-9]*$/); + } + ); + + it('should return empty string with a length of 0', () => { + const actual = faker.string.numeric(0); + + expect(actual).toHaveLength(0); + }); + + it('should return empty string with a negative length', () => { + const actual = faker.string.numeric(-10); + + expect(actual).toHaveLength(0); + }); + + it('should return a valid numeric string with provided length', () => { + const actual = faker.string.numeric(1000); + + expect(actual).toBeTypeOf('string'); + expect(actual).toHaveLength(1000); + expect(actual).toMatch(/^[1-9][0-9]+$/); + }); + + it('should allow leading zeros via option', () => { + const actual = faker.string.numeric({ + length: 15, + allowLeadingZeros: true, + }); + + expect(actual).toMatch(/^[0-9]+$/); + }); + + it('should allow leading zeros via option and all other digits banned', () => { + const actual = faker.string.numeric({ + length: 4, + allowLeadingZeros: true, + bannedDigits: '123456789'.split(''), + }); + + expect(actual).toBe('0000'); + }); + + it('should allow leading zeros via option and all other digits banned via string', () => { + const actual = faker.string.numeric({ + length: 4, + allowLeadingZeros: true, + bannedDigits: '123456789', + }); + + expect(actual).toBe('0000'); + }); + + it('should fail on leading zeros via option and all other digits banned', () => { + expect(() => + faker.string.numeric({ + length: 4, + allowLeadingZeros: false, + bannedDigits: '123456789'.split(''), + }) + ).toThrowError( + new FakerError( + 'Unable to generate numeric string, because all possible digits are banned.' + ) + ); + }); + + it('should fail on leading zeros via option and all other digits banned via string', () => { + expect(() => + faker.string.numeric({ + length: 4, + allowLeadingZeros: false, + bannedDigits: '123456789', + }) + ).toThrowError( + new FakerError( + 'Unable to generate numeric string, because all possible digits are banned.' + ) + ); + }); + + it('should ban all digits passed via bannedDigits', () => { + const actual = faker.string.numeric({ + length: 1000, + bannedDigits: 'c84U1'.split(''), + }); + + expect(actual).toHaveLength(1000); + expect(actual).toMatch(/^[0235679]{1000}$/); + }); + + it('should ban all digits passed via bannedDigits via string', () => { + const actual = faker.string.numeric({ + length: 1000, + bannedDigits: 'c84U1', + }); + + expect(actual).toHaveLength(1000); + expect(actual).toMatch(/^[0235679]{1000}$/); + }); + }); } }); }); From ea720021f3a4a62e4dc2a88ff94831baa4a275ad Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Fri, 15 Jul 2022 23:18:48 +0200 Subject: [PATCH 19/48] refactor(Random): deprecate numeric --- src/modules/datatype/index.ts | 3 +- src/modules/random/index.ts | 57 +++++++++-------------------------- 2 files changed, 17 insertions(+), 43 deletions(-) diff --git a/src/modules/datatype/index.ts b/src/modules/datatype/index.ts index 5f0229e1df3..6d741659722 100644 --- a/src/modules/datatype/index.ts +++ b/src/modules/datatype/index.ts @@ -339,7 +339,8 @@ export class DatatypeModule { const offset = BigInt( - this.faker.random.numeric(delta.toString(10).length, { + this.faker.string.numeric({ + length: delta.toString(10).length, allowLeadingZeros: true, }) ) % diff --git a/src/modules/random/index.ts b/src/modules/random/index.ts index 6caf9f37902..ef149d34732 100644 --- a/src/modules/random/index.ts +++ b/src/modules/random/index.ts @@ -1,12 +1,9 @@ import type { Faker } from '../..'; -import { FakerError } from '../../errors/faker-error'; import { deprecated } from '../../internal/deprecated'; import type { LiteralUnion } from '../../utils/types'; export type Casing = 'upper' | 'lower' | 'mixed'; -const DIGIT_CHARS: readonly string[] = '0123456789'.split(''); - export type LowerAlphaChar = | 'a' | 'b' @@ -328,6 +325,8 @@ export class RandomModule { * @param options.allowLeadingZeros If true, leading zeros will be allowed. Defaults to `false`. * @param options.bannedDigits An array of digits which should be banned in the generated string. Defaults to `[]`. * + * @see faker.string.numeric() + * * @example * faker.random.numeric() // '2' * faker.random.numeric(5) // '31507' @@ -336,6 +335,8 @@ export class RandomModule { * faker.random.numeric(6, { bannedDigits: ['0'] }) // '943228' * * @since 6.3.0 + * + * @deprecated Use faker.string.numeric() instead. */ numeric( length: number = 1, @@ -344,44 +345,16 @@ export class RandomModule { bannedDigits?: readonly LiteralUnion[] | string; } = {} ): string { - if (length <= 0) { - return ''; - } - - const { allowLeadingZeros = false } = options; - let { bannedDigits = [] } = options; - - if (typeof bannedDigits === 'string') { - bannedDigits = bannedDigits.split(''); - } - - const allowedDigits = DIGIT_CHARS.filter( - (digit) => !bannedDigits.includes(digit) - ); - - if ( - allowedDigits.length === 0 || - (allowedDigits.length === 1 && - !allowLeadingZeros && - allowedDigits[0] === '0') - ) { - throw new FakerError( - 'Unable to generate numeric string, because all possible digits are banned.' - ); - } - - let result = ''; - - if (!allowLeadingZeros && !bannedDigits.includes('0')) { - result += this.faker.helpers.arrayElement( - allowedDigits.filter((digit) => digit !== '0') - ); - } - - while (result.length < length) { - result += this.faker.helpers.arrayElement(allowedDigits); - } - - return result; + deprecated({ + deprecated: 'faker.random.numeric()', + proposed: 'faker.string.numeric()', + since: '8.0', + until: '9.0', + }); + return this.faker.string.numeric({ + allowLeadingZeros: options.allowLeadingZeros, + bannedDigits: options.bannedDigits, + length, + }); } } From 36361dabc395cb1e2ea0762aab8ac0941e28dfed Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Fri, 15 Jul 2022 23:22:51 +0200 Subject: [PATCH 20/48] chore(Random): import char types from StringModule --- src/modules/random/index.ts | 79 +++---------------------------------- 1 file changed, 6 insertions(+), 73 deletions(-) diff --git a/src/modules/random/index.ts b/src/modules/random/index.ts index ef149d34732..6618c1efa80 100644 --- a/src/modules/random/index.ts +++ b/src/modules/random/index.ts @@ -1,79 +1,12 @@ import type { Faker } from '../..'; import { deprecated } from '../../internal/deprecated'; import type { LiteralUnion } from '../../utils/types'; - -export type Casing = 'upper' | 'lower' | 'mixed'; - -export type LowerAlphaChar = - | 'a' - | 'b' - | 'c' - | 'd' - | 'e' - | 'f' - | 'g' - | 'h' - | 'i' - | 'j' - | 'k' - | 'l' - | 'm' - | 'n' - | 'o' - | 'p' - | 'q' - | 'r' - | 's' - | 't' - | 'u' - | 'v' - | 'w' - | 'x' - | 'y' - | 'z'; - -export type UpperAlphaChar = - | 'A' - | 'B' - | 'C' - | 'D' - | 'E' - | 'F' - | 'G' - | 'H' - | 'I' - | 'J' - | 'K' - | 'L' - | 'M' - | 'N' - | 'O' - | 'P' - | 'Q' - | 'R' - | 'S' - | 'T' - | 'U' - | 'V' - | 'W' - | 'X' - | 'Y' - | 'Z'; - -export type NumericChar = - | '0' - | '1' - | '2' - | '3' - | '4' - | '5' - | '6' - | '7' - | '8' - | '9'; - -export type AlphaChar = LowerAlphaChar | UpperAlphaChar; -export type AlphaNumericChar = AlphaChar | NumericChar; +import type { + AlphaChar, + AlphaNumericChar, + Casing, + NumericChar, +} from '../string'; /** * Generates random values of different kinds. From 483891f684efda33c96f862e2f5d2ecd08855681 Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Fri, 22 Jul 2022 00:13:12 +0200 Subject: [PATCH 21/48] refactor(string): rename alphaNumeric to alphanumeric --- src/modules/random/index.ts | 4 ++-- src/modules/string/index.ts | 8 ++++---- src/modules/vehicle/index.ts | 4 ++-- test/__snapshots__/string.spec.ts.snap | 6 +++--- test/string.spec.ts | 26 +++++++++++++------------- 5 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/modules/random/index.ts b/src/modules/random/index.ts index 6618c1efa80..891b0f56a4a 100644 --- a/src/modules/random/index.ts +++ b/src/modules/random/index.ts @@ -239,11 +239,11 @@ export class RandomModule { ): string { deprecated({ deprecated: 'faker.random.alphaNumeric()', - proposed: 'faker.string.alphaNumeric()', + proposed: 'faker.string.alphanumeric()', since: '8.0', until: '9.0', }); - return this.faker.string.alphaNumeric({ + return this.faker.string.alphanumeric({ bannedChars: options.bannedChars, casing: options.casing, count, diff --git a/src/modules/string/index.ts b/src/modules/string/index.ts index aa0781d9a35..ea2d640e60d 100644 --- a/src/modules/string/index.ts +++ b/src/modules/string/index.ts @@ -200,11 +200,11 @@ export class StringModule { * @param options.bannedChars An array of characters and digits which should be banned in the generated string. Defaults to `[]`. * * @example - * faker.string.alphaNumeric() // '2' - * faker.string.alphaNumeric(5) // '3e5v7' - * faker.string.alphaNumeric(5, { bannedChars: ["a"] }) // 'xszlm' + * faker.string.alphanumeric() // '2' + * faker.string.alphanumeric(5) // '3e5v7' + * faker.string.alphanumeric(5, { bannedChars: ["a"] }) // 'xszlm' */ - alphaNumeric( + alphanumeric( options: | number | { diff --git a/src/modules/vehicle/index.ts b/src/modules/vehicle/index.ts index c6a0545d7ec..d6421cfc943 100644 --- a/src/modules/vehicle/index.ts +++ b/src/modules/vehicle/index.ts @@ -88,7 +88,7 @@ export class VehicleModule { */ vin(): string { const bannedChars = ['o', 'i', 'q', 'O', 'I', 'Q']; - return `${this.faker.string.alphaNumeric({ + return `${this.faker.string.alphanumeric({ count: 10, casing: 'upper', bannedChars, @@ -96,7 +96,7 @@ export class VehicleModule { count: 1, casing: 'upper', bannedChars, - })}${this.faker.string.alphaNumeric({ + })}${this.faker.string.alphanumeric({ count: 1, casing: 'upper', bannedChars, diff --git a/test/__snapshots__/string.spec.ts.snap b/test/__snapshots__/string.spec.ts.snap index 89b0c02994f..8e0e09e3a58 100644 --- a/test/__snapshots__/string.spec.ts.snap +++ b/test/__snapshots__/string.spec.ts.snap @@ -8,7 +8,7 @@ exports[`string > random() > should return a deterministic string of given lengt exports[`string > seed: 42 > alpha() 1`] = `"t"`; -exports[`string > seed: 42 > alphaNumeric() 1`] = `"n"`; +exports[`string > seed: 42 > alphanumeric() 1`] = `"n"`; exports[`string > seed: 42 > hexadecimal() > should return a deterministic hex of given length 1`] = `"8BE4ABdd39321aD7d3fe01FfCE404F4d6db0906bd8"`; @@ -22,7 +22,7 @@ exports[`string > seed: 42 > uuid() 1`] = `"5cf2bc99-2721-407d-992b-a00fbdf302f2 exports[`string > seed: 1211 > alpha() 1`] = `"W"`; -exports[`string > seed: 1211 > alphaNumeric() 1`] = `"V"`; +exports[`string > seed: 1211 > alphanumeric() 1`] = `"V"`; exports[`string > seed: 1211 > hexadecimal() > should return a deterministic hex of given length 1`] = `"EaDB42F0e3f4A973fAB0AeefCE96DFCF49cD438dF9"`; @@ -36,7 +36,7 @@ exports[`string > seed: 1211 > uuid() 1`] = `"e7ec32f0-a2a3-4c65-abbd-0caabde64d exports[`string > seed: 1337 > alpha() 1`] = `"n"`; -exports[`string > seed: 1337 > alphaNumeric() 1`] = `"g"`; +exports[`string > seed: 1337 > alphanumeric() 1`] = `"g"`; exports[`string > seed: 1337 > hexadecimal() > should return a deterministic hex of given length 1`] = `"5c346ba075bd57F5A62B82d72AF39CBBB07a98cbA8"`; diff --git a/test/string.spec.ts b/test/string.spec.ts index 4c61e1ca9e6..32f4cb7f831 100644 --- a/test/string.spec.ts +++ b/test/string.spec.ts @@ -11,7 +11,7 @@ const functionNames: (keyof StringModule)[] = [ 'hexadecimal', 'random', 'alpha', - 'alphaNumeric', + 'alphanumeric', 'numeric', ]; @@ -212,7 +212,7 @@ describe('string', () => { describe('alphaNumeric', () => { it('should generate single character when no additional argument was provided', () => { - const actual = faker.string.alphaNumeric(); + const actual = faker.string.alphanumeric(); expect(actual).toHaveLength(1); }); @@ -222,12 +222,12 @@ describe('string', () => { ['lower', /^[a-z0-9]{250}$/], ['mixed', /^[a-zA-Z0-9]{250}$/], ] as const)('should return %s-case', (casing, pattern) => { - const actual = faker.string.alphaNumeric({ count: 250, casing }); + const actual = faker.string.alphanumeric({ count: 250, casing }); expect(actual).toMatch(pattern); }); it('should generate many random characters', () => { - const actual = faker.string.alphaNumeric(5); + const actual = faker.string.alphanumeric(5); expect(actual).toHaveLength(5); }); @@ -235,7 +235,7 @@ describe('string', () => { it.each([0, -1, -100])( 'should return empty string when length is <= 0', (length) => { - const actual = faker.string.alphaNumeric(length); + const actual = faker.string.alphanumeric(length); expect(actual).toBe(''); } @@ -243,7 +243,7 @@ describe('string', () => { it('should be able to ban all alphabetic characters', () => { const bannedChars = 'abcdefghijklmnopqrstuvwxyz'.split(''); - const alphaText = faker.string.alphaNumeric({ + const alphaText = faker.string.alphanumeric({ count: 5, bannedChars, casing: 'lower', @@ -257,7 +257,7 @@ describe('string', () => { it('should be able to ban all alphabetic characters via string', () => { const bannedChars = 'abcdefghijklmnopqrstuvwxyz'; - const alphaText = faker.string.alphaNumeric({ + const alphaText = faker.string.alphanumeric({ count: 5, bannedChars, casing: 'lower', @@ -271,7 +271,7 @@ describe('string', () => { it('should be able to ban all numeric characters', () => { const bannedChars = '0123456789'.split(''); - const alphaText = faker.string.alphaNumeric({ + const alphaText = faker.string.alphanumeric({ count: 5, bannedChars, }); @@ -284,7 +284,7 @@ describe('string', () => { it('should be able to ban all numeric characters via string', () => { const bannedChars = '0123456789'; - const alphaText = faker.string.alphaNumeric({ + const alphaText = faker.string.alphanumeric({ count: 5, bannedChars, }); @@ -296,7 +296,7 @@ describe('string', () => { }); it('should be able to handle mistake in banned characters array', () => { - const alphaText = faker.string.alphaNumeric({ + const alphaText = faker.string.alphanumeric({ count: 5, bannedChars: ['a', 'p', 'a'], casing: 'lower', @@ -309,7 +309,7 @@ describe('string', () => { it('should throw if all possible characters being banned', () => { const bannedChars = 'abcdefghijklmnopqrstuvwxyz0123456789'.split(''); expect(() => - faker.string.alphaNumeric({ + faker.string.alphanumeric({ count: 5, bannedChars, casing: 'lower', @@ -324,7 +324,7 @@ describe('string', () => { it('should throw if all possible characters being banned via string', () => { const bannedChars = 'abcdefghijklmnopqrstuvwxyz0123456789'; expect(() => - faker.string.alphaNumeric({ + faker.string.alphanumeric({ count: 5, bannedChars, casing: 'lower', @@ -341,7 +341,7 @@ describe('string', () => { count: 5, }); - expect(() => faker.string.alphaNumeric(input)).not.toThrow(); + expect(() => faker.string.alphanumeric(input)).not.toThrow(); expect(input.bannedChars).toEqual(['a', '0', '%']); }); }); From 28e6da96fb7b3faf8d33954a156d784527448dc4 Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Sat, 10 Sep 2022 00:36:25 +0200 Subject: [PATCH 22/48] test(helpers): fix alpha casing expectation --- test/helpers.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/helpers.spec.ts b/test/helpers.spec.ts index 74803a1781a..971ed93fae5 100644 --- a/test/helpers.spec.ts +++ b/test/helpers.spec.ts @@ -599,13 +599,13 @@ describe('helpers', () => { }); it('should be able to handle random }} brackets', () => { - expect(faker.helpers.fake('}}hello{{random.alpha}}')).toMatch( + expect(faker.helpers.fake('}}hello{{string.alpha}}')).toMatch( /^}}hello[a-z]$/ ); }); it('should be able to handle connected brackets', () => { - expect(faker.helpers.fake('{{{random.alpha}}}')).toMatch(/^{[a-z]}$/); + expect(faker.helpers.fake('{{{string.alpha}}}')).toMatch(/^{[a-z]}$/); }); it('should be able to handle empty brackets', () => { From 3338fd67ad231bb8a12a0eb1ce6dd26eab054fe3 Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Sat, 10 Sep 2022 00:39:35 +0200 Subject: [PATCH 23/48] docs(string): JSDocs add `@since` tag --- src/modules/string/index.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/modules/string/index.ts b/src/modules/string/index.ts index ea2d640e60d..99ebde85b14 100644 --- a/src/modules/string/index.ts +++ b/src/modules/string/index.ts @@ -101,6 +101,8 @@ export class StringModule { * @example * faker.string.random() // 'Zo!.:*e>wR' * faker.string.random(5) // '6Bye8' + * + * @since 8.0.0 */ random(length = 10): string { const maxLength = Math.pow(2, 20); @@ -137,6 +139,8 @@ export class StringModule { * faker.string.alpha() // 'b' * faker.string.alpha(10) // 'qccrabobaf' * faker.string.alpha({ count: 5, casing: 'upper', bannedChars: ['A'] }) // 'DTCIC' + * + * @since 8.0.0 */ alpha( options: @@ -203,6 +207,8 @@ export class StringModule { * faker.string.alphanumeric() // '2' * faker.string.alphanumeric(5) // '3e5v7' * faker.string.alphanumeric(5, { bannedChars: ["a"] }) // 'xszlm' + * + * @since 8.0.0 */ alphanumeric( options: @@ -273,6 +279,8 @@ export class StringModule { * faker.string.numeric(42) // '56434563150765416546479875435481513188548' * faker.string.numeric({ allowLeadingZeros: true, length: 42 }) // '00564846278453876543517840713421451546115' * faker.string.numeric({ bannedDigits: ['0'], length: 6 }) // '943228' + * + * @since 8.0.0 */ numeric( options: @@ -335,6 +343,8 @@ export class StringModule { * * @example * faker.string.uuid() // '4136cd0b-d90b-4af7-b485-5d1ded8db252' + * + * @since 8.0.0 */ uuid(): string { const RFC4122_TEMPLATE = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'; @@ -354,6 +364,8 @@ export class StringModule { * @example * faker.string.hexadecimal() // 'b' * faker.string.hexadecimal(10) // 'aE13F044fb' + * + * @since 8.0.0 */ hexadecimal(length = 1): string { let wholeString = ''; From fdb987861c4aa4b04b0867d760d864cb7f9da47c Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Sat, 10 Sep 2022 00:47:51 +0200 Subject: [PATCH 24/48] test(helpers): fix alpha casing expectation for real --- test/helpers.spec.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/helpers.spec.ts b/test/helpers.spec.ts index 971ed93fae5..2ee04701406 100644 --- a/test/helpers.spec.ts +++ b/test/helpers.spec.ts @@ -600,12 +600,14 @@ describe('helpers', () => { it('should be able to handle random }} brackets', () => { expect(faker.helpers.fake('}}hello{{string.alpha}}')).toMatch( - /^}}hello[a-z]$/ + /^}}hello[a-z]$/i ); }); it('should be able to handle connected brackets', () => { - expect(faker.helpers.fake('{{{string.alpha}}}')).toMatch(/^{[a-z]}$/); + expect(faker.helpers.fake('{{{string.alpha}}}')).toMatch( + /^{[a-z]}$/i + ); }); it('should be able to handle empty brackets', () => { From 0fffffb960d9ee54b24442933a871ea91365341b Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Sat, 10 Sep 2022 01:01:11 +0200 Subject: [PATCH 25/48] refactor(finance): use `string.alpha` in `bic` --- src/modules/finance/index.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/modules/finance/index.ts b/src/modules/finance/index.ts index 905c60689b9..c70b9e2b606 100644 --- a/src/modules/finance/index.ts +++ b/src/modules/finance/index.ts @@ -433,15 +433,18 @@ export class FinanceModule { * @since 4.0.0 */ bic(): string { - const bankIdentifier = this.faker.random.alpha({ + const bankIdentifier = this.faker.string.alpha({ count: 4, casing: 'upper', }); const countryCode = this.faker.helpers.arrayElement(iban.iso3166); - const locationCode = this.faker.random.alphaNumeric(2, { casing: 'upper' }); + const locationCode = this.faker.string.alphanumeric({ + count: 2, + casing: 'upper', + }); const branchCode = this.faker.datatype.boolean() ? this.faker.datatype.boolean() - ? this.faker.random.alphaNumeric(3, { casing: 'upper' }) + ? this.faker.string.alphanumeric({ count: 3, casing: 'upper' }) : 'XXX' : ''; From 9d955cece8db0ccb59e2c26b60d057bcecd3edb3 Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Sat, 10 Sep 2022 01:06:19 +0200 Subject: [PATCH 26/48] docs(datatype): human readble deprecation message --- src/modules/datatype/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/datatype/index.ts b/src/modules/datatype/index.ts index 6d741659722..e2a58c02895 100644 --- a/src/modules/datatype/index.ts +++ b/src/modules/datatype/index.ts @@ -217,7 +217,7 @@ export class DatatypeModule { * * @since 6.1.2 * - * @deprecated + * @deprecated Use `faker.string.hexadecimal()` instead. */ hexadecimal( options: From 9935052a10ee2248a86896168ce943dfb2f0604a Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Sat, 10 Sep 2022 01:10:01 +0200 Subject: [PATCH 27/48] test(datatype): fix hexadecimal expectations --- test/datatype.spec.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/datatype.spec.ts b/test/datatype.spec.ts index bbfcb8c8e99..0c0683770a3 100644 --- a/test/datatype.spec.ts +++ b/test/datatype.spec.ts @@ -331,8 +331,8 @@ describe('datatype', () => { describe('hexadecimal', () => { it('generates single hex character when no additional argument was provided', () => { const hex = faker.datatype.hexadecimal(); - expect(hex).toMatch(/^[0-9a-f]{1}$/i); - expect(hex).toHaveLength(1); + expect(hex).toMatch(/^(0x)[0-9a-f]{1}$/i); + expect(hex).toHaveLength(3); }); it('generates a hex string with a provided prefix', () => { @@ -343,8 +343,8 @@ describe('datatype', () => { it('generates a random hex string with a provided length', () => { const hex = faker.datatype.hexadecimal({ length: 5 }); - expect(hex).toMatch(/^[0-9a-f]+$/i); - expect(hex).toHaveLength(5); + expect(hex).toMatch(/^(0x)[0-9a-f]+$/i); + expect(hex).toHaveLength(7); }); it('generates a hex string with a provided casing', () => { From e9829fec854bbf28b64ab740588c867713c202c1 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Mon, 17 Oct 2022 18:26:44 +0200 Subject: [PATCH 28/48] chore: fix missing import --- src/modules/random/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/modules/random/index.ts b/src/modules/random/index.ts index e981f10622c..fbe9a61e2c4 100644 --- a/src/modules/random/index.ts +++ b/src/modules/random/index.ts @@ -1,4 +1,5 @@ import type { Faker } from '../..'; +import { deprecated } from '../../internal/deprecated'; import type { LiteralUnion } from '../../utils/types'; import type { AlphaChar, From 17c2a1eee01f53309be98449cf81fd8edc2606d4 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Mon, 17 Oct 2022 18:37:33 +0200 Subject: [PATCH 29/48] chore: fix calling new method --- src/modules/finance/index.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/modules/finance/index.ts b/src/modules/finance/index.ts index bbc9268bf02..7e71cd7b670 100644 --- a/src/modules/finance/index.ts +++ b/src/modules/finance/index.ts @@ -235,7 +235,8 @@ export class FinanceModule { let address = this.faker.helpers.arrayElement(['1', '3']); - address += this.faker.random.alphaNumeric(addressLength, { + address += this.faker.string.alphanumeric({ + count: addressLength, casing: 'mixed', bannedChars: '0OIl', }); From 647b63bec85e684bfb00643a234e8fbc0970c1f2 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Mon, 17 Oct 2022 18:42:50 +0200 Subject: [PATCH 30/48] test: revert datatype test file --- test/datatype.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/datatype.spec.ts b/test/datatype.spec.ts index 0c0683770a3..25847e07415 100644 --- a/test/datatype.spec.ts +++ b/test/datatype.spec.ts @@ -332,7 +332,7 @@ describe('datatype', () => { it('generates single hex character when no additional argument was provided', () => { const hex = faker.datatype.hexadecimal(); expect(hex).toMatch(/^(0x)[0-9a-f]{1}$/i); - expect(hex).toHaveLength(3); + expect(hex.substring(2)).toHaveLength(1); }); it('generates a hex string with a provided prefix', () => { @@ -344,7 +344,7 @@ describe('datatype', () => { it('generates a random hex string with a provided length', () => { const hex = faker.datatype.hexadecimal({ length: 5 }); expect(hex).toMatch(/^(0x)[0-9a-f]+$/i); - expect(hex).toHaveLength(7); + expect(hex.substring(2)).toHaveLength(5); }); it('generates a hex string with a provided casing', () => { From 44fea5017bf890f31e1aca54ddd6d435f4cc8dc8 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Mon, 17 Oct 2022 18:50:18 +0200 Subject: [PATCH 31/48] chore: adjust minor things --- src/modules/random/index.ts | 6 +++--- test/helpers.spec.ts | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/modules/random/index.ts b/src/modules/random/index.ts index fbe9a61e2c4..958f2e2b389 100644 --- a/src/modules/random/index.ts +++ b/src/modules/random/index.ts @@ -228,7 +228,7 @@ export class RandomModule { * * @since 3.1.0 * - * @deprecated Use faker.string.alphaNumeric() instead. + * @deprecated Use faker.string.alphanumeric() instead. */ alphaNumeric( count: number = 1, @@ -244,9 +244,9 @@ export class RandomModule { until: '9.0', }); return this.faker.string.alphanumeric({ + count, bannedChars: options.bannedChars, casing: options.casing, - count, }); } @@ -285,9 +285,9 @@ export class RandomModule { until: '9.0', }); return this.faker.string.numeric({ + length, allowLeadingZeros: options.allowLeadingZeros, bannedDigits: options.bannedDigits, - length, }); } } diff --git a/test/helpers.spec.ts b/test/helpers.spec.ts index fb0ad09ddf2..29a3a3af661 100644 --- a/test/helpers.spec.ts +++ b/test/helpers.spec.ts @@ -557,7 +557,7 @@ describe('helpers', () => { }); it('should be able to return empty strings', () => { - expect(faker.helpers.fake('{{random.alphaNumeric(0)}}')).toBe(''); + expect(faker.helpers.fake('{{string.alphanumeric(0)}}')).toBe(''); }); it('should be able to return locale definition strings', () => { @@ -604,12 +604,12 @@ describe('helpers', () => { it('should be able to handle special replacement patterns', () => { // eslint-disable-next-line @typescript-eslint/no-explicit-any - (faker.random as any).special = () => '$&'; + (faker.string as any).special = () => '$&'; - expect(faker.helpers.fake('{{random.special}}')).toBe('$&'); + expect(faker.helpers.fake('{{string.special}}')).toBe('$&'); // eslint-disable-next-line @typescript-eslint/no-explicit-any - delete (faker.random as any).special; + delete (faker.string as any).special; }); it('should support deprecated aliases', () => { From ea8f75a7e16920546e0851b7b2f0d0a542218100 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Mon, 17 Oct 2022 18:56:38 +0200 Subject: [PATCH 32/48] refactor: rename string.random to string.sample --- src/modules/datatype/index.ts | 12 ++++++------ src/modules/string/index.ts | 6 +++--- test/__snapshots__/string.spec.ts.snap | 12 ++++++------ test/helpers.spec.ts | 4 ++-- test/string.spec.ts | 16 ++++++++-------- 5 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/modules/datatype/index.ts b/src/modules/datatype/index.ts index 884f797537d..3ba15b06c9c 100644 --- a/src/modules/datatype/index.ts +++ b/src/modules/datatype/index.ts @@ -146,7 +146,7 @@ export class DatatypeModule { * * @param length Length of the generated string. Max length is `2^20`. Defaults to `10`. * - * @see faker.string.random() + * @see faker.string.sample() * * @example * faker.datatype.string() // 'Zo!.:*e>wR' @@ -154,16 +154,16 @@ export class DatatypeModule { * * @since 5.5.0 * - * @deprecated Use faker.string.random() instead. + * @deprecated Use faker.string.sample() instead. */ string(length = 10): string { deprecated({ deprecated: 'faker.datatype.string()', - proposed: 'faker.string.random()', + proposed: 'faker.string.sample()', since: '8.0', until: '9.0', }); - return this.faker.string.random(length); + return this.faker.string.sample(length); } /** @@ -265,7 +265,7 @@ export class DatatypeModule { properties.forEach((prop) => { returnObject[prop] = this.boolean() - ? this.faker.string.random() + ? this.faker.string.sample() : this.number(); }); @@ -285,7 +285,7 @@ export class DatatypeModule { */ array(length = 10): Array { return Array.from({ length }).map(() => - this.boolean() ? this.faker.string.random() : this.number() + this.boolean() ? this.faker.string.sample() : this.number() ); } diff --git a/src/modules/string/index.ts b/src/modules/string/index.ts index 99ebde85b14..a516e920ea1 100644 --- a/src/modules/string/index.ts +++ b/src/modules/string/index.ts @@ -99,12 +99,12 @@ export class StringModule { * @param length Length of the generated string. Max length is `2^20`. Defaults to `10`. * * @example - * faker.string.random() // 'Zo!.:*e>wR' - * faker.string.random(5) // '6Bye8' + * faker.string.sample() // 'Zo!.:*e>wR' + * faker.string.sample(5) // '6Bye8' * * @since 8.0.0 */ - random(length = 10): string { + sample(length = 10): string { const maxLength = Math.pow(2, 20); if (length >= maxLength) { length = maxLength; diff --git a/test/__snapshots__/string.spec.ts.snap b/test/__snapshots__/string.spec.ts.snap index 8e0e09e3a58..8a33e6f3539 100644 --- a/test/__snapshots__/string.spec.ts.snap +++ b/test/__snapshots__/string.spec.ts.snap @@ -1,10 +1,10 @@ // Vitest Snapshot v1 -exports[`string > random() > should return a deterministic string of given length 1`] = `"Cky2eiXX/J/*&Kq@X.b]\\"&{dnx4!1}2Z=YQ!I# sample() > should return a deterministic string of given length 1`] = `"Cky2eiXX/J/*&Kq@X.b]\\"&{dnx4!1}2Z=YQ!I# random() > should return a deterministic string of given length 2`] = `"9U/4:SK$>6QX9@{:e=+kD)[B,e|/Jqjjj!BLGDWQgC"`; +exports[`string > sample() > should return a deterministic string of given length 2`] = `"9U/4:SK$>6QX9@{:e=+kD)[B,e|/Jqjjj!BLGDWQgC"`; -exports[`string > random() > should return a deterministic string of given length 3`] = `"wKti5-}$_/\`4hHA0afl\\"h^]dnwI sample() > should return a deterministic string of given length 3`] = `"wKti5-}$_/\`4hHA0afl\\"h^]dnwI seed: 42 > alpha() 1`] = `"t"`; @@ -16,7 +16,7 @@ exports[`string > seed: 42 > hexadecimal() 1`] = `"8"`; exports[`string > seed: 42 > numeric() 1`] = `"4"`; -exports[`string > seed: 42 > random() 1`] = `"Cky2eiXX/J"`; +exports[`string > seed: 42 > sample() 1`] = `"Cky2eiXX/J"`; exports[`string > seed: 42 > uuid() 1`] = `"5cf2bc99-2721-407d-992b-a00fbdf302f2"`; @@ -30,7 +30,7 @@ exports[`string > seed: 1211 > hexadecimal() 1`] = `"E"`; exports[`string > seed: 1211 > numeric() 1`] = `"9"`; -exports[`string > seed: 1211 > random() 1`] = `"wKti5-}$_/"`; +exports[`string > seed: 1211 > sample() 1`] = `"wKti5-}$_/"`; exports[`string > seed: 1211 > uuid() 1`] = `"e7ec32f0-a2a3-4c65-abbd-0caabde64dfd"`; @@ -44,6 +44,6 @@ exports[`string > seed: 1337 > hexadecimal() 1`] = `"5"`; exports[`string > seed: 1337 > numeric() 1`] = `"3"`; -exports[`string > seed: 1337 > random() 1`] = `"9U/4:SK$>6"`; +exports[`string > seed: 1337 > sample() 1`] = `"9U/4:SK$>6"`; exports[`string > seed: 1337 > uuid() 1`] = `"48234870-5389-445f-8b41-c61a52bf27dc"`; diff --git a/test/helpers.spec.ts b/test/helpers.spec.ts index 29a3a3af661..44316c1d2e2 100644 --- a/test/helpers.spec.ts +++ b/test/helpers.spec.ts @@ -396,7 +396,7 @@ describe('helpers', () => { it('supports function replace values faker values', () => { const actual = faker.helpers.mustache('1{{value}}3', { - value: faker.string.random(2), + value: faker.string.sample(2), }); expect(actual).toHaveLength(4); @@ -404,7 +404,7 @@ describe('helpers', () => { it('supports function replace values faker function', () => { const actual = faker.helpers.mustache('1{{value}}3', { - value: () => faker.string.random(3), + value: () => faker.string.sample(3), }); expect(actual).toHaveLength(5); diff --git a/test/string.spec.ts b/test/string.spec.ts index 32f4cb7f831..3d8673cae08 100644 --- a/test/string.spec.ts +++ b/test/string.spec.ts @@ -9,7 +9,7 @@ const NON_SEEDED_BASED_RUN = 5; const functionNames: (keyof StringModule)[] = [ 'uuid', 'hexadecimal', - 'random', + 'sample', 'alpha', 'alphanumeric', 'numeric', @@ -42,11 +42,11 @@ describe('string', () => { }); }); - describe('random()', () => { + describe('sample()', () => { it('should return a deterministic string of given length', () => { faker.seed(seed); - const actual = faker.string.random(42); + const actual = faker.string.sample(42); expect(actual).toMatchSnapshot(); }); }); @@ -59,29 +59,29 @@ describe('string', () => { faker.seed() )}`, () => { for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) { - describe('random()', () => { + describe('sample()', () => { it('should generate a string value', () => { - const generatedString = faker.string.random(); + const generatedString = faker.string.sample(); expect(generatedString).toBeTypeOf('string'); expect(generatedString).toHaveLength(10); }); it('should return empty string if negative length is passed', () => { const negativeValue = faker.datatype.number({ min: -1000, max: -1 }); - const generatedString = faker.string.random(negativeValue); + const generatedString = faker.string.sample(negativeValue); expect(generatedString).toBe(''); expect(generatedString).toHaveLength(0); }); it('should return string with length of 2^20 if bigger length value is passed', () => { const overMaxValue = Math.pow(2, 28); - const generatedString = faker.string.random(overMaxValue); + const generatedString = faker.string.sample(overMaxValue); expect(generatedString).toHaveLength(Math.pow(2, 20)); }); it('should return string with a specific length', () => { const length = 1337; - const generatedString = faker.string.random(length); + const generatedString = faker.string.sample(length); expect(generatedString).toHaveLength(length); }); }); From 7c459be52259068d8aa4b663280079dd2fd6fdea Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Mon, 17 Oct 2022 19:17:53 +0200 Subject: [PATCH 33/48] chore: revert signature changes --- src/modules/color/index.ts | 5 +++- src/modules/database/index.ts | 6 ++++- src/modules/datatype/index.ts | 13 +--------- src/modules/finance/index.ts | 8 +++--- src/modules/git/index.ts | 12 +++++++-- src/modules/string/index.ts | 35 +++++++++++++++++++++----- test/__snapshots__/string.spec.ts.snap | 12 ++++----- test/string.spec.ts | 11 +++++--- 8 files changed, 67 insertions(+), 35 deletions(-) diff --git a/src/modules/color/index.ts b/src/modules/color/index.ts index f14f4d2b1ed..4fd79a063e9 100644 --- a/src/modules/color/index.ts +++ b/src/modules/color/index.ts @@ -313,7 +313,10 @@ export class ColorModule { let color: string | number[]; let cssFunction: CSSFunction = 'rgb'; if (format === 'hex') { - color = this.faker.string.hexadecimal(includeAlpha ? 8 : 6); + color = this.faker.string.hexadecimal({ + length: includeAlpha ? 8 : 6, + prefix: '', + }); color = formatHexColor(color, options); return color; } diff --git a/src/modules/database/index.ts b/src/modules/database/index.ts index 0166e2fbef5..09ffacd36da 100644 --- a/src/modules/database/index.ts +++ b/src/modules/database/index.ts @@ -79,6 +79,10 @@ export class DatabaseModule { * @since 6.2.0 */ mongodbObjectId(): string { - return this.faker.string.hexadecimal(24).toLowerCase(); + return this.faker.string.hexadecimal({ + length: 24, + case: 'lower', + prefix: '', + }); } } diff --git a/src/modules/datatype/index.ts b/src/modules/datatype/index.ts index 3ba15b06c9c..fb8a1a15061 100644 --- a/src/modules/datatype/index.ts +++ b/src/modules/datatype/index.ts @@ -237,18 +237,7 @@ export class DatatypeModule { since: '8.0', until: '9.0', }); - - const { length = 1, prefix = '0x', case: letterCase = 'mixed' } = options; - - let wholeString = this.faker.string.hexadecimal(length); - - if (letterCase === 'upper') { - wholeString = wholeString.toUpperCase(); - } else if (letterCase === 'lower') { - wholeString = wholeString.toLowerCase(); - } - - return `${prefix}${wholeString}`; + return this.faker.string.hexadecimal(options); } /** diff --git a/src/modules/finance/index.ts b/src/modules/finance/index.ts index 7e71cd7b670..f74be0011d5 100644 --- a/src/modules/finance/index.ts +++ b/src/modules/finance/index.ts @@ -354,9 +354,11 @@ export class FinanceModule { * @since 5.0.0 */ ethereumAddress(): string { - const addressPrefix = '0x'; - const address = this.faker.string.hexadecimal(40).toLowerCase(); - return addressPrefix + address; + const address = this.faker.string.hexadecimal({ + length: 40, + case: 'lower', + }); + return address; } /** diff --git a/src/modules/git/index.ts b/src/modules/git/index.ts index f5613401481..80a9c7a99e4 100644 --- a/src/modules/git/index.ts +++ b/src/modules/git/index.ts @@ -100,7 +100,11 @@ export class GitModule { * @since 5.0.0 */ commitSha(): string { - return this.faker.string.hexadecimal(40).toLowerCase(); + return this.faker.string.hexadecimal({ + length: 40, + case: 'lower', + prefix: '', + }); } /** @@ -112,6 +116,10 @@ export class GitModule { * @since 5.0.0 */ shortSha(): string { - return this.faker.string.hexadecimal(7).toLowerCase(); + return this.faker.string.hexadecimal({ + length: 7, + case: 'lower', + prefix: '', + }); } } diff --git a/src/modules/string/index.ts b/src/modules/string/index.ts index a516e920ea1..86ff96ec204 100644 --- a/src/modules/string/index.ts +++ b/src/modules/string/index.ts @@ -359,18 +359,35 @@ export class StringModule { /** * Returns a [hexadecimal](https://en.wikipedia.org/wiki/Hexadecimal) string. * - * @param length Length of the generated string. Defaults to `1`. + * @param options The optional options object. + * @param options.length Length of the generated number. Defaults to `1`. + * @param options.prefix Prefix for the generated number. Defaults to `'0x'`. + * @param options.case Case of the generated number. Defaults to `'mixed'`. * * @example - * faker.string.hexadecimal() // 'b' - * faker.string.hexadecimal(10) // 'aE13F044fb' + * faker.string.hexadecimal() // '0xB' + * faker.string.hexadecimal({ length: 10 }) // '0xaE13d044cB' + * faker.string.hexadecimal({ prefix: '0x' }) // '0xE' + * faker.string.hexadecimal({ case: 'lower' }) // '0xf' + * faker.string.hexadecimal({ length: 10, prefix: '#' }) // '#f12a974eB1' + * faker.string.hexadecimal({ length: 10, case: 'upper' }) // '0xE3F38014FB' + * faker.string.hexadecimal({ prefix: '', case: 'lower' }) // 'd' + * faker.string.hexadecimal({ length: 10, prefix: '0x', case: 'mixed' }) // '0xAdE330a4D1' * * @since 8.0.0 */ - hexadecimal(length = 1): string { + hexadecimal( + options: { + length?: number; + prefix?: string; + case?: 'lower' | 'upper' | 'mixed'; + } = {} + ): string { + const { length = 1, prefix = '0x', case: letterCase = 'mixed' } = options; + let wholeString = ''; - while (wholeString.length < length) { + for (let i = 0; i < length; i++) { wholeString += this.faker.helpers.arrayElement([ '0', '1', @@ -397,6 +414,12 @@ export class StringModule { ]); } - return wholeString; + if (letterCase === 'upper') { + wholeString = wholeString.toUpperCase(); + } else if (letterCase === 'lower') { + wholeString = wholeString.toLowerCase(); + } + + return `${prefix}${wholeString}`; } } diff --git a/test/__snapshots__/string.spec.ts.snap b/test/__snapshots__/string.spec.ts.snap index 8a33e6f3539..abe5a478e0e 100644 --- a/test/__snapshots__/string.spec.ts.snap +++ b/test/__snapshots__/string.spec.ts.snap @@ -10,9 +10,9 @@ exports[`string > seed: 42 > alpha() 1`] = `"t"`; exports[`string > seed: 42 > alphanumeric() 1`] = `"n"`; -exports[`string > seed: 42 > hexadecimal() > should return a deterministic hex of given length 1`] = `"8BE4ABdd39321aD7d3fe01FfCE404F4d6db0906bd8"`; +exports[`string > seed: 42 > hexadecimal() > should return a deterministic hex of given length 1`] = `"0x8BE4ABdd39321aD7d3fe01FfCE404F4d6db0906bd8"`; -exports[`string > seed: 42 > hexadecimal() 1`] = `"8"`; +exports[`string > seed: 42 > hexadecimal() 1`] = `"0x8"`; exports[`string > seed: 42 > numeric() 1`] = `"4"`; @@ -24,9 +24,9 @@ exports[`string > seed: 1211 > alpha() 1`] = `"W"`; exports[`string > seed: 1211 > alphanumeric() 1`] = `"V"`; -exports[`string > seed: 1211 > hexadecimal() > should return a deterministic hex of given length 1`] = `"EaDB42F0e3f4A973fAB0AeefCE96DFCF49cD438dF9"`; +exports[`string > seed: 1211 > hexadecimal() > should return a deterministic hex of given length 1`] = `"0xEaDB42F0e3f4A973fAB0AeefCE96DFCF49cD438dF9"`; -exports[`string > seed: 1211 > hexadecimal() 1`] = `"E"`; +exports[`string > seed: 1211 > hexadecimal() 1`] = `"0xE"`; exports[`string > seed: 1211 > numeric() 1`] = `"9"`; @@ -38,9 +38,9 @@ exports[`string > seed: 1337 > alpha() 1`] = `"n"`; exports[`string > seed: 1337 > alphanumeric() 1`] = `"g"`; -exports[`string > seed: 1337 > hexadecimal() > should return a deterministic hex of given length 1`] = `"5c346ba075bd57F5A62B82d72AF39CBBB07a98cbA8"`; +exports[`string > seed: 1337 > hexadecimal() > should return a deterministic hex of given length 1`] = `"0x5c346ba075bd57F5A62B82d72AF39CBBB07a98cbA8"`; -exports[`string > seed: 1337 > hexadecimal() 1`] = `"5"`; +exports[`string > seed: 1337 > hexadecimal() 1`] = `"0x5"`; exports[`string > seed: 1337 > numeric() 1`] = `"3"`; diff --git a/test/string.spec.ts b/test/string.spec.ts index 3d8673cae08..58c3c375db2 100644 --- a/test/string.spec.ts +++ b/test/string.spec.ts @@ -36,7 +36,7 @@ describe('string', () => { it('should return a deterministic hex of given length', () => { faker.seed(seed); - const actual = faker.string.hexadecimal(42); + const actual = faker.string.hexadecimal({ length: 42 }); expect(actual).toMatchSnapshot(); }); }); @@ -98,12 +98,15 @@ describe('string', () => { describe(`hexadecimal()`, () => { it('generates single hex character when no additional argument was provided', () => { const hex = faker.string.hexadecimal(); - expect(hex).toMatch(/^[0-9a-f]*$/i); - expect(hex).toHaveLength(1); + expect(hex).toMatch(/^0x[0-9a-f]*$/i); + expect(hex).toHaveLength(3); }); it('generates a random hex string', () => { - const hex = faker.string.hexadecimal(5); + const hex = faker.string.hexadecimal({ + length: 5, + prefix: '', + }); expect(hex).toMatch(/^[0-9a-f]*$/i); expect(hex).toHaveLength(5); }); From 2a0244b42be41112d09e86928f8499a116787768 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Mon, 17 Oct 2022 19:31:47 +0200 Subject: [PATCH 34/48] refactor: rename count to length --- src/modules/finance/index.ts | 8 ++++---- src/modules/random/index.ts | 7 +++++-- src/modules/string/index.ts | 30 +++++++++++++++--------------- src/modules/vehicle/index.ts | 13 ++++++++----- test/string.spec.ts | 36 ++++++++++++++++++------------------ 5 files changed, 50 insertions(+), 44 deletions(-) diff --git a/src/modules/finance/index.ts b/src/modules/finance/index.ts index f74be0011d5..1b9d4e7ec05 100644 --- a/src/modules/finance/index.ts +++ b/src/modules/finance/index.ts @@ -236,7 +236,7 @@ export class FinanceModule { let address = this.faker.helpers.arrayElement(['1', '3']); address += this.faker.string.alphanumeric({ - count: addressLength, + length: addressLength, casing: 'mixed', bannedChars: '0OIl', }); @@ -448,17 +448,17 @@ export class FinanceModule { const { includeBranchCode = this.faker.datatype.boolean() } = options; const bankIdentifier = this.faker.string.alpha({ - count: 4, + length: 4, casing: 'upper', }); const countryCode = this.faker.helpers.arrayElement(iban.iso3166); const locationCode = this.faker.string.alphanumeric({ - count: 2, + length: 2, casing: 'upper', }); const branchCode = includeBranchCode ? this.faker.datatype.boolean() - ? this.faker.string.alphanumeric({ count: 3, casing: 'upper' }) + ? this.faker.string.alphanumeric({ length: 3, casing: 'upper' }) : 'XXX' : ''; diff --git a/src/modules/random/index.ts b/src/modules/random/index.ts index 958f2e2b389..d51d6273be8 100644 --- a/src/modules/random/index.ts +++ b/src/modules/random/index.ts @@ -208,7 +208,10 @@ export class RandomModule { since: '8.0', until: '9.0', }); - return this.faker.string.alpha(options); + if (typeof options === 'number') { + return this.faker.string.alpha(options); + } + return this.faker.string.alpha({ ...options, length: options.count }); } /** @@ -244,7 +247,7 @@ export class RandomModule { until: '9.0', }); return this.faker.string.alphanumeric({ - count, + length: count, bannedChars: options.bannedChars, casing: options.casing, }); diff --git a/src/modules/string/index.ts b/src/modules/string/index.ts index 86ff96ec204..593b40bacb0 100644 --- a/src/modules/string/index.ts +++ b/src/modules/string/index.ts @@ -129,8 +129,8 @@ export class StringModule { /** * Generating a string consisting of letters in the English alphabet. * - * @param options Either the number of characters or an options instance. Defaults to `{ count: 1, casing: 'lower', bannedChars: [] }`. - * @param options.count The number of characters to generate. Defaults to `1`. + * @param options Either the number of characters or an options instance. Defaults to `{ length: 1, casing: 'lower', bannedChars: [] }`. + * @param options.length The number of characters to generate. Defaults to `1`. * @param options.casing The casing of the characters. Defaults to `'lower'`. * @param options.upcase Deprecated, use `casing: 'upper'` instead. * @param options.bannedChars An array with characters to exclude. Defaults to `[]`. @@ -138,7 +138,7 @@ export class StringModule { * @example * faker.string.alpha() // 'b' * faker.string.alpha(10) // 'qccrabobaf' - * faker.string.alpha({ count: 5, casing: 'upper', bannedChars: ['A'] }) // 'DTCIC' + * faker.string.alpha({ length: 5, casing: 'upper', bannedChars: ['A'] }) // 'DTCIC' * * @since 8.0.0 */ @@ -146,25 +146,25 @@ export class StringModule { options: | number | { - count?: number; + length?: number; casing?: Casing; bannedChars?: readonly LiteralUnion[] | string; } = {} ): string { if (typeof options === 'number') { options = { - count: options, + length: options, }; } - const { count = 1, casing = 'mixed' } = options; + const { length = 1, casing = 'mixed' } = options; let { bannedChars = [] } = options; if (typeof bannedChars === 'string') { bannedChars = bannedChars.split(''); } - if (count <= 0) { + if (length <= 0) { return ''; } @@ -190,7 +190,7 @@ export class StringModule { ); } - return Array.from({ length: count }, () => + return Array.from({ length }, () => this.faker.helpers.arrayElement(charsArray) ).join(''); } @@ -198,8 +198,8 @@ export class StringModule { /** * Generating a string consisting of alpha characters and digits. * - * @param options Either the number of characters or an options instance. Defaults to `{ count: 1, casing: 'mixed', bannedChars: [] }`. - * @param options.count The number of characters and digits to generate. Defaults to `1`. + * @param options Either the number of characters or an options instance. Defaults to `{ length: 1, casing: 'mixed', bannedChars: [] }`. + * @param options.length The number of characters and digits to generate. Defaults to `1`. * @param options.casing The casing of the characters. Defaults to `'mixed'`. * @param options.bannedChars An array of characters and digits which should be banned in the generated string. Defaults to `[]`. * @@ -214,20 +214,20 @@ export class StringModule { options: | number | { - count?: number; + length?: number; casing?: Casing; bannedChars?: readonly LiteralUnion[] | string; } = {} ): string { if (typeof options === 'number') { options = { - count: options, + length: options, }; } - const { casing = 'mixed', count = 1 } = options; + const { casing = 'mixed', length = 1 } = options; - if (count <= 0) { + if (length <= 0) { return ''; } @@ -260,7 +260,7 @@ export class StringModule { ); } - return Array.from({ length: count }, () => + return Array.from({ length }, () => this.faker.helpers.arrayElement(charsArray) ).join(''); } diff --git a/src/modules/vehicle/index.ts b/src/modules/vehicle/index.ts index d6421cfc943..13ae45b0196 100644 --- a/src/modules/vehicle/index.ts +++ b/src/modules/vehicle/index.ts @@ -89,15 +89,15 @@ export class VehicleModule { vin(): string { const bannedChars = ['o', 'i', 'q', 'O', 'I', 'Q']; return `${this.faker.string.alphanumeric({ - count: 10, + length: 10, casing: 'upper', bannedChars, })}${this.faker.string.alpha({ - count: 1, + length: 1, casing: 'upper', bannedChars, })}${this.faker.string.alphanumeric({ - count: 1, + length: 1, casing: 'upper', bannedChars, })}${this.faker.datatype.number({ min: 10000, max: 99999 })}` // return five digit # @@ -126,7 +126,7 @@ export class VehicleModule { */ vrm(): string { return `${this.faker.string.alpha({ - count: 2, + length: 2, casing: 'upper', })}${this.faker.datatype.number({ min: 0, @@ -134,7 +134,10 @@ export class VehicleModule { })}${this.faker.datatype.number({ min: 0, max: 9, - })}${this.faker.string.alpha({ count: 3, casing: 'upper' })}`.toUpperCase(); + })}${this.faker.string.alpha({ + length: 3, + casing: 'upper', + })}`.toUpperCase(); } /** diff --git a/test/string.spec.ts b/test/string.spec.ts index 58c3c375db2..a8c3aae9fb3 100644 --- a/test/string.spec.ts +++ b/test/string.spec.ts @@ -113,7 +113,7 @@ describe('string', () => { }); describe('alpha()', () => { - it('should return single letter when no count provided', () => { + it('should return single letter when no length provided', () => { const actual = faker.string.alpha(); expect(actual).toHaveLength(1); @@ -130,7 +130,7 @@ describe('string', () => { ['lower', /^[a-z]{250}$/], ['mixed', /^[a-zA-Z]{250}$/], ] as const)('should return %s-case', (casing, pattern) => { - const actual = faker.string.alpha({ count: 250, casing }); + const actual = faker.string.alpha({ length: 250, casing }); expect(actual).toMatch(pattern); }); @@ -151,7 +151,7 @@ describe('string', () => { it('should be able to ban some characters', () => { const actual = faker.string.alpha({ - count: 5, + length: 5, bannedChars: ['a', 'p'], casing: 'lower', }); @@ -162,7 +162,7 @@ describe('string', () => { it('should be able to ban some characters via string', () => { const actual = faker.string.alpha({ - count: 5, + length: 5, bannedChars: 'ap', casing: 'lower', }); @@ -173,7 +173,7 @@ describe('string', () => { it('should be able handle mistake in banned characters array', () => { const alphaText = faker.string.alpha({ - count: 5, + length: 5, bannedChars: ['a', 'a', 'p'], casing: 'lower', }); @@ -186,7 +186,7 @@ describe('string', () => { const bannedChars = 'abcdefghijklmnopqrstuvwxyz'.split(''); expect(() => faker.string.alpha({ - count: 5, + length: 5, bannedChars, casing: 'lower', }) @@ -199,11 +199,11 @@ describe('string', () => { it('should not mutate the input object', () => { const input: { - count: number; + length: number; casing: 'mixed'; bannedChars: string[]; } = Object.freeze({ - count: 5, + length: 5, casing: 'mixed', bannedChars: ['a', '%'], }); @@ -225,7 +225,7 @@ describe('string', () => { ['lower', /^[a-z0-9]{250}$/], ['mixed', /^[a-zA-Z0-9]{250}$/], ] as const)('should return %s-case', (casing, pattern) => { - const actual = faker.string.alphanumeric({ count: 250, casing }); + const actual = faker.string.alphanumeric({ length: 250, casing }); expect(actual).toMatch(pattern); }); @@ -247,7 +247,7 @@ describe('string', () => { it('should be able to ban all alphabetic characters', () => { const bannedChars = 'abcdefghijklmnopqrstuvwxyz'.split(''); const alphaText = faker.string.alphanumeric({ - count: 5, + length: 5, bannedChars, casing: 'lower', }); @@ -261,7 +261,7 @@ describe('string', () => { it('should be able to ban all alphabetic characters via string', () => { const bannedChars = 'abcdefghijklmnopqrstuvwxyz'; const alphaText = faker.string.alphanumeric({ - count: 5, + length: 5, bannedChars, casing: 'lower', }); @@ -275,7 +275,7 @@ describe('string', () => { it('should be able to ban all numeric characters', () => { const bannedChars = '0123456789'.split(''); const alphaText = faker.string.alphanumeric({ - count: 5, + length: 5, bannedChars, }); @@ -288,7 +288,7 @@ describe('string', () => { it('should be able to ban all numeric characters via string', () => { const bannedChars = '0123456789'; const alphaText = faker.string.alphanumeric({ - count: 5, + length: 5, bannedChars, }); @@ -300,7 +300,7 @@ describe('string', () => { it('should be able to handle mistake in banned characters array', () => { const alphaText = faker.string.alphanumeric({ - count: 5, + length: 5, bannedChars: ['a', 'p', 'a'], casing: 'lower', }); @@ -313,7 +313,7 @@ describe('string', () => { const bannedChars = 'abcdefghijklmnopqrstuvwxyz0123456789'.split(''); expect(() => faker.string.alphanumeric({ - count: 5, + length: 5, bannedChars, casing: 'lower', }) @@ -328,7 +328,7 @@ describe('string', () => { const bannedChars = 'abcdefghijklmnopqrstuvwxyz0123456789'; expect(() => faker.string.alphanumeric({ - count: 5, + length: 5, bannedChars, casing: 'lower', }) @@ -338,10 +338,10 @@ describe('string', () => { it('should not mutate the input object', () => { const input: { bannedChars: string[]; - count: number; + length: number; } = Object.freeze({ bannedChars: ['a', '0', '%'], - count: 5, + length: 5, }); expect(() => faker.string.alphanumeric(input)).not.toThrow(); From d8352c63461a4feca8461999ae9513323e3bc8f3 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Mon, 17 Oct 2022 19:35:22 +0200 Subject: [PATCH 35/48] refactor: rename case to casing --- src/modules/database/index.ts | 2 +- src/modules/datatype/index.ts | 2 +- src/modules/finance/index.ts | 2 +- src/modules/git/index.ts | 4 ++-- src/modules/string/index.ts | 19 +++++++++---------- test/color.spec.ts | 8 ++++---- 6 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/modules/database/index.ts b/src/modules/database/index.ts index 09ffacd36da..d0985c53f4f 100644 --- a/src/modules/database/index.ts +++ b/src/modules/database/index.ts @@ -81,7 +81,7 @@ export class DatabaseModule { mongodbObjectId(): string { return this.faker.string.hexadecimal({ length: 24, - case: 'lower', + casing: 'lower', prefix: '', }); } diff --git a/src/modules/datatype/index.ts b/src/modules/datatype/index.ts index fb8a1a15061..41c5c3e4eb2 100644 --- a/src/modules/datatype/index.ts +++ b/src/modules/datatype/index.ts @@ -237,7 +237,7 @@ export class DatatypeModule { since: '8.0', until: '9.0', }); - return this.faker.string.hexadecimal(options); + return this.faker.string.hexadecimal({ ...options, casing: options.case }); } /** diff --git a/src/modules/finance/index.ts b/src/modules/finance/index.ts index 1b9d4e7ec05..3c60ec41d06 100644 --- a/src/modules/finance/index.ts +++ b/src/modules/finance/index.ts @@ -356,7 +356,7 @@ export class FinanceModule { ethereumAddress(): string { const address = this.faker.string.hexadecimal({ length: 40, - case: 'lower', + casing: 'lower', }); return address; } diff --git a/src/modules/git/index.ts b/src/modules/git/index.ts index 80a9c7a99e4..78cf0c684b6 100644 --- a/src/modules/git/index.ts +++ b/src/modules/git/index.ts @@ -102,7 +102,7 @@ export class GitModule { commitSha(): string { return this.faker.string.hexadecimal({ length: 40, - case: 'lower', + casing: 'lower', prefix: '', }); } @@ -118,7 +118,7 @@ export class GitModule { shortSha(): string { return this.faker.string.hexadecimal({ length: 7, - case: 'lower', + casing: 'lower', prefix: '', }); } diff --git a/src/modules/string/index.ts b/src/modules/string/index.ts index 593b40bacb0..44dab4dc900 100644 --- a/src/modules/string/index.ts +++ b/src/modules/string/index.ts @@ -132,7 +132,6 @@ export class StringModule { * @param options Either the number of characters or an options instance. Defaults to `{ length: 1, casing: 'lower', bannedChars: [] }`. * @param options.length The number of characters to generate. Defaults to `1`. * @param options.casing The casing of the characters. Defaults to `'lower'`. - * @param options.upcase Deprecated, use `casing: 'upper'` instead. * @param options.bannedChars An array with characters to exclude. Defaults to `[]`. * * @example @@ -362,17 +361,17 @@ export class StringModule { * @param options The optional options object. * @param options.length Length of the generated number. Defaults to `1`. * @param options.prefix Prefix for the generated number. Defaults to `'0x'`. - * @param options.case Case of the generated number. Defaults to `'mixed'`. + * @param options.casing Casing of the generated number. Defaults to `'mixed'`. * * @example * faker.string.hexadecimal() // '0xB' * faker.string.hexadecimal({ length: 10 }) // '0xaE13d044cB' * faker.string.hexadecimal({ prefix: '0x' }) // '0xE' - * faker.string.hexadecimal({ case: 'lower' }) // '0xf' + * faker.string.hexadecimal({ casing: 'lower' }) // '0xf' * faker.string.hexadecimal({ length: 10, prefix: '#' }) // '#f12a974eB1' - * faker.string.hexadecimal({ length: 10, case: 'upper' }) // '0xE3F38014FB' - * faker.string.hexadecimal({ prefix: '', case: 'lower' }) // 'd' - * faker.string.hexadecimal({ length: 10, prefix: '0x', case: 'mixed' }) // '0xAdE330a4D1' + * faker.string.hexadecimal({ length: 10, casing: 'upper' }) // '0xE3F38014FB' + * faker.string.hexadecimal({ prefix: '', casing: 'lower' }) // 'd' + * faker.string.hexadecimal({ length: 10, prefix: '0x', casing: 'mixed' }) // '0xAdE330a4D1' * * @since 8.0.0 */ @@ -380,10 +379,10 @@ export class StringModule { options: { length?: number; prefix?: string; - case?: 'lower' | 'upper' | 'mixed'; + casing?: 'lower' | 'upper' | 'mixed'; } = {} ): string { - const { length = 1, prefix = '0x', case: letterCase = 'mixed' } = options; + const { length = 1, prefix = '0x', casing = 'mixed' } = options; let wholeString = ''; @@ -414,9 +413,9 @@ export class StringModule { ]); } - if (letterCase === 'upper') { + if (casing === 'upper') { wholeString = wholeString.toUpperCase(); - } else if (letterCase === 'lower') { + } else if (casing === 'lower') { wholeString = wholeString.toLowerCase(); } diff --git a/test/color.spec.ts b/test/color.spec.ts index 66db2620096..091d7ff0e37 100644 --- a/test/color.spec.ts +++ b/test/color.spec.ts @@ -73,15 +73,15 @@ describe('color', () => { }); }); - describe(`rgbHex({ prefix: '0x', case: 'lower' })`, () => { - it('should return a random rgb hex color with # prefix and lower case only', () => { + describe(`rgbHex({ prefix: '0x', casing: 'lower' })`, () => { + it('should return a random rgb hex color with # prefix and lower casing only', () => { const color = faker.color.rgb({ prefix: '0x', casing: 'lower' }); expect(color).match(/^(0x[a-f0-9]{6})$/); }); }); - describe(`rgb({ prefix: '0x', case: 'upper' })`, () => { - it('should return a random rgb hex color with # prefix and upper case only', () => { + describe(`rgb({ prefix: '0x', casing: 'upper' })`, () => { + it('should return a random rgb hex color with # prefix and upper casing only', () => { const color = faker.color.rgb({ prefix: '0x', casing: 'upper' }); expect(color).match(/^(0x[A-F0-9]{6})$/); }); From 8a847b7aa0407f82666760cee8bcd04ae741f93f Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Mon, 17 Oct 2022 19:44:44 +0200 Subject: [PATCH 36/48] test: clean some casing to lower --- test/random.spec.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/test/random.spec.ts b/test/random.spec.ts index 5b068bed5b8..2713880af6d 100644 --- a/test/random.spec.ts +++ b/test/random.spec.ts @@ -130,7 +130,7 @@ describe('random', () => { it('should return mixed letter when no option provided', () => { const actual = faker.random.alpha(); - expect(actual).toMatch(/^[a-zA-Z]$/); + expect(actual).toMatch(/^[a-z]$/i); }); it.each([ @@ -254,7 +254,6 @@ describe('random', () => { const bannedChars = 'abcdefghijklmnopqrstuvwxyz'.split(''); const alphaText = faker.random.alphaNumeric(5, { bannedChars, - casing: 'lower', }); expect(alphaText).toHaveLength(5); @@ -267,7 +266,6 @@ describe('random', () => { const bannedChars = 'abcdefghijklmnopqrstuvwxyz'; const alphaText = faker.random.alphaNumeric(5, { bannedChars, - casing: 'lower', }); expect(alphaText).toHaveLength(5); @@ -280,7 +278,6 @@ describe('random', () => { const bannedChars = '0123456789'.split(''); const alphaText = faker.random.alphaNumeric(5, { bannedChars, - casing: 'lower', }); expect(alphaText).toHaveLength(5); @@ -293,7 +290,6 @@ describe('random', () => { const bannedChars = '0123456789'; const alphaText = faker.random.alphaNumeric(5, { bannedChars, - casing: 'lower', }); expect(alphaText).toHaveLength(5); From 97131a67aa572a6133fa15e28c20049f7266717a Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Mon, 17 Oct 2022 19:49:12 +0200 Subject: [PATCH 37/48] chore: sort methods alphabetically --- src/modules/string/index.ts | 200 ++++++++++++++++++------------------ test/string.spec.ts | 112 ++++++++++---------- 2 files changed, 156 insertions(+), 156 deletions(-) diff --git a/src/modules/string/index.ts b/src/modules/string/index.ts index 44dab4dc900..645141ffc9f 100644 --- a/src/modules/string/index.ts +++ b/src/modules/string/index.ts @@ -93,39 +93,6 @@ export class StringModule { } } - /** - * Returns a string containing UTF-16 chars between 33 and 125 (`!` to `}`). - * - * @param length Length of the generated string. Max length is `2^20`. Defaults to `10`. - * - * @example - * faker.string.sample() // 'Zo!.:*e>wR' - * faker.string.sample(5) // '6Bye8' - * - * @since 8.0.0 - */ - sample(length = 10): string { - const maxLength = Math.pow(2, 20); - if (length >= maxLength) { - length = maxLength; - } - - const charCodeOption = { - min: 33, - max: 125, - }; - - let returnString = ''; - - while (returnString.length < length) { - returnString += String.fromCharCode( - this.faker.datatype.number(charCodeOption) - ); - } - - return returnString; - } - /** * Generating a string consisting of letters in the English alphabet. * @@ -264,6 +231,73 @@ export class StringModule { ).join(''); } + /** + * Returns a [hexadecimal](https://en.wikipedia.org/wiki/Hexadecimal) string. + * + * @param options The optional options object. + * @param options.length Length of the generated number. Defaults to `1`. + * @param options.prefix Prefix for the generated number. Defaults to `'0x'`. + * @param options.casing Casing of the generated number. Defaults to `'mixed'`. + * + * @example + * faker.string.hexadecimal() // '0xB' + * faker.string.hexadecimal({ length: 10 }) // '0xaE13d044cB' + * faker.string.hexadecimal({ prefix: '0x' }) // '0xE' + * faker.string.hexadecimal({ casing: 'lower' }) // '0xf' + * faker.string.hexadecimal({ length: 10, prefix: '#' }) // '#f12a974eB1' + * faker.string.hexadecimal({ length: 10, casing: 'upper' }) // '0xE3F38014FB' + * faker.string.hexadecimal({ prefix: '', casing: 'lower' }) // 'd' + * faker.string.hexadecimal({ length: 10, prefix: '0x', casing: 'mixed' }) // '0xAdE330a4D1' + * + * @since 8.0.0 + */ + hexadecimal( + options: { + length?: number; + prefix?: string; + casing?: 'lower' | 'upper' | 'mixed'; + } = {} + ): string { + const { length = 1, prefix = '0x', casing = 'mixed' } = options; + + let wholeString = ''; + + for (let i = 0; i < length; i++) { + wholeString += this.faker.helpers.arrayElement([ + '0', + '1', + '2', + '3', + '4', + '5', + '6', + '7', + '8', + '9', + 'a', + 'b', + 'c', + 'd', + 'e', + 'f', + 'A', + 'B', + 'C', + 'D', + 'E', + 'F', + ]); + } + + if (casing === 'upper') { + wholeString = wholeString.toUpperCase(); + } else if (casing === 'lower') { + wholeString = wholeString.toLowerCase(); + } + + return `${prefix}${wholeString}`; + } + /** * Generates a given length string of digits. * @@ -337,6 +371,39 @@ export class StringModule { return result; } + /** + * Returns a string containing UTF-16 chars between 33 and 125 (`!` to `}`). + * + * @param length Length of the generated string. Max length is `2^20`. Defaults to `10`. + * + * @example + * faker.string.sample() // 'Zo!.:*e>wR' + * faker.string.sample(5) // '6Bye8' + * + * @since 8.0.0 + */ + sample(length = 10): string { + const maxLength = Math.pow(2, 20); + if (length >= maxLength) { + length = maxLength; + } + + const charCodeOption = { + min: 33, + max: 125, + }; + + let returnString = ''; + + while (returnString.length < length) { + returnString += String.fromCharCode( + this.faker.datatype.number(charCodeOption) + ); + } + + return returnString; + } + /** * Returns a UUID v4 ([Universally Unique Identifier](https://en.wikipedia.org/wiki/Universally_unique_identifier)). * @@ -354,71 +421,4 @@ export class StringModule { }; return RFC4122_TEMPLATE.replace(/[xy]/g, replacePlaceholders); } - - /** - * Returns a [hexadecimal](https://en.wikipedia.org/wiki/Hexadecimal) string. - * - * @param options The optional options object. - * @param options.length Length of the generated number. Defaults to `1`. - * @param options.prefix Prefix for the generated number. Defaults to `'0x'`. - * @param options.casing Casing of the generated number. Defaults to `'mixed'`. - * - * @example - * faker.string.hexadecimal() // '0xB' - * faker.string.hexadecimal({ length: 10 }) // '0xaE13d044cB' - * faker.string.hexadecimal({ prefix: '0x' }) // '0xE' - * faker.string.hexadecimal({ casing: 'lower' }) // '0xf' - * faker.string.hexadecimal({ length: 10, prefix: '#' }) // '#f12a974eB1' - * faker.string.hexadecimal({ length: 10, casing: 'upper' }) // '0xE3F38014FB' - * faker.string.hexadecimal({ prefix: '', casing: 'lower' }) // 'd' - * faker.string.hexadecimal({ length: 10, prefix: '0x', casing: 'mixed' }) // '0xAdE330a4D1' - * - * @since 8.0.0 - */ - hexadecimal( - options: { - length?: number; - prefix?: string; - casing?: 'lower' | 'upper' | 'mixed'; - } = {} - ): string { - const { length = 1, prefix = '0x', casing = 'mixed' } = options; - - let wholeString = ''; - - for (let i = 0; i < length; i++) { - wholeString += this.faker.helpers.arrayElement([ - '0', - '1', - '2', - '3', - '4', - '5', - '6', - '7', - '8', - '9', - 'a', - 'b', - 'c', - 'd', - 'e', - 'f', - 'A', - 'B', - 'C', - 'D', - 'E', - 'F', - ]); - } - - if (casing === 'upper') { - wholeString = wholeString.toUpperCase(); - } else if (casing === 'lower') { - wholeString = wholeString.toLowerCase(); - } - - return `${prefix}${wholeString}`; - } } diff --git a/test/string.spec.ts b/test/string.spec.ts index a8c3aae9fb3..4b7036ef937 100644 --- a/test/string.spec.ts +++ b/test/string.spec.ts @@ -7,12 +7,12 @@ import { times } from './support/times'; const NON_SEEDED_BASED_RUN = 5; const functionNames: (keyof StringModule)[] = [ - 'uuid', - 'hexadecimal', - 'sample', 'alpha', 'alphanumeric', + 'hexadecimal', 'numeric', + 'sample', + 'uuid', ]; describe('string', () => { @@ -59,59 +59,6 @@ describe('string', () => { faker.seed() )}`, () => { for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) { - describe('sample()', () => { - it('should generate a string value', () => { - const generatedString = faker.string.sample(); - expect(generatedString).toBeTypeOf('string'); - expect(generatedString).toHaveLength(10); - }); - - it('should return empty string if negative length is passed', () => { - const negativeValue = faker.datatype.number({ min: -1000, max: -1 }); - const generatedString = faker.string.sample(negativeValue); - expect(generatedString).toBe(''); - expect(generatedString).toHaveLength(0); - }); - - it('should return string with length of 2^20 if bigger length value is passed', () => { - const overMaxValue = Math.pow(2, 28); - const generatedString = faker.string.sample(overMaxValue); - expect(generatedString).toHaveLength(Math.pow(2, 20)); - }); - - it('should return string with a specific length', () => { - const length = 1337; - const generatedString = faker.string.sample(length); - expect(generatedString).toHaveLength(length); - }); - }); - - describe(`uuid()`, () => { - it('generates a valid UUID', () => { - const UUID = faker.string.uuid(); - const RFC4122 = - /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/; - expect(UUID).toMatch(RFC4122); - }); - }); - - describe(`hexadecimal()`, () => { - it('generates single hex character when no additional argument was provided', () => { - const hex = faker.string.hexadecimal(); - expect(hex).toMatch(/^0x[0-9a-f]*$/i); - expect(hex).toHaveLength(3); - }); - - it('generates a random hex string', () => { - const hex = faker.string.hexadecimal({ - length: 5, - prefix: '', - }); - expect(hex).toMatch(/^[0-9a-f]*$/i); - expect(hex).toHaveLength(5); - }); - }); - describe('alpha()', () => { it('should return single letter when no length provided', () => { const actual = faker.string.alpha(); @@ -349,6 +296,23 @@ describe('string', () => { }); }); + describe(`hexadecimal()`, () => { + it('generates single hex character when no additional argument was provided', () => { + const hex = faker.string.hexadecimal(); + expect(hex).toMatch(/^0x[0-9a-f]*$/i); + expect(hex).toHaveLength(3); + }); + + it('generates a random hex string', () => { + const hex = faker.string.hexadecimal({ + length: 5, + prefix: '', + }); + expect(hex).toMatch(/^[0-9a-f]*$/i); + expect(hex).toHaveLength(5); + }); + }); + describe('numeric', () => { it('should return single digit when no length provided', () => { const actual = faker.string.numeric(); @@ -464,6 +428,42 @@ describe('string', () => { expect(actual).toMatch(/^[0235679]{1000}$/); }); }); + + describe('sample()', () => { + it('should generate a string value', () => { + const generatedString = faker.string.sample(); + expect(generatedString).toBeTypeOf('string'); + expect(generatedString).toHaveLength(10); + }); + + it('should return empty string if negative length is passed', () => { + const negativeValue = faker.datatype.number({ min: -1000, max: -1 }); + const generatedString = faker.string.sample(negativeValue); + expect(generatedString).toBe(''); + expect(generatedString).toHaveLength(0); + }); + + it('should return string with length of 2^20 if bigger length value is passed', () => { + const overMaxValue = Math.pow(2, 28); + const generatedString = faker.string.sample(overMaxValue); + expect(generatedString).toHaveLength(Math.pow(2, 20)); + }); + + it('should return string with a specific length', () => { + const length = 1337; + const generatedString = faker.string.sample(length); + expect(generatedString).toHaveLength(length); + }); + }); + + describe(`uuid()`, () => { + it('generates a valid UUID', () => { + const UUID = faker.string.uuid(); + const RFC4122 = + /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/; + expect(UUID).toMatch(RFC4122); + }); + }); } }); }); From e3df60e44075749697677042866c0af1a58853ef Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Mon, 17 Oct 2022 19:50:09 +0200 Subject: [PATCH 38/48] test: nothing in string should depend on locale --- test/string.spec.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/test/string.spec.ts b/test/string.spec.ts index 4b7036ef937..58350ba22aa 100644 --- a/test/string.spec.ts +++ b/test/string.spec.ts @@ -1,4 +1,4 @@ -import { afterEach, describe, expect, it } from 'vitest'; +import { describe, expect, it } from 'vitest'; import { faker, FakerError } from '../src'; import type { StringModule } from '../src/modules/string'; import { seededRuns } from './support/seededRuns'; @@ -16,10 +16,6 @@ const functionNames: (keyof StringModule)[] = [ ]; describe('string', () => { - afterEach(() => { - faker.locale = 'en'; - }); - for (const seed of seededRuns) { describe(`seed: ${seed}`, () => { for (const functionName of functionNames) { From f54730fa074d3d6946f0965772754c3fb7afdda6 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Mon, 17 Oct 2022 19:53:46 +0200 Subject: [PATCH 39/48] chore: move sample max length to const --- src/modules/string/index.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/modules/string/index.ts b/src/modules/string/index.ts index 645141ffc9f..b4ab331708e 100644 --- a/src/modules/string/index.ts +++ b/src/modules/string/index.ts @@ -79,6 +79,8 @@ export type NumericChar = export type AlphaChar = LowerAlphaChar | UpperAlphaChar; export type AlphaNumericChar = AlphaChar | NumericChar; +const SAMPLE_MAX_LENGTH = Math.pow(2, 20); + /** * Module to generate string related entries. */ @@ -383,9 +385,8 @@ export class StringModule { * @since 8.0.0 */ sample(length = 10): string { - const maxLength = Math.pow(2, 20); - if (length >= maxLength) { - length = maxLength; + if (length >= SAMPLE_MAX_LENGTH) { + length = SAMPLE_MAX_LENGTH; } const charCodeOption = { From a05e72c14b95d88c62c3d54b30d26f42653f2d70 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Mon, 17 Oct 2022 19:59:20 +0200 Subject: [PATCH 40/48] docs: run pnpm run generate:api-docs --- docs/.vitepress/api-pages.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/.vitepress/api-pages.ts b/docs/.vitepress/api-pages.ts index 575ec823897..a4446a22ffe 100644 --- a/docs/.vitepress/api-pages.ts +++ b/docs/.vitepress/api-pages.ts @@ -22,6 +22,7 @@ export const apiPages = [ { text: 'Phone', link: '/api/phone.html' }, { text: 'Random', link: '/api/random.html' }, { text: 'Science', link: '/api/science.html' }, + { text: 'String', link: '/api/string.html' }, { text: 'System', link: '/api/system.html' }, { text: 'Vehicle', link: '/api/vehicle.html' }, { text: 'Word', link: '/api/word.html' }, From 1909555bfe249509bd6cf5c1b5c767146a736dd8 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Mon, 17 Oct 2022 22:16:31 +0200 Subject: [PATCH 41/48] test: use seeded tests factory --- test/__snapshots__/string.spec.ts.snap | 312 +++++++++++++++++++++++-- test/string.spec.ts | 106 +++++---- 2 files changed, 351 insertions(+), 67 deletions(-) diff --git a/test/__snapshots__/string.spec.ts.snap b/test/__snapshots__/string.spec.ts.snap index abe5a478e0e..cc0c7fc31e6 100644 --- a/test/__snapshots__/string.spec.ts.snap +++ b/test/__snapshots__/string.spec.ts.snap @@ -1,49 +1,313 @@ // Vitest Snapshot v1 -exports[`string > sample() > should return a deterministic string of given length 1`] = `"Cky2eiXX/J/*&Kq@X.b]\\"&{dnx4!1}2Z=YQ!I# 42 > alpha > noArgs 1`] = `"t"`; -exports[`string > sample() > should return a deterministic string of given length 2`] = `"9U/4:SK$>6QX9@{:e=+kD)[B,e|/Jqjjj!BLGDWQgC"`; +exports[`string > 42 > alpha > repeated 1`] = `"tPXjM"`; -exports[`string > sample() > should return a deterministic string of given length 3`] = `"wKti5-}$_/\`4hHA0afl\\"h^]dnwI 42 > alpha > repeated 2`] = `"OFFix"`; -exports[`string > seed: 42 > alpha() 1`] = `"t"`; +exports[`string > 42 > alpha > repeated 3`] = `"ifdxT"`; -exports[`string > seed: 42 > alphanumeric() 1`] = `"n"`; +exports[`string > 42 > alpha > repeated 4`] = `"rFhKH"`; -exports[`string > seed: 42 > hexadecimal() > should return a deterministic hex of given length 1`] = `"0x8BE4ABdd39321aD7d3fe01FfCE404F4d6db0906bd8"`; +exports[`string > 42 > alpha > repeated 5`] = `"bcYLR"`; -exports[`string > seed: 42 > hexadecimal() 1`] = `"0x8"`; +exports[`string > 42 > alpha > with bannedChars 1`] = `"A"`; -exports[`string > seed: 42 > numeric() 1`] = `"4"`; +exports[`string > 42 > alpha > with casing 1`] = `"j"`; -exports[`string > seed: 42 > sample() 1`] = `"Cky2eiXX/J"`; +exports[`string > 42 > alpha > with casing 2`] = `"J"`; -exports[`string > seed: 42 > uuid() 1`] = `"5cf2bc99-2721-407d-992b-a00fbdf302f2"`; +exports[`string > 42 > alpha > with casing 3`] = `"t"`; -exports[`string > seed: 1211 > alpha() 1`] = `"W"`; +exports[`string > 42 > alpha > with length 1`] = `"tPXjMO"`; -exports[`string > seed: 1211 > alphanumeric() 1`] = `"V"`; +exports[`string > 42 > alpha > with length, casing and bannedChars 1`] = `"fwzcvwj"`; -exports[`string > seed: 1211 > hexadecimal() > should return a deterministic hex of given length 1`] = `"0xEaDB42F0e3f4A973fAB0AeefCE96DFCF49cD438dF9"`; +exports[`string > 42 > alphanumeric > noArgs 1`] = `"n"`; -exports[`string > seed: 1211 > hexadecimal() 1`] = `"0xE"`; +exports[`string > 42 > alphanumeric > repeated 1`] = `"nNWbJ"`; -exports[`string > seed: 1211 > numeric() 1`] = `"9"`; +exports[`string > 42 > alphanumeric > repeated 2`] = `"MBB9r"`; -exports[`string > seed: 1211 > sample() 1`] = `"wKti5-}$_/"`; +exports[`string > 42 > alphanumeric > repeated 3`] = `"963sR"`; -exports[`string > seed: 1211 > uuid() 1`] = `"e7ec32f0-a2a3-4c65-abbd-0caabde64dfd"`; +exports[`string > 42 > alphanumeric > repeated 4`] = `"kB8HE"`; -exports[`string > seed: 1337 > alpha() 1`] = `"n"`; +exports[`string > 42 > alphanumeric > repeated 5`] = `"13YIP"`; -exports[`string > seed: 1337 > alphanumeric() 1`] = `"g"`; +exports[`string > 42 > alphanumeric > with bannedChars 1`] = `"x"`; -exports[`string > seed: 1337 > hexadecimal() > should return a deterministic hex of given length 1`] = `"0x5c346ba075bd57F5A62B82d72AF39CBBB07a98cbA8"`; +exports[`string > 42 > alphanumeric > with casing 1`] = `"d"`; -exports[`string > seed: 1337 > hexadecimal() 1`] = `"0x5"`; +exports[`string > 42 > alphanumeric > with casing 2`] = `"D"`; -exports[`string > seed: 1337 > numeric() 1`] = `"3"`; +exports[`string > 42 > alphanumeric > with casing 3`] = `"n"`; -exports[`string > seed: 1337 > sample() 1`] = `"9U/4:SK$>6"`; +exports[`string > 42 > alphanumeric > with length 1`] = `"nNWbJM"`; -exports[`string > seed: 1337 > uuid() 1`] = `"48234870-5389-445f-8b41-c61a52bf27dc"`; +exports[`string > 42 > alphanumeric > with length, casing and bannedChars 1`] = `"cvy4kvh"`; + +exports[`string > 42 > hexadecimal > noArgs 1`] = `"0x8"`; + +exports[`string > 42 > hexadecimal > with casing 1`] = `"0x8"`; + +exports[`string > 42 > hexadecimal > with casing 2`] = `"0x8"`; + +exports[`string > 42 > hexadecimal > with casing 3`] = `"0x8"`; + +exports[`string > 42 > hexadecimal > with custom prefix 1`] = `"hex_8"`; + +exports[`string > 42 > hexadecimal > with length 1`] = `"0x8BE4AB"`; + +exports[`string > 42 > hexadecimal > with length, casing and empty prefix 1`] = `"8be4abd"`; + +exports[`string > 42 > numeric > noArgs 1`] = `"4"`; + +exports[`string > 42 > numeric > repeated 1`] = `"47917"`; + +exports[`string > 42 > numeric > repeated 2`] = `"85514"`; + +exports[`string > 42 > numeric > repeated 3`] = `"20048"`; + +exports[`string > 42 > numeric > repeated 4`] = `"46176"`; + +exports[`string > 42 > numeric > repeated 5`] = `"10978"`; + +exports[`string > 42 > numeric > with bannedChars 1`] = `"7"`; + +exports[`string > 42 > numeric > with casing 1`] = `"3"`; + +exports[`string > 42 > numeric > with length 1`] = `"479177"`; + +exports[`string > 42 > numeric > with length, casing and bannedChars 1`] = `"6890887"`; + +exports[`string > 42 > sample > noArgs 1`] = `"Cky2eiXX/J"`; + +exports[`string > 42 > sample > repeated 1`] = `"Cky2e"`; + +exports[`string > 42 > sample > repeated 2`] = `"iXX/J"`; + +exports[`string > 42 > sample > repeated 3`] = `"/*&Kq"`; + +exports[`string > 42 > sample > repeated 4`] = `"@X.b]"`; + +exports[`string > 42 > sample > repeated 5`] = `"\\"&{dn"`; + +exports[`string > 42 > uuid > repeated 1`] = `"5cf2bc99-2721-407d-992b-a00fbdf302f2"`; + +exports[`string > 42 > uuid > repeated 2`] = `"94980604-8962-404f-9371-c9368f970d9a"`; + +exports[`string > 42 > uuid > repeated 3`] = `"2710fff9-c640-413a-b7a1-97d02e642ac4"`; + +exports[`string > 42 > uuid > repeated 4`] = `"6838920f-dc7f-46ee-9be5-19380f5d6b48"`; + +exports[`string > 42 > uuid > repeated 5`] = `"d95f4984-24c2-410f-ac63-400d3bbbcc91"`; + +exports[`string > 1211 > alpha > noArgs 1`] = `"W"`; + +exports[`string > 1211 > alpha > repeated 1`] = `"WxUOl"`; + +exports[`string > 1211 > alpha > repeated 2`] = `"gZbIi"`; + +exports[`string > 1211 > alpha > repeated 3`] = `"JkNvs"`; + +exports[`string > 1211 > alpha > repeated 4`] = `"iKMQa"`; + +exports[`string > 1211 > alpha > repeated 5`] = `"NIILR"`; + +exports[`string > 1211 > alpha > with bannedChars 1`] = `"X"`; + +exports[`string > 1211 > alpha > with casing 1`] = `"y"`; + +exports[`string > 1211 > alpha > with casing 2`] = `"Y"`; + +exports[`string > 1211 > alpha > with casing 3`] = `"W"`; + +exports[`string > 1211 > alpha > with length 1`] = `"WxUOlg"`; + +exports[`string > 1211 > alpha > with length, casing and bannedChars 1`] = `"yhywdcz"`; + +exports[`string > 1211 > alphanumeric > noArgs 1`] = `"V"`; + +exports[`string > 1211 > alphanumeric > repeated 1`] = `"VsTMd"`; + +exports[`string > 1211 > alphanumeric > repeated 2`] = `"8Z2F9"`; + +exports[`string > 1211 > alphanumeric > repeated 3`] = `"GdLql"`; + +exports[`string > 1211 > alphanumeric > repeated 4`] = `"aHKO0"`; + +exports[`string > 1211 > alphanumeric > repeated 5`] = `"LFEJP"`; + +exports[`string > 1211 > alphanumeric > with bannedChars 1`] = `"W"`; + +exports[`string > 1211 > alphanumeric > with casing 1`] = `"x"`; + +exports[`string > 1211 > alphanumeric > with casing 2`] = `"X"`; + +exports[`string > 1211 > alphanumeric > with casing 3`] = `"V"`; + +exports[`string > 1211 > alphanumeric > with length 1`] = `"VsTMd8"`; + +exports[`string > 1211 > alphanumeric > with length, casing and bannedChars 1`] = `"yexv53z"`; + +exports[`string > 1211 > hexadecimal > noArgs 1`] = `"0xE"`; + +exports[`string > 1211 > hexadecimal > with casing 1`] = `"0xe"`; + +exports[`string > 1211 > hexadecimal > with casing 2`] = `"0xE"`; + +exports[`string > 1211 > hexadecimal > with casing 3`] = `"0xE"`; + +exports[`string > 1211 > hexadecimal > with custom prefix 1`] = `"hex_E"`; + +exports[`string > 1211 > hexadecimal > with length 1`] = `"0xEaDB42"`; + +exports[`string > 1211 > hexadecimal > with length, casing and empty prefix 1`] = `"eadb42f"`; + +exports[`string > 1211 > numeric > noArgs 1`] = `"9"`; + +exports[`string > 1211 > numeric > repeated 1`] = `"94872"`; + +exports[`string > 1211 > numeric > repeated 2`] = `"29061"`; + +exports[`string > 1211 > numeric > repeated 3`] = `"72743"`; + +exports[`string > 1211 > numeric > repeated 4`] = `"26780"`; + +exports[`string > 1211 > numeric > repeated 5`] = `"76678"`; + +exports[`string > 1211 > numeric > with bannedChars 1`] = `"9"`; + +exports[`string > 1211 > numeric > with casing 1`] = `"9"`; + +exports[`string > 1211 > numeric > with length 1`] = `"948721"`; + +exports[`string > 1211 > numeric > with length, casing and bannedChars 1`] = `"9798609"`; + +exports[`string > 1211 > sample > noArgs 1`] = `"wKti5-}$_/"`; + +exports[`string > 1211 > sample > repeated 1`] = `"wKti5"`; + +exports[`string > 1211 > sample > repeated 2`] = `"-}$_/"`; + +exports[`string > 1211 > sample > repeated 3`] = `"\`4hHA"`; + +exports[`string > 1211 > sample > repeated 4`] = `"0afl\\""`; + +exports[`string > 1211 > sample > repeated 5`] = `"h^]dn"`; + +exports[`string > 1211 > uuid > repeated 1`] = `"e7ec32f0-a2a3-4c65-abbd-0caabde64dfd"`; + +exports[`string > 1211 > uuid > repeated 2`] = `"f379e325-9f7c-4064-a086-f23942b68e5f"`; + +exports[`string > 1211 > uuid > repeated 3`] = `"d4694649-2183-4b32-8bd7-a336639d6997"`; + +exports[`string > 1211 > uuid > repeated 4`] = `"10ab829b-742c-4a8b-b732-98d718d77069"`; + +exports[`string > 1211 > uuid > repeated 5`] = `"7b91ce88-effb-4d1d-93bb-ad759e00b86c"`; + +exports[`string > 1337 > alpha > noArgs 1`] = `"n"`; + +exports[`string > 1337 > alpha > repeated 1`] = `"nDilo"`; + +exports[`string > 1337 > alpha > repeated 2`] = `"Cxbqm"`; + +exports[`string > 1337 > alpha > repeated 3`] = `"AEnrY"`; + +exports[`string > 1337 > alpha > repeated 4`] = `"oMpfP"`; + +exports[`string > 1337 > alpha > repeated 5`] = `"ueGsg"`; + +exports[`string > 1337 > alpha > with bannedChars 1`] = `"v"`; + +exports[`string > 1337 > alpha > with casing 1`] = `"g"`; + +exports[`string > 1337 > alpha > with casing 2`] = `"G"`; + +exports[`string > 1337 > alpha > with casing 3`] = `"n"`; + +exports[`string > 1337 > alpha > with length 1`] = `"nDiloC"`; + +exports[`string > 1337 > alpha > with length, casing and bannedChars 1`] = `"eicdeih"`; + +exports[`string > 1337 > alphanumeric > noArgs 1`] = `"g"`; + +exports[`string > 1337 > alphanumeric > repeated 1`] = `"gy9dh"`; + +exports[`string > 1337 > alphanumeric > repeated 2`] = `"xs2je"`; + +exports[`string > 1337 > alphanumeric > repeated 3`] = `"wAgkY"`; + +exports[`string > 1337 > alphanumeric > repeated 4`] = `"gJj7N"`; + +exports[`string > 1337 > alphanumeric > repeated 5`] = `"n5Cm7"`; + +exports[`string > 1337 > alphanumeric > with bannedChars 1`] = `"s"`; + +exports[`string > 1337 > alphanumeric > with casing 1`] = `"9"`; + +exports[`string > 1337 > alphanumeric > with casing 2`] = `"9"`; + +exports[`string > 1337 > alphanumeric > with casing 3`] = `"g"`; + +exports[`string > 1337 > alphanumeric > with length 1`] = `"gy9dhx"`; + +exports[`string > 1337 > alphanumeric > with length, casing and bannedChars 1`] = `"ag45age"`; + +exports[`string > 1337 > hexadecimal > noArgs 1`] = `"0x5"`; + +exports[`string > 1337 > hexadecimal > with casing 1`] = `"0x5"`; + +exports[`string > 1337 > hexadecimal > with casing 2`] = `"0x5"`; + +exports[`string > 1337 > hexadecimal > with casing 3`] = `"0x5"`; + +exports[`string > 1337 > hexadecimal > with custom prefix 1`] = `"hex_5"`; + +exports[`string > 1337 > hexadecimal > with length 1`] = `"0x5c346b"`; + +exports[`string > 1337 > hexadecimal > with length, casing and empty prefix 1`] = `"5c346ba"`; + +exports[`string > 1337 > numeric > noArgs 1`] = `"3"`; + +exports[`string > 1337 > numeric > repeated 1`] = `"35122"`; + +exports[`string > 1337 > numeric > repeated 2`] = `"54032"`; + +exports[`string > 1337 > numeric > repeated 3`] = `"55239"`; + +exports[`string > 1337 > numeric > repeated 4`] = `"37318"`; + +exports[`string > 1337 > numeric > repeated 5`] = `"40631"`; + +exports[`string > 1337 > numeric > with bannedChars 1`] = `"7"`; + +exports[`string > 1337 > numeric > with casing 1`] = `"2"`; + +exports[`string > 1337 > numeric > with length 1`] = `"351225"`; + +exports[`string > 1337 > numeric > with length, casing and bannedChars 1`] = `"6706677"`; + +exports[`string > 1337 > sample > noArgs 1`] = `"9U/4:SK$>6"`; + +exports[`string > 1337 > sample > repeated 1`] = `"9U/4:"`; + +exports[`string > 1337 > sample > repeated 2`] = `"SK$>6"`; + +exports[`string > 1337 > sample > repeated 3`] = `"QX9@{"`; + +exports[`string > 1337 > sample > repeated 4`] = `":e=+k"`; + +exports[`string > 1337 > sample > repeated 5`] = `"D)[B,"`; + +exports[`string > 1337 > uuid > repeated 1`] = `"48234870-5389-445f-8b41-c61a52bf27dc"`; + +exports[`string > 1337 > uuid > repeated 2`] = `"cc057669-8c53-474d-a677-226d3e8ed92f"`; + +exports[`string > 1337 > uuid > repeated 3`] = `"fe6d8b8b-0db9-4fa2-b265-abc0a5d0ccf5"`; + +exports[`string > 1337 > uuid > repeated 4`] = `"0b87afbd-8949-4dfb-8d04-19f4fe7458b2"`; + +exports[`string > 1337 > uuid > repeated 5`] = `"0bcea83c-a7ea-428e-85db-d448f2b777a6"`; diff --git a/test/string.spec.ts b/test/string.spec.ts index 58350ba22aa..96653969773 100644 --- a/test/string.spec.ts +++ b/test/string.spec.ts @@ -1,61 +1,81 @@ import { describe, expect, it } from 'vitest'; import { faker, FakerError } from '../src'; -import type { StringModule } from '../src/modules/string'; -import { seededRuns } from './support/seededRuns'; +import { seededTests } from './support/seededRuns'; import { times } from './support/times'; const NON_SEEDED_BASED_RUN = 5; -const functionNames: (keyof StringModule)[] = [ - 'alpha', - 'alphanumeric', - 'hexadecimal', - 'numeric', - 'sample', - 'uuid', -]; - describe('string', () => { - for (const seed of seededRuns) { - describe(`seed: ${seed}`, () => { - for (const functionName of functionNames) { - it(`${functionName}()`, () => { - faker.seed(seed); - - const actual = faker.string[functionName](); - - expect(actual).toMatchSnapshot(); + seededTests(faker, 'string', (t) => { + t.describe('alpha', (t) => { + t.it('noArgs') + .itRepeated('repeated', 5, 5) + .it('with length', { length: 6 }) + .it('with casing', { casing: 'lower' }) + .it('with casing', { casing: 'upper' }) + .it('with casing', { casing: 'mixed' }) + .it('with bannedChars', { bannedChars: 'abcdefghijk' }) + .it('with length, casing and bannedChars', { + length: 7, + casing: 'lower', + bannedChars: 'lmnopqrstu', }); - } + }); - describe('hexadecimal()', () => { - it('should return a deterministic hex of given length', () => { - faker.seed(seed); + t.describe('alphanumeric', (t) => { + t.it('noArgs') + .itRepeated('repeated', 5, 5) + .it('with length', { length: 6 }) + .it('with casing', { casing: 'lower' }) + .it('with casing', { casing: 'upper' }) + .it('with casing', { casing: 'mixed' }) + .it('with bannedChars', { bannedChars: 'abcdefghijk12345' }) + .it('with length, casing and bannedChars', { + length: 7, + casing: 'lower', + bannedChars: 'lmnopqrstu67890', + }); + }); - const actual = faker.string.hexadecimal({ length: 42 }); - expect(actual).toMatchSnapshot(); + t.describe('hexadecimal', (t) => { + t.it('noArgs') + .it('with length', { length: 6 }) + .it('with casing', { casing: 'lower' }) + .it('with casing', { casing: 'upper' }) + .it('with casing', { casing: 'mixed' }) + .it('with custom prefix', { prefix: 'hex_' }) + .it('with length, casing and empty prefix', { + length: 7, + casing: 'lower', + prefix: '', }); - }); }); - describe('sample()', () => { - it('should return a deterministic string of given length', () => { - faker.seed(seed); + t.describe('numeric', (t) => { + t.it('noArgs') + .itRepeated('repeated', 5, 5) + .it('with length', { length: 6 }) + .it('with casing', { allowLeadingZeros: true }) + .it('with bannedChars', { bannedDigits: '12345' }) + .it('with length, casing and bannedChars', { + length: 7, + bannedDigits: '12345', + allowLeadingZeros: true, + }); + }); - const actual = faker.string.sample(42); - expect(actual).toMatchSnapshot(); - }); + t.describe('sample', (t) => { + t.it('noArgs').itRepeated('repeated', 5, 5); }); - } - // Create and log-back the seed for debug purposes - faker.seed(Math.ceil(Math.random() * 1_000_000_000)); + t.describe('uuid', (t) => { + t.itRepeated('repeated', 5); + }); + }); - describe(`random seeded tests for seed ${JSON.stringify( - faker.seed() - )}`, () => { + describe(`random seeded tests for seed ${faker.seed()}`, () => { for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) { - describe('alpha()', () => { + describe('alpha', () => { it('should return single letter when no length provided', () => { const actual = faker.string.alpha(); @@ -292,7 +312,7 @@ describe('string', () => { }); }); - describe(`hexadecimal()`, () => { + describe(`hexadecimal`, () => { it('generates single hex character when no additional argument was provided', () => { const hex = faker.string.hexadecimal(); expect(hex).toMatch(/^0x[0-9a-f]*$/i); @@ -425,7 +445,7 @@ describe('string', () => { }); }); - describe('sample()', () => { + describe('sample', () => { it('should generate a string value', () => { const generatedString = faker.string.sample(); expect(generatedString).toBeTypeOf('string'); @@ -452,7 +472,7 @@ describe('string', () => { }); }); - describe(`uuid()`, () => { + describe(`uuid`, () => { it('generates a valid UUID', () => { const UUID = faker.string.uuid(); const RFC4122 = From 2caf404da2f2d2e51f9bd5d2858caa1b6be4b5a7 Mon Sep 17 00:00:00 2001 From: Shinigami Date: Tue, 18 Oct 2022 08:27:55 +0200 Subject: [PATCH 42/48] chore: apply review suggestion Co-authored-by: Eric Cheng --- src/modules/random/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/random/index.ts b/src/modules/random/index.ts index d51d6273be8..7f567edf2e1 100644 --- a/src/modules/random/index.ts +++ b/src/modules/random/index.ts @@ -222,7 +222,7 @@ export class RandomModule { * @param options.casing The casing of the characters. Defaults to `'lower'`. * @param options.bannedChars An array of characters and digits which should be banned in the generated string. Defaults to `[]`. * - * @see faker.string.alphaNumeric() + * @see faker.string.alphanumeric() * * @example * faker.random.alphaNumeric() // '2' From 833f395882f944c763719c7484a58edd8485955c Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Tue, 18 Oct 2022 08:29:12 +0200 Subject: [PATCH 43/48] chore: apply review suggestion Co-authored-by: Eric Cheng --- src/modules/vehicle/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/vehicle/index.ts b/src/modules/vehicle/index.ts index 13ae45b0196..3ef8f748d32 100644 --- a/src/modules/vehicle/index.ts +++ b/src/modules/vehicle/index.ts @@ -137,7 +137,7 @@ export class VehicleModule { })}${this.faker.string.alpha({ length: 3, casing: 'upper', - })}`.toUpperCase(); + })}`; } /** From 6d163b6ea876d14f67839c4960c6d00c9fe47e95 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Tue, 18 Oct 2022 08:41:43 +0200 Subject: [PATCH 44/48] chore: reorder options --- src/modules/string/index.ts | 20 ++++++++++---------- test/string.spec.ts | 22 +++++++++++----------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/modules/string/index.ts b/src/modules/string/index.ts index b4ab331708e..a02fea0f33a 100644 --- a/src/modules/string/index.ts +++ b/src/modules/string/index.ts @@ -174,7 +174,7 @@ export class StringModule { * @example * faker.string.alphanumeric() // '2' * faker.string.alphanumeric(5) // '3e5v7' - * faker.string.alphanumeric(5, { bannedChars: ["a"] }) // 'xszlm' + * faker.string.alphanumeric({ length: 5, bannedChars: ["a"] }) // 'xszlm' * * @since 8.0.0 */ @@ -193,7 +193,7 @@ export class StringModule { }; } - const { casing = 'mixed', length = 1 } = options; + const { length = 1, casing = 'mixed' } = options; if (length <= 0) { return ''; @@ -238,8 +238,8 @@ export class StringModule { * * @param options The optional options object. * @param options.length Length of the generated number. Defaults to `1`. - * @param options.prefix Prefix for the generated number. Defaults to `'0x'`. * @param options.casing Casing of the generated number. Defaults to `'mixed'`. + * @param options.prefix Prefix for the generated number. Defaults to `'0x'`. * * @example * faker.string.hexadecimal() // '0xB' @@ -248,19 +248,19 @@ export class StringModule { * faker.string.hexadecimal({ casing: 'lower' }) // '0xf' * faker.string.hexadecimal({ length: 10, prefix: '#' }) // '#f12a974eB1' * faker.string.hexadecimal({ length: 10, casing: 'upper' }) // '0xE3F38014FB' - * faker.string.hexadecimal({ prefix: '', casing: 'lower' }) // 'd' - * faker.string.hexadecimal({ length: 10, prefix: '0x', casing: 'mixed' }) // '0xAdE330a4D1' + * faker.string.hexadecimal({ casing: 'lower', prefix: '' }) // 'd' + * faker.string.hexadecimal({ length: 10, casing: 'mixed', prefix: '0x' }) // '0xAdE330a4D1' * * @since 8.0.0 */ hexadecimal( options: { length?: number; + casing?: Casing; prefix?: string; - casing?: 'lower' | 'upper' | 'mixed'; } = {} ): string { - const { length = 1, prefix = '0x', casing = 'mixed' } = options; + const { length = 1, casing = 'mixed', prefix = '0x' } = options; let wholeString = ''; @@ -312,8 +312,8 @@ export class StringModule { * faker.string.numeric() // '2' * faker.string.numeric(5) // '31507' * faker.string.numeric(42) // '56434563150765416546479875435481513188548' - * faker.string.numeric({ allowLeadingZeros: true, length: 42 }) // '00564846278453876543517840713421451546115' - * faker.string.numeric({ bannedDigits: ['0'], length: 6 }) // '943228' + * faker.string.numeric({ length: 42, allowLeadingZeros: true }) // '00564846278453876543517840713421451546115' + * faker.string.numeric({ length: 6, bannedDigits: ['0'] }) // '943228' * * @since 8.0.0 */ @@ -332,7 +332,7 @@ export class StringModule { }; } - const { allowLeadingZeros = false, length = 1 } = options; + const { length = 1, allowLeadingZeros = false } = options; if (length <= 0) { return ''; } diff --git a/test/string.spec.ts b/test/string.spec.ts index 96653969773..c241b9bf495 100644 --- a/test/string.spec.ts +++ b/test/string.spec.ts @@ -115,8 +115,8 @@ describe('string', () => { it('should be able to ban some characters', () => { const actual = faker.string.alpha({ length: 5, - bannedChars: ['a', 'p'], casing: 'lower', + bannedChars: ['a', 'p'], }); expect(actual).toHaveLength(5); @@ -126,8 +126,8 @@ describe('string', () => { it('should be able to ban some characters via string', () => { const actual = faker.string.alpha({ length: 5, - bannedChars: 'ap', casing: 'lower', + bannedChars: 'ap', }); expect(actual).toHaveLength(5); @@ -137,8 +137,8 @@ describe('string', () => { it('should be able handle mistake in banned characters array', () => { const alphaText = faker.string.alpha({ length: 5, - bannedChars: ['a', 'a', 'p'], casing: 'lower', + bannedChars: ['a', 'a', 'p'], }); expect(alphaText).toHaveLength(5); @@ -150,8 +150,8 @@ describe('string', () => { expect(() => faker.string.alpha({ length: 5, - bannedChars, casing: 'lower', + bannedChars, }) ).toThrowError( new FakerError( @@ -211,8 +211,8 @@ describe('string', () => { const bannedChars = 'abcdefghijklmnopqrstuvwxyz'.split(''); const alphaText = faker.string.alphanumeric({ length: 5, - bannedChars, casing: 'lower', + bannedChars, }); expect(alphaText).toHaveLength(5); @@ -225,8 +225,8 @@ describe('string', () => { const bannedChars = 'abcdefghijklmnopqrstuvwxyz'; const alphaText = faker.string.alphanumeric({ length: 5, - bannedChars, casing: 'lower', + bannedChars, }); expect(alphaText).toHaveLength(5); @@ -264,8 +264,8 @@ describe('string', () => { it('should be able to handle mistake in banned characters array', () => { const alphaText = faker.string.alphanumeric({ length: 5, - bannedChars: ['a', 'p', 'a'], casing: 'lower', + bannedChars: ['a', 'p', 'a'], }); expect(alphaText).toHaveLength(5); @@ -277,8 +277,8 @@ describe('string', () => { expect(() => faker.string.alphanumeric({ length: 5, - bannedChars, casing: 'lower', + bannedChars, }) ).toThrowError( new FakerError( @@ -292,19 +292,19 @@ describe('string', () => { expect(() => faker.string.alphanumeric({ length: 5, - bannedChars, casing: 'lower', + bannedChars, }) ).toThrowError(); }); it('should not mutate the input object', () => { const input: { - bannedChars: string[]; length: number; + bannedChars: string[]; } = Object.freeze({ - bannedChars: ['a', '0', '%'], length: 5, + bannedChars: ['a', '0', '%'], }); expect(() => faker.string.alphanumeric(input)).not.toThrow(); From 42690f8c108168332a740cc971c541ab0b78421c Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Tue, 18 Oct 2022 08:49:35 +0200 Subject: [PATCH 45/48] test: apply review suggestions Co-authored-by: ST-DDT --- test/__snapshots__/string.spec.ts.snap | 252 ++++++++++++------------- test/string.spec.ts | 36 ++-- 2 files changed, 143 insertions(+), 145 deletions(-) diff --git a/test/__snapshots__/string.spec.ts.snap b/test/__snapshots__/string.spec.ts.snap index cc0c7fc31e6..77f94da26d9 100644 --- a/test/__snapshots__/string.spec.ts.snap +++ b/test/__snapshots__/string.spec.ts.snap @@ -2,59 +2,59 @@ exports[`string > 42 > alpha > noArgs 1`] = `"t"`; -exports[`string > 42 > alpha > repeated 1`] = `"tPXjM"`; +exports[`string > 42 > alpha > with bannedChars 1`] = `"A"`; -exports[`string > 42 > alpha > repeated 2`] = `"OFFix"`; +exports[`string > 42 > alpha > with casing = lower 1`] = `"j"`; -exports[`string > 42 > alpha > repeated 3`] = `"ifdxT"`; +exports[`string > 42 > alpha > with casing = mixed 1`] = `"t"`; -exports[`string > 42 > alpha > repeated 4`] = `"rFhKH"`; +exports[`string > 42 > alpha > with casing = upper 1`] = `"J"`; -exports[`string > 42 > alpha > repeated 5`] = `"bcYLR"`; +exports[`string > 42 > alpha > with length 1`] = `"tPXjMO"`; -exports[`string > 42 > alpha > with bannedChars 1`] = `"A"`; +exports[`string > 42 > alpha > with length parameter 1`] = `"tPXjM"`; -exports[`string > 42 > alpha > with casing 1`] = `"j"`; +exports[`string > 42 > alpha > with length parameter 2`] = `"OFFix"`; -exports[`string > 42 > alpha > with casing 2`] = `"J"`; +exports[`string > 42 > alpha > with length parameter 3`] = `"ifdxT"`; -exports[`string > 42 > alpha > with casing 3`] = `"t"`; +exports[`string > 42 > alpha > with length parameter 4`] = `"rFhKH"`; -exports[`string > 42 > alpha > with length 1`] = `"tPXjMO"`; +exports[`string > 42 > alpha > with length parameter 5`] = `"bcYLR"`; exports[`string > 42 > alpha > with length, casing and bannedChars 1`] = `"fwzcvwj"`; exports[`string > 42 > alphanumeric > noArgs 1`] = `"n"`; -exports[`string > 42 > alphanumeric > repeated 1`] = `"nNWbJ"`; +exports[`string > 42 > alphanumeric > with bannedChars 1`] = `"x"`; -exports[`string > 42 > alphanumeric > repeated 2`] = `"MBB9r"`; +exports[`string > 42 > alphanumeric > with casing = lower 1`] = `"d"`; -exports[`string > 42 > alphanumeric > repeated 3`] = `"963sR"`; +exports[`string > 42 > alphanumeric > with casing = mixed 1`] = `"n"`; -exports[`string > 42 > alphanumeric > repeated 4`] = `"kB8HE"`; +exports[`string > 42 > alphanumeric > with casing = upper 1`] = `"D"`; -exports[`string > 42 > alphanumeric > repeated 5`] = `"13YIP"`; +exports[`string > 42 > alphanumeric > with length 1`] = `"nNWbJM"`; -exports[`string > 42 > alphanumeric > with bannedChars 1`] = `"x"`; +exports[`string > 42 > alphanumeric > with length parameter 1`] = `"nNWbJ"`; -exports[`string > 42 > alphanumeric > with casing 1`] = `"d"`; +exports[`string > 42 > alphanumeric > with length parameter 2`] = `"MBB9r"`; -exports[`string > 42 > alphanumeric > with casing 2`] = `"D"`; +exports[`string > 42 > alphanumeric > with length parameter 3`] = `"963sR"`; -exports[`string > 42 > alphanumeric > with casing 3`] = `"n"`; +exports[`string > 42 > alphanumeric > with length parameter 4`] = `"kB8HE"`; -exports[`string > 42 > alphanumeric > with length 1`] = `"nNWbJM"`; +exports[`string > 42 > alphanumeric > with length parameter 5`] = `"13YIP"`; exports[`string > 42 > alphanumeric > with length, casing and bannedChars 1`] = `"cvy4kvh"`; exports[`string > 42 > hexadecimal > noArgs 1`] = `"0x8"`; -exports[`string > 42 > hexadecimal > with casing 1`] = `"0x8"`; +exports[`string > 42 > hexadecimal > with casing = lower 1`] = `"0x8"`; -exports[`string > 42 > hexadecimal > with casing 2`] = `"0x8"`; +exports[`string > 42 > hexadecimal > with casing = mixed 1`] = `"0x8"`; -exports[`string > 42 > hexadecimal > with casing 3`] = `"0x8"`; +exports[`string > 42 > hexadecimal > with casing = upper 1`] = `"0x8"`; exports[`string > 42 > hexadecimal > with custom prefix 1`] = `"hex_8"`; @@ -64,101 +64,101 @@ exports[`string > 42 > hexadecimal > with length, casing and empty prefix 1`] = exports[`string > 42 > numeric > noArgs 1`] = `"4"`; -exports[`string > 42 > numeric > repeated 1`] = `"47917"`; +exports[`string > 42 > numeric > with allowLeadingZeros 1`] = `"3"`; -exports[`string > 42 > numeric > repeated 2`] = `"85514"`; +exports[`string > 42 > numeric > with bannedChars 1`] = `"7"`; -exports[`string > 42 > numeric > repeated 3`] = `"20048"`; +exports[`string > 42 > numeric > with length 1`] = `"479177"`; -exports[`string > 42 > numeric > repeated 4`] = `"46176"`; +exports[`string > 42 > numeric > with length parameter 1`] = `"47917"`; -exports[`string > 42 > numeric > repeated 5`] = `"10978"`; +exports[`string > 42 > numeric > with length parameter 2`] = `"85514"`; -exports[`string > 42 > numeric > with bannedChars 1`] = `"7"`; +exports[`string > 42 > numeric > with length parameter 3`] = `"20048"`; -exports[`string > 42 > numeric > with casing 1`] = `"3"`; +exports[`string > 42 > numeric > with length parameter 4`] = `"46176"`; -exports[`string > 42 > numeric > with length 1`] = `"479177"`; +exports[`string > 42 > numeric > with length parameter 5`] = `"10978"`; -exports[`string > 42 > numeric > with length, casing and bannedChars 1`] = `"6890887"`; +exports[`string > 42 > numeric > with length, allowLeadingZeros and bannedChars 1`] = `"6890887"`; exports[`string > 42 > sample > noArgs 1`] = `"Cky2eiXX/J"`; -exports[`string > 42 > sample > repeated 1`] = `"Cky2e"`; +exports[`string > 42 > sample > with length parameter 1`] = `"Cky2e"`; -exports[`string > 42 > sample > repeated 2`] = `"iXX/J"`; +exports[`string > 42 > sample > with length parameter 2`] = `"iXX/J"`; -exports[`string > 42 > sample > repeated 3`] = `"/*&Kq"`; +exports[`string > 42 > sample > with length parameter 3`] = `"/*&Kq"`; -exports[`string > 42 > sample > repeated 4`] = `"@X.b]"`; +exports[`string > 42 > sample > with length parameter 4`] = `"@X.b]"`; -exports[`string > 42 > sample > repeated 5`] = `"\\"&{dn"`; +exports[`string > 42 > sample > with length parameter 5`] = `"\\"&{dn"`; -exports[`string > 42 > uuid > repeated 1`] = `"5cf2bc99-2721-407d-992b-a00fbdf302f2"`; +exports[`string > 42 > uuid 1`] = `"5cf2bc99-2721-407d-992b-a00fbdf302f2"`; -exports[`string > 42 > uuid > repeated 2`] = `"94980604-8962-404f-9371-c9368f970d9a"`; +exports[`string > 42 > uuid 2`] = `"94980604-8962-404f-9371-c9368f970d9a"`; -exports[`string > 42 > uuid > repeated 3`] = `"2710fff9-c640-413a-b7a1-97d02e642ac4"`; +exports[`string > 42 > uuid 3`] = `"2710fff9-c640-413a-b7a1-97d02e642ac4"`; -exports[`string > 42 > uuid > repeated 4`] = `"6838920f-dc7f-46ee-9be5-19380f5d6b48"`; +exports[`string > 42 > uuid 4`] = `"6838920f-dc7f-46ee-9be5-19380f5d6b48"`; -exports[`string > 42 > uuid > repeated 5`] = `"d95f4984-24c2-410f-ac63-400d3bbbcc91"`; +exports[`string > 42 > uuid 5`] = `"d95f4984-24c2-410f-ac63-400d3bbbcc91"`; exports[`string > 1211 > alpha > noArgs 1`] = `"W"`; -exports[`string > 1211 > alpha > repeated 1`] = `"WxUOl"`; +exports[`string > 1211 > alpha > with bannedChars 1`] = `"X"`; -exports[`string > 1211 > alpha > repeated 2`] = `"gZbIi"`; +exports[`string > 1211 > alpha > with casing = lower 1`] = `"y"`; -exports[`string > 1211 > alpha > repeated 3`] = `"JkNvs"`; +exports[`string > 1211 > alpha > with casing = mixed 1`] = `"W"`; -exports[`string > 1211 > alpha > repeated 4`] = `"iKMQa"`; +exports[`string > 1211 > alpha > with casing = upper 1`] = `"Y"`; -exports[`string > 1211 > alpha > repeated 5`] = `"NIILR"`; +exports[`string > 1211 > alpha > with length 1`] = `"WxUOlg"`; -exports[`string > 1211 > alpha > with bannedChars 1`] = `"X"`; +exports[`string > 1211 > alpha > with length parameter 1`] = `"WxUOl"`; -exports[`string > 1211 > alpha > with casing 1`] = `"y"`; +exports[`string > 1211 > alpha > with length parameter 2`] = `"gZbIi"`; -exports[`string > 1211 > alpha > with casing 2`] = `"Y"`; +exports[`string > 1211 > alpha > with length parameter 3`] = `"JkNvs"`; -exports[`string > 1211 > alpha > with casing 3`] = `"W"`; +exports[`string > 1211 > alpha > with length parameter 4`] = `"iKMQa"`; -exports[`string > 1211 > alpha > with length 1`] = `"WxUOlg"`; +exports[`string > 1211 > alpha > with length parameter 5`] = `"NIILR"`; exports[`string > 1211 > alpha > with length, casing and bannedChars 1`] = `"yhywdcz"`; exports[`string > 1211 > alphanumeric > noArgs 1`] = `"V"`; -exports[`string > 1211 > alphanumeric > repeated 1`] = `"VsTMd"`; +exports[`string > 1211 > alphanumeric > with bannedChars 1`] = `"W"`; -exports[`string > 1211 > alphanumeric > repeated 2`] = `"8Z2F9"`; +exports[`string > 1211 > alphanumeric > with casing = lower 1`] = `"x"`; -exports[`string > 1211 > alphanumeric > repeated 3`] = `"GdLql"`; +exports[`string > 1211 > alphanumeric > with casing = mixed 1`] = `"V"`; -exports[`string > 1211 > alphanumeric > repeated 4`] = `"aHKO0"`; +exports[`string > 1211 > alphanumeric > with casing = upper 1`] = `"X"`; -exports[`string > 1211 > alphanumeric > repeated 5`] = `"LFEJP"`; +exports[`string > 1211 > alphanumeric > with length 1`] = `"VsTMd8"`; -exports[`string > 1211 > alphanumeric > with bannedChars 1`] = `"W"`; +exports[`string > 1211 > alphanumeric > with length parameter 1`] = `"VsTMd"`; -exports[`string > 1211 > alphanumeric > with casing 1`] = `"x"`; +exports[`string > 1211 > alphanumeric > with length parameter 2`] = `"8Z2F9"`; -exports[`string > 1211 > alphanumeric > with casing 2`] = `"X"`; +exports[`string > 1211 > alphanumeric > with length parameter 3`] = `"GdLql"`; -exports[`string > 1211 > alphanumeric > with casing 3`] = `"V"`; +exports[`string > 1211 > alphanumeric > with length parameter 4`] = `"aHKO0"`; -exports[`string > 1211 > alphanumeric > with length 1`] = `"VsTMd8"`; +exports[`string > 1211 > alphanumeric > with length parameter 5`] = `"LFEJP"`; exports[`string > 1211 > alphanumeric > with length, casing and bannedChars 1`] = `"yexv53z"`; exports[`string > 1211 > hexadecimal > noArgs 1`] = `"0xE"`; -exports[`string > 1211 > hexadecimal > with casing 1`] = `"0xe"`; +exports[`string > 1211 > hexadecimal > with casing = lower 1`] = `"0xe"`; -exports[`string > 1211 > hexadecimal > with casing 2`] = `"0xE"`; +exports[`string > 1211 > hexadecimal > with casing = mixed 1`] = `"0xE"`; -exports[`string > 1211 > hexadecimal > with casing 3`] = `"0xE"`; +exports[`string > 1211 > hexadecimal > with casing = upper 1`] = `"0xE"`; exports[`string > 1211 > hexadecimal > with custom prefix 1`] = `"hex_E"`; @@ -168,101 +168,101 @@ exports[`string > 1211 > hexadecimal > with length, casing and empty prefix 1`] exports[`string > 1211 > numeric > noArgs 1`] = `"9"`; -exports[`string > 1211 > numeric > repeated 1`] = `"94872"`; +exports[`string > 1211 > numeric > with allowLeadingZeros 1`] = `"9"`; -exports[`string > 1211 > numeric > repeated 2`] = `"29061"`; +exports[`string > 1211 > numeric > with bannedChars 1`] = `"9"`; -exports[`string > 1211 > numeric > repeated 3`] = `"72743"`; +exports[`string > 1211 > numeric > with length 1`] = `"948721"`; -exports[`string > 1211 > numeric > repeated 4`] = `"26780"`; +exports[`string > 1211 > numeric > with length parameter 1`] = `"94872"`; -exports[`string > 1211 > numeric > repeated 5`] = `"76678"`; +exports[`string > 1211 > numeric > with length parameter 2`] = `"29061"`; -exports[`string > 1211 > numeric > with bannedChars 1`] = `"9"`; +exports[`string > 1211 > numeric > with length parameter 3`] = `"72743"`; -exports[`string > 1211 > numeric > with casing 1`] = `"9"`; +exports[`string > 1211 > numeric > with length parameter 4`] = `"26780"`; -exports[`string > 1211 > numeric > with length 1`] = `"948721"`; +exports[`string > 1211 > numeric > with length parameter 5`] = `"76678"`; -exports[`string > 1211 > numeric > with length, casing and bannedChars 1`] = `"9798609"`; +exports[`string > 1211 > numeric > with length, allowLeadingZeros and bannedChars 1`] = `"9798609"`; exports[`string > 1211 > sample > noArgs 1`] = `"wKti5-}$_/"`; -exports[`string > 1211 > sample > repeated 1`] = `"wKti5"`; +exports[`string > 1211 > sample > with length parameter 1`] = `"wKti5"`; -exports[`string > 1211 > sample > repeated 2`] = `"-}$_/"`; +exports[`string > 1211 > sample > with length parameter 2`] = `"-}$_/"`; -exports[`string > 1211 > sample > repeated 3`] = `"\`4hHA"`; +exports[`string > 1211 > sample > with length parameter 3`] = `"\`4hHA"`; -exports[`string > 1211 > sample > repeated 4`] = `"0afl\\""`; +exports[`string > 1211 > sample > with length parameter 4`] = `"0afl\\""`; -exports[`string > 1211 > sample > repeated 5`] = `"h^]dn"`; +exports[`string > 1211 > sample > with length parameter 5`] = `"h^]dn"`; -exports[`string > 1211 > uuid > repeated 1`] = `"e7ec32f0-a2a3-4c65-abbd-0caabde64dfd"`; +exports[`string > 1211 > uuid 1`] = `"e7ec32f0-a2a3-4c65-abbd-0caabde64dfd"`; -exports[`string > 1211 > uuid > repeated 2`] = `"f379e325-9f7c-4064-a086-f23942b68e5f"`; +exports[`string > 1211 > uuid 2`] = `"f379e325-9f7c-4064-a086-f23942b68e5f"`; -exports[`string > 1211 > uuid > repeated 3`] = `"d4694649-2183-4b32-8bd7-a336639d6997"`; +exports[`string > 1211 > uuid 3`] = `"d4694649-2183-4b32-8bd7-a336639d6997"`; -exports[`string > 1211 > uuid > repeated 4`] = `"10ab829b-742c-4a8b-b732-98d718d77069"`; +exports[`string > 1211 > uuid 4`] = `"10ab829b-742c-4a8b-b732-98d718d77069"`; -exports[`string > 1211 > uuid > repeated 5`] = `"7b91ce88-effb-4d1d-93bb-ad759e00b86c"`; +exports[`string > 1211 > uuid 5`] = `"7b91ce88-effb-4d1d-93bb-ad759e00b86c"`; exports[`string > 1337 > alpha > noArgs 1`] = `"n"`; -exports[`string > 1337 > alpha > repeated 1`] = `"nDilo"`; +exports[`string > 1337 > alpha > with bannedChars 1`] = `"v"`; -exports[`string > 1337 > alpha > repeated 2`] = `"Cxbqm"`; +exports[`string > 1337 > alpha > with casing = lower 1`] = `"g"`; -exports[`string > 1337 > alpha > repeated 3`] = `"AEnrY"`; +exports[`string > 1337 > alpha > with casing = mixed 1`] = `"n"`; -exports[`string > 1337 > alpha > repeated 4`] = `"oMpfP"`; +exports[`string > 1337 > alpha > with casing = upper 1`] = `"G"`; -exports[`string > 1337 > alpha > repeated 5`] = `"ueGsg"`; +exports[`string > 1337 > alpha > with length 1`] = `"nDiloC"`; -exports[`string > 1337 > alpha > with bannedChars 1`] = `"v"`; +exports[`string > 1337 > alpha > with length parameter 1`] = `"nDilo"`; -exports[`string > 1337 > alpha > with casing 1`] = `"g"`; +exports[`string > 1337 > alpha > with length parameter 2`] = `"Cxbqm"`; -exports[`string > 1337 > alpha > with casing 2`] = `"G"`; +exports[`string > 1337 > alpha > with length parameter 3`] = `"AEnrY"`; -exports[`string > 1337 > alpha > with casing 3`] = `"n"`; +exports[`string > 1337 > alpha > with length parameter 4`] = `"oMpfP"`; -exports[`string > 1337 > alpha > with length 1`] = `"nDiloC"`; +exports[`string > 1337 > alpha > with length parameter 5`] = `"ueGsg"`; exports[`string > 1337 > alpha > with length, casing and bannedChars 1`] = `"eicdeih"`; exports[`string > 1337 > alphanumeric > noArgs 1`] = `"g"`; -exports[`string > 1337 > alphanumeric > repeated 1`] = `"gy9dh"`; +exports[`string > 1337 > alphanumeric > with bannedChars 1`] = `"s"`; -exports[`string > 1337 > alphanumeric > repeated 2`] = `"xs2je"`; +exports[`string > 1337 > alphanumeric > with casing = lower 1`] = `"9"`; -exports[`string > 1337 > alphanumeric > repeated 3`] = `"wAgkY"`; +exports[`string > 1337 > alphanumeric > with casing = mixed 1`] = `"g"`; -exports[`string > 1337 > alphanumeric > repeated 4`] = `"gJj7N"`; +exports[`string > 1337 > alphanumeric > with casing = upper 1`] = `"9"`; -exports[`string > 1337 > alphanumeric > repeated 5`] = `"n5Cm7"`; +exports[`string > 1337 > alphanumeric > with length 1`] = `"gy9dhx"`; -exports[`string > 1337 > alphanumeric > with bannedChars 1`] = `"s"`; +exports[`string > 1337 > alphanumeric > with length parameter 1`] = `"gy9dh"`; -exports[`string > 1337 > alphanumeric > with casing 1`] = `"9"`; +exports[`string > 1337 > alphanumeric > with length parameter 2`] = `"xs2je"`; -exports[`string > 1337 > alphanumeric > with casing 2`] = `"9"`; +exports[`string > 1337 > alphanumeric > with length parameter 3`] = `"wAgkY"`; -exports[`string > 1337 > alphanumeric > with casing 3`] = `"g"`; +exports[`string > 1337 > alphanumeric > with length parameter 4`] = `"gJj7N"`; -exports[`string > 1337 > alphanumeric > with length 1`] = `"gy9dhx"`; +exports[`string > 1337 > alphanumeric > with length parameter 5`] = `"n5Cm7"`; exports[`string > 1337 > alphanumeric > with length, casing and bannedChars 1`] = `"ag45age"`; exports[`string > 1337 > hexadecimal > noArgs 1`] = `"0x5"`; -exports[`string > 1337 > hexadecimal > with casing 1`] = `"0x5"`; +exports[`string > 1337 > hexadecimal > with casing = lower 1`] = `"0x5"`; -exports[`string > 1337 > hexadecimal > with casing 2`] = `"0x5"`; +exports[`string > 1337 > hexadecimal > with casing = mixed 1`] = `"0x5"`; -exports[`string > 1337 > hexadecimal > with casing 3`] = `"0x5"`; +exports[`string > 1337 > hexadecimal > with casing = upper 1`] = `"0x5"`; exports[`string > 1337 > hexadecimal > with custom prefix 1`] = `"hex_5"`; @@ -272,42 +272,42 @@ exports[`string > 1337 > hexadecimal > with length, casing and empty prefix 1`] exports[`string > 1337 > numeric > noArgs 1`] = `"3"`; -exports[`string > 1337 > numeric > repeated 1`] = `"35122"`; +exports[`string > 1337 > numeric > with allowLeadingZeros 1`] = `"2"`; -exports[`string > 1337 > numeric > repeated 2`] = `"54032"`; +exports[`string > 1337 > numeric > with bannedChars 1`] = `"7"`; -exports[`string > 1337 > numeric > repeated 3`] = `"55239"`; +exports[`string > 1337 > numeric > with length 1`] = `"351225"`; -exports[`string > 1337 > numeric > repeated 4`] = `"37318"`; +exports[`string > 1337 > numeric > with length parameter 1`] = `"35122"`; -exports[`string > 1337 > numeric > repeated 5`] = `"40631"`; +exports[`string > 1337 > numeric > with length parameter 2`] = `"54032"`; -exports[`string > 1337 > numeric > with bannedChars 1`] = `"7"`; +exports[`string > 1337 > numeric > with length parameter 3`] = `"55239"`; -exports[`string > 1337 > numeric > with casing 1`] = `"2"`; +exports[`string > 1337 > numeric > with length parameter 4`] = `"37318"`; -exports[`string > 1337 > numeric > with length 1`] = `"351225"`; +exports[`string > 1337 > numeric > with length parameter 5`] = `"40631"`; -exports[`string > 1337 > numeric > with length, casing and bannedChars 1`] = `"6706677"`; +exports[`string > 1337 > numeric > with length, allowLeadingZeros and bannedChars 1`] = `"6706677"`; exports[`string > 1337 > sample > noArgs 1`] = `"9U/4:SK$>6"`; -exports[`string > 1337 > sample > repeated 1`] = `"9U/4:"`; +exports[`string > 1337 > sample > with length parameter 1`] = `"9U/4:"`; -exports[`string > 1337 > sample > repeated 2`] = `"SK$>6"`; +exports[`string > 1337 > sample > with length parameter 2`] = `"SK$>6"`; -exports[`string > 1337 > sample > repeated 3`] = `"QX9@{"`; +exports[`string > 1337 > sample > with length parameter 3`] = `"QX9@{"`; -exports[`string > 1337 > sample > repeated 4`] = `":e=+k"`; +exports[`string > 1337 > sample > with length parameter 4`] = `":e=+k"`; -exports[`string > 1337 > sample > repeated 5`] = `"D)[B,"`; +exports[`string > 1337 > sample > with length parameter 5`] = `"D)[B,"`; -exports[`string > 1337 > uuid > repeated 1`] = `"48234870-5389-445f-8b41-c61a52bf27dc"`; +exports[`string > 1337 > uuid 1`] = `"48234870-5389-445f-8b41-c61a52bf27dc"`; -exports[`string > 1337 > uuid > repeated 2`] = `"cc057669-8c53-474d-a677-226d3e8ed92f"`; +exports[`string > 1337 > uuid 2`] = `"cc057669-8c53-474d-a677-226d3e8ed92f"`; -exports[`string > 1337 > uuid > repeated 3`] = `"fe6d8b8b-0db9-4fa2-b265-abc0a5d0ccf5"`; +exports[`string > 1337 > uuid 3`] = `"fe6d8b8b-0db9-4fa2-b265-abc0a5d0ccf5"`; -exports[`string > 1337 > uuid > repeated 4`] = `"0b87afbd-8949-4dfb-8d04-19f4fe7458b2"`; +exports[`string > 1337 > uuid 4`] = `"0b87afbd-8949-4dfb-8d04-19f4fe7458b2"`; -exports[`string > 1337 > uuid > repeated 5`] = `"0bcea83c-a7ea-428e-85db-d448f2b777a6"`; +exports[`string > 1337 > uuid 5`] = `"0bcea83c-a7ea-428e-85db-d448f2b777a6"`; diff --git a/test/string.spec.ts b/test/string.spec.ts index c241b9bf495..48a25eaf59c 100644 --- a/test/string.spec.ts +++ b/test/string.spec.ts @@ -9,11 +9,11 @@ describe('string', () => { seededTests(faker, 'string', (t) => { t.describe('alpha', (t) => { t.it('noArgs') - .itRepeated('repeated', 5, 5) + .itRepeated('with length parameter', 5, 5) .it('with length', { length: 6 }) - .it('with casing', { casing: 'lower' }) - .it('with casing', { casing: 'upper' }) - .it('with casing', { casing: 'mixed' }) + .it('with casing = lower', { casing: 'lower' }) + .it('with casing = upper', { casing: 'upper' }) + .it('with casing = mixed', { casing: 'mixed' }) .it('with bannedChars', { bannedChars: 'abcdefghijk' }) .it('with length, casing and bannedChars', { length: 7, @@ -24,11 +24,11 @@ describe('string', () => { t.describe('alphanumeric', (t) => { t.it('noArgs') - .itRepeated('repeated', 5, 5) + .itRepeated('with length parameter', 5, 5) .it('with length', { length: 6 }) - .it('with casing', { casing: 'lower' }) - .it('with casing', { casing: 'upper' }) - .it('with casing', { casing: 'mixed' }) + .it('with casing = lower', { casing: 'lower' }) + .it('with casing = upper', { casing: 'upper' }) + .it('with casing = mixed', { casing: 'mixed' }) .it('with bannedChars', { bannedChars: 'abcdefghijk12345' }) .it('with length, casing and bannedChars', { length: 7, @@ -40,9 +40,9 @@ describe('string', () => { t.describe('hexadecimal', (t) => { t.it('noArgs') .it('with length', { length: 6 }) - .it('with casing', { casing: 'lower' }) - .it('with casing', { casing: 'upper' }) - .it('with casing', { casing: 'mixed' }) + .it('with casing = lower', { casing: 'lower' }) + .it('with casing = upper', { casing: 'upper' }) + .it('with casing = mixed', { casing: 'mixed' }) .it('with custom prefix', { prefix: 'hex_' }) .it('with length, casing and empty prefix', { length: 7, @@ -53,24 +53,22 @@ describe('string', () => { t.describe('numeric', (t) => { t.it('noArgs') - .itRepeated('repeated', 5, 5) + .itRepeated('with length parameter', 5, 5) .it('with length', { length: 6 }) - .it('with casing', { allowLeadingZeros: true }) + .it('with allowLeadingZeros', { allowLeadingZeros: true }) .it('with bannedChars', { bannedDigits: '12345' }) - .it('with length, casing and bannedChars', { + .it('with length, allowLeadingZeros and bannedChars', { length: 7, - bannedDigits: '12345', allowLeadingZeros: true, + bannedDigits: '12345', }); }); t.describe('sample', (t) => { - t.it('noArgs').itRepeated('repeated', 5, 5); + t.it('noArgs').itRepeated('with length parameter', 5, 5); }); - t.describe('uuid', (t) => { - t.itRepeated('repeated', 5); - }); + t.itRepeated('uuid', 5); }); describe(`random seeded tests for seed ${faker.seed()}`, () => { From c5deddd46e3698a059848486c199f0aa1bdaead3 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Tue, 18 Oct 2022 08:51:35 +0200 Subject: [PATCH 46/48] test: remove unnecessary casing --- test/random.spec.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/test/random.spec.ts b/test/random.spec.ts index 2713880af6d..2cdfd2c033c 100644 --- a/test/random.spec.ts +++ b/test/random.spec.ts @@ -194,7 +194,6 @@ describe('random', () => { faker.random.alpha({ count: 5, bannedChars, - casing: 'lower', }) ).toThrowError( new FakerError( From dc4255f8ba531de98cdcb8a9f18e7248d06bfe8e Mon Sep 17 00:00:00 2001 From: Shinigami Date: Tue, 18 Oct 2022 14:40:51 +0200 Subject: [PATCH 47/48] Update test/string.spec.ts Co-authored-by: Eric Cheng --- test/string.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/string.spec.ts b/test/string.spec.ts index 48a25eaf59c..66c6bc59dca 100644 --- a/test/string.spec.ts +++ b/test/string.spec.ts @@ -56,8 +56,8 @@ describe('string', () => { .itRepeated('with length parameter', 5, 5) .it('with length', { length: 6 }) .it('with allowLeadingZeros', { allowLeadingZeros: true }) - .it('with bannedChars', { bannedDigits: '12345' }) - .it('with length, allowLeadingZeros and bannedChars', { + .it('with bannedDigits', { bannedDigits: '12345' }) + .it('with length, allowLeadingZeros and bannedDigits', { length: 7, allowLeadingZeros: true, bannedDigits: '12345', From f5e25f88bd186e49878bd1615c58e9bc9e7c89bc Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Tue, 18 Oct 2022 14:49:30 +0200 Subject: [PATCH 48/48] test: update snapshots --- test/__snapshots__/string.spec.ts.snap | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/__snapshots__/string.spec.ts.snap b/test/__snapshots__/string.spec.ts.snap index 77f94da26d9..f4a607b4888 100644 --- a/test/__snapshots__/string.spec.ts.snap +++ b/test/__snapshots__/string.spec.ts.snap @@ -66,7 +66,7 @@ exports[`string > 42 > numeric > noArgs 1`] = `"4"`; exports[`string > 42 > numeric > with allowLeadingZeros 1`] = `"3"`; -exports[`string > 42 > numeric > with bannedChars 1`] = `"7"`; +exports[`string > 42 > numeric > with bannedDigits 1`] = `"7"`; exports[`string > 42 > numeric > with length 1`] = `"479177"`; @@ -80,7 +80,7 @@ exports[`string > 42 > numeric > with length parameter 4`] = `"46176"`; exports[`string > 42 > numeric > with length parameter 5`] = `"10978"`; -exports[`string > 42 > numeric > with length, allowLeadingZeros and bannedChars 1`] = `"6890887"`; +exports[`string > 42 > numeric > with length, allowLeadingZeros and bannedDigits 1`] = `"6890887"`; exports[`string > 42 > sample > noArgs 1`] = `"Cky2eiXX/J"`; @@ -170,7 +170,7 @@ exports[`string > 1211 > numeric > noArgs 1`] = `"9"`; exports[`string > 1211 > numeric > with allowLeadingZeros 1`] = `"9"`; -exports[`string > 1211 > numeric > with bannedChars 1`] = `"9"`; +exports[`string > 1211 > numeric > with bannedDigits 1`] = `"9"`; exports[`string > 1211 > numeric > with length 1`] = `"948721"`; @@ -184,7 +184,7 @@ exports[`string > 1211 > numeric > with length parameter 4`] = `"26780"`; exports[`string > 1211 > numeric > with length parameter 5`] = `"76678"`; -exports[`string > 1211 > numeric > with length, allowLeadingZeros and bannedChars 1`] = `"9798609"`; +exports[`string > 1211 > numeric > with length, allowLeadingZeros and bannedDigits 1`] = `"9798609"`; exports[`string > 1211 > sample > noArgs 1`] = `"wKti5-}$_/"`; @@ -274,7 +274,7 @@ exports[`string > 1337 > numeric > noArgs 1`] = `"3"`; exports[`string > 1337 > numeric > with allowLeadingZeros 1`] = `"2"`; -exports[`string > 1337 > numeric > with bannedChars 1`] = `"7"`; +exports[`string > 1337 > numeric > with bannedDigits 1`] = `"7"`; exports[`string > 1337 > numeric > with length 1`] = `"351225"`; @@ -288,7 +288,7 @@ exports[`string > 1337 > numeric > with length parameter 4`] = `"37318"`; exports[`string > 1337 > numeric > with length parameter 5`] = `"40631"`; -exports[`string > 1337 > numeric > with length, allowLeadingZeros and bannedChars 1`] = `"6706677"`; +exports[`string > 1337 > numeric > with length, allowLeadingZeros and bannedDigits 1`] = `"6706677"`; exports[`string > 1337 > sample > noArgs 1`] = `"9U/4:SK$>6"`;