Skip to content

Commit 77f4e63

Browse files
Shinigami92damienwebdev
authored andcommitted
feat: migrate phone (#127)
1 parent 82ab145 commit 77f4e63

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { Image } from './image';
1111
import { Internet } from './internet';
1212
import { Mersenne } from './mersenne';
1313
import { Name } from './name';
14+
import { Phone } from './phone_number';
1415
import { Random } from './random';
1516
import { System } from './system';
1617
import { Time } from './time';
@@ -191,7 +192,7 @@ export class Faker {
191192
readonly lorem = new (require('./lorem'))(this);
192193
readonly music = new (require('./music'))(this);
193194
readonly name: Name = new Name(this);
194-
readonly phone = new (require('./phone_number'))(this);
195+
readonly phone: Phone = new Phone(this);
195196
readonly system: System = new System(this);
196197
readonly time: Time = new Time();
197198
readonly vehicle = new (require('./vehicle'))(this);

src/phone_number.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import type { Faker } from '.';
2+
3+
export class Phone {
4+
constructor(private readonly faker: Faker) {
5+
// Bind `this` so namespaced is working correctly
6+
for (const name of Object.getOwnPropertyNames(Phone.prototype)) {
7+
if (name === 'constructor' || typeof this[name] !== 'function') {
8+
continue;
9+
}
10+
this[name] = this[name].bind(this);
11+
}
12+
}
13+
14+
/**
15+
* phoneNumber
16+
*
17+
* @method faker.phone.phoneNumber
18+
* @param format
19+
* @memberOf faker.phone
20+
*/
21+
phoneNumber(format?: string) {
22+
format ||= this.faker.phone.phoneFormats();
23+
return this.faker.helpers.replaceSymbolWithNumber(format);
24+
}
25+
26+
// FIXME: this is strange passing in an array index.
27+
/**
28+
* phoneNumberFormat
29+
*
30+
* @method faker.phone.phoneFormatsArrayIndex
31+
* @param phoneFormatsArrayIndex
32+
* @memberOf faker.phone
33+
*/
34+
phoneNumberFormat(phoneFormatsArrayIndex: number = 0) {
35+
return this.faker.helpers.replaceSymbolWithNumber(
36+
this.faker.definitions.phone_number.formats[phoneFormatsArrayIndex]
37+
);
38+
}
39+
40+
/**
41+
* phoneFormats
42+
*
43+
* @method faker.phone.phoneFormats
44+
*/
45+
phoneFormats(): string {
46+
return this.faker.random.arrayElement(
47+
this.faker.definitions.phone_number.formats
48+
);
49+
}
50+
}

0 commit comments

Comments
 (0)