From 8c6f7e3ff8eeeba649343a45110d4dcf61929b81 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Thu, 13 Jan 2022 15:33:59 +0100 Subject: [PATCH] feat: migrate phone --- src/index.ts | 3 ++- src/phone_number.ts | 50 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 src/phone_number.ts diff --git a/src/index.ts b/src/index.ts index b5cdd3c49b4..965b38e94c5 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 00000000000..1db85eae446 --- /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 + ); + } +}