Skip to content

Commit

Permalink
feat: migrate phone (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 authored and damienwebdev committed Jan 14, 2022
1 parent 82ab145 commit 77f4e63
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/index.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down
50 changes: 50 additions & 0 deletions 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
);
}
}

0 comments on commit 77f4e63

Please sign in to comment.