diff options
Diffstat (limited to 'src/modules')
| -rw-r--r-- | src/modules/phone/index.ts | 32 |
1 files changed, 28 insertions, 4 deletions
diff --git a/src/modules/phone/index.ts b/src/modules/phone/index.ts index 44f2a111..0d670f58 100644 --- a/src/modules/phone/index.ts +++ b/src/modules/phone/index.ts @@ -12,18 +12,42 @@ export class PhoneModule extends ModuleBase { /** * Generates a random phone number. * + * @param options Options object + * @param options.style Style of the phone number. Defaults to `'human'`. + * * @see faker.string.numeric(): For generating a random string of numbers. * @see faker.helpers.fromRegExp(): For generating a phone number matching a regular expression. * * @example * faker.phone.number() // '961-770-7727' + * faker.phone.number({ style: 'human' }) // '555.770.7727 x1234' + * faker.phone.number({ style: 'national' }) // '(961) 770-7727' + * faker.phone.number({ style: 'international' }) // '+15551234567' * * @since 7.3.0 */ - number(): string { - const format = this.faker.helpers.arrayElement( - this.faker.definitions.phone_number.formats - ); + number( + options: { + /** + * Style of the generated phone number: + * - `'human'`: (default) A human-input phone number, e.g. `555-770-7727` or `555.770.7727 x1234` + * - `'national'`: A phone number in a standardized national format, e.g. `(555) 123-4567`. + * - `'international'`: A phone number in the E.123 international format, e.g. `+15551234567` + * + * @default 'human' + */ + style?: 'human' | 'national' | 'international'; + } = {} + ): string { + const { style = 'human' } = options; + const formats = this.faker.definitions.phone_number.format; + + const definitions = formats[style]; + if (!definitions) { + throw new Error(`No definitions for ${style} in this locale`); + } + + const format = this.faker.helpers.arrayElement(definitions); return legacyReplaceSymbolWithNumber(this.faker, format); } |
