-
-
Notifications
You must be signed in to change notification settings - Fork 943
/
Copy pathphone.ts
63 lines (59 loc) · 1.98 KB
/
phone.ts
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
import type { Faker } from '.';
/**
* Module to generate phone numbers.
*/
export class Phone {
constructor(private readonly faker: Faker) {
// Bind `this` so namespaced is working correctly
for (const name of Object.getOwnPropertyNames(Phone.prototype)) {
if (name === 'constructor' || typeof this[name] !== 'function') {
continue;
}
this[name] = this[name].bind(this);
}
}
/**
* Generates a random phone number.
*
* @param format Format of the phone number. Defaults to `faker.phone.phoneFormats()`.
*
* @example
* faker.phone.phoneNumber() // '961-770-7727'
* faker.phone.phoneNumber('501-###-###') // '501-039-841'
* faker.phone.phoneNumber('+48 91 ### ## ##') // '+48 91 463 61 70'
*/
// TODO @pkuczynski 2022-02-01: simplify name to `number()`
phoneNumber(format?: string): string {
return this.faker.helpers.replaceSymbolWithNumber(
format || this.phoneFormats()
);
}
/**
* Returns phone number in a format of the given index from `faker.definitions.phone_number.formats`.
*
* @param phoneFormatsArrayIndex Index in the `faker.definitions.phone_number.formats` array. Defaults to `0`.
*
* @example
* faker.phone.phoneNumberFormat() // '943-627-0355'
* faker.phone.phoneNumberFormat(3) // '282.652.3201'
*/
// FIXME @Shinigami 2022-01-14: this is strange passing in an array index
// TODO @pkuczynski 2022-02-01: discuss removing this method as it tightly couples with localisation
phoneNumberFormat(phoneFormatsArrayIndex = 0): string {
return this.faker.helpers.replaceSymbolWithNumber(
this.faker.definitions.phone_number.formats[phoneFormatsArrayIndex]
);
}
/**
* Returns a random phone number format.
*
* @example
* faker.phone.phoneFormats() // '!##.!##.####'
*/
// TODO @pkuczynski 2022-02-01: simplify name to `format()`
phoneFormats(): string {
return this.faker.random.arrayElement(
this.faker.definitions.phone_number.formats
);
}
}