aboutsummaryrefslogtreecommitdiff
path: root/src/modules/phone/index.ts
blob: 0d670f58e9eb081cc674ed4f20b5cd1f7b887293 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { ModuleBase } from '../../internal/module-base';
import { legacyReplaceSymbolWithNumber } from '../helpers';

/**
 * Module to generate phone-related data.
 *
 * ### Overview
 *
 * For a phone number, use [`number()`](https://fakerjs.dev/api/phone.html#number). Many locales provide country-specific formats.
 */
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(
    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);
  }

  /**
   * Generates IMEI number.
   *
   * @example
   * faker.phone.imei() // '13-850175-913761-7'
   *
   * @since 6.2.0
   */
  imei(): string {
    return this.faker.helpers.replaceCreditCardSymbols(
      '##-######-######-L',
      '#'
    );
  }
}