Skip to content

Commit

Permalink
refactor(phone): rename phoneNumber to number (faker-js#1063)
Browse files Browse the repository at this point in the history
  • Loading branch information
xDivisionByZerox authored and Minozzzi committed Jul 19, 2022
1 parent dadb7b8 commit 2cb4f23
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/modules/fake/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class Fake {
* and if that isn't possible, we will fall back to string:
*
* ```js
* const message = faker.fake(`You can call me at {{phone.phoneNumber(+!# !## #### #####!)}}.')
* const message = faker.fake(`You can call me at {{phone.number(+!# !## #### #####!)}}.')
* ```
*
* Currently it is not possible to set more than a single parameter.
Expand All @@ -53,7 +53,7 @@ export class Fake {
* faker.fake('{{name.lastName}}, {{name.firstName}} {{name.suffix}}') // 'Durgan, Noe MD'
* faker.fake('This is static test.') // 'This is static test.'
* faker.fake('Good Morning {{name.firstName}}!') // 'Good Morning Estelle!'
* faker.fake('You can call me at {{phone.phoneNumber(!## ### #####!)}}.') // 'You can call me at 202 555 973722.'
* faker.fake('You can call me at {{phone.number(!## ### #####!)}}.') // 'You can call me at 202 555 973722.'
* faker.fake('I flipped the coin an got: {{helpers.arrayElement(["heads", "tails"])}}') // 'I flipped the coin an got: tails'
*/
fake(str: string): string {
Expand Down
25 changes: 24 additions & 1 deletion src/modules/phone/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,36 @@ export class Phone {
*
* @param format Format of the phone number. Defaults to a random phone number format.
*
* @see faker.phone.number
*
* @example
* faker.phone.phoneNumber() // '961-770-7727'
* faker.phone.phoneNumber('501-###-###') // '501-039-841'
* faker.phone.phoneNumber('+48 91 ### ## ##') // '+48 91 463 61 70'
*
* @deprecated
*/
// TODO @pkuczynski 2022-02-01: simplify name to `number()`
phoneNumber(format?: string): string {
deprecated({
deprecated: 'faker.phone.phoneNumber()',
proposed: 'faker.phone.number()',
since: 'v7.3',
until: 'v8.0',
});
return this.faker.phone.number(format);
}

/**
* Generates a random phone number.
*
* @param format Format of the phone number. Defaults to a random phone number format.
*
* @example
* faker.phone.number() // '961-770-7727'
* faker.phone.number('501-###-###') // '501-039-841'
* faker.phone.number('+48 91 ### ## ##') // '+48 91 463 61 70'
*/
number(format?: string): string {
format =
format ??
this.faker.helpers.arrayElement(
Expand Down
2 changes: 1 addition & 1 deletion test/fake.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { FakerError } from '../src/errors/faker-error';
describe('fake', () => {
describe('fake()', () => {
it('replaces a token with a random value for a method with no parameters', () => {
const name = faker.fake('{{phone.phoneNumber}}');
const name = faker.fake('{{phone.number}}');
expect(name).toMatch(/\d/);
});

Expand Down
18 changes: 18 additions & 0 deletions test/phone.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const seededRuns = [
phoneNumber: {
noArgs: '891.775.5141',
},
number: {
noArgs: '891.775.5141',
},
phoneNumberFormat: {
noArgs: '479-377-5514',
phoneFormatsArrayIndex: { arrayIndex: 1, expected: '(479) 377-5514' },
Expand All @@ -27,6 +30,9 @@ const seededRuns = [
phoneNumber: {
noArgs: '(612) 454-0325',
},
number: {
noArgs: '(612) 454-0325',
},
phoneNumberFormat: {
noArgs: '451-325-4032',
phoneFormatsArrayIndex: { arrayIndex: 1, expected: '(451) 325-4032' },
Expand All @@ -45,6 +51,9 @@ const seededRuns = [
phoneNumber: {
noArgs: '1-587-319-0616 x27431',
},
number: {
noArgs: '1-587-319-0616 x27431',
},
phoneNumberFormat: {
noArgs: '948-821-9061',
phoneFormatsArrayIndex: { arrayIndex: 1, expected: '(948) 821-9061' },
Expand All @@ -61,6 +70,7 @@ const seededRuns = [

const functionNames = [
'phoneNumber',
'number',
'phoneNumberFormat',
'phoneFormats',
'imei',
Expand Down Expand Up @@ -111,6 +121,14 @@ describe('phone', () => {
});
});

describe('number()', () => {
it('should return a random phoneNumber with a random format', () => {
const phoneNumber = faker.phone.number();

expect(phoneNumber).toMatch(/\d/);
});
});

describe('phoneNumberFormat()', () => {
it('should return phone number with proper US format (Array index)', () => {
faker.locale = 'en';
Expand Down

0 comments on commit 2cb4f23

Please sign in to comment.