diff options
| author | Shinigami <[email protected]> | 2022-01-14 17:43:29 +0100 |
|---|---|---|
| committer | Damien Retzinger <[email protected]> | 2022-01-14 18:37:49 -0500 |
| commit | 77f4e63c4bfac731b813f7577a6e257548e20dd9 (patch) | |
| tree | b42fb1bce86a0ac2f5a98c73d286dbf24d3f00b6 /src | |
| parent | 82ab145286909d49a798c95bf46ea504ebdd7be7 (diff) | |
| download | faker-77f4e63c4bfac731b813f7577a6e257548e20dd9.tar.xz faker-77f4e63c4bfac731b813f7577a6e257548e20dd9.zip | |
feat: migrate phone (#127)
Diffstat (limited to 'src')
| -rw-r--r-- | src/index.ts | 3 | ||||
| -rw-r--r-- | src/phone_number.ts | 50 |
2 files changed, 52 insertions, 1 deletions
diff --git a/src/index.ts b/src/index.ts index b5cdd3c4..965b38e9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,6 +11,7 @@ import { Image } from './image'; import { Internet } from './internet'; import { Mersenne } from './mersenne'; import { Name } from './name'; +import { Phone } from './phone_number'; import { Random } from './random'; import { System } from './system'; import { Time } from './time'; @@ -191,7 +192,7 @@ export class Faker { readonly lorem = new (require('./lorem'))(this); readonly music = new (require('./music'))(this); readonly name: Name = new Name(this); - readonly phone = new (require('./phone_number'))(this); + readonly phone: Phone = new Phone(this); readonly system: System = new System(this); readonly time: Time = new Time(); readonly vehicle = new (require('./vehicle'))(this); diff --git a/src/phone_number.ts b/src/phone_number.ts new file mode 100644 index 00000000..1db85eae --- /dev/null +++ b/src/phone_number.ts @@ -0,0 +1,50 @@ +import type { Faker } from '.'; + +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); + } + } + + /** + * phoneNumber + * + * @method faker.phone.phoneNumber + * @param format + * @memberOf faker.phone + */ + phoneNumber(format?: string) { + format ||= this.faker.phone.phoneFormats(); + return this.faker.helpers.replaceSymbolWithNumber(format); + } + + // FIXME: this is strange passing in an array index. + /** + * phoneNumberFormat + * + * @method faker.phone.phoneFormatsArrayIndex + * @param phoneFormatsArrayIndex + * @memberOf faker.phone + */ + phoneNumberFormat(phoneFormatsArrayIndex: number = 0) { + return this.faker.helpers.replaceSymbolWithNumber( + this.faker.definitions.phone_number.formats[phoneFormatsArrayIndex] + ); + } + + /** + * phoneFormats + * + * @method faker.phone.phoneFormats + */ + phoneFormats(): string { + return this.faker.random.arrayElement( + this.faker.definitions.phone_number.formats + ); + } +} |
