diff --git a/README.md b/README.md index e15e2c5..d261171 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,7 @@ Contents - [Faker.Music](doc/music.md) - [Faker.Name](doc/name.md) - [Faker.Number](doc/number.md) + - [Faker.PhoneNumber](doc/phone_number.md) - [Faker.Placeholdit](doc/placeholdit.md) - [Faker.Pokemon](doc/pokemon.md) - [Faker.RickAndMorty](doc/rick_and_morty.md) diff --git a/data/phone-number.json b/data/phone-number.json new file mode 100644 index 0000000..132c0af --- /dev/null +++ b/data/phone-number.json @@ -0,0 +1,586 @@ +{ + "phoneNumber": { + "formats": [ + "01#### #####", + "01### ######", + "01#1 ### ####", + "011# ### ####", + "02# #### ####", + "03## ### ####", + "055 #### ####", + "056 #### ####", + "0800 ### ####", + "08## ### ####", + "09## ### ####", + "016977 ####", + "01### #####", + "0500 ######", + "0800 ######" + ] + }, + "cellPhone": { + "formats": [ + "074## ######", + "075## ######", + "076## ######", + "077## ######", + "078## ######", + "079## ######" + ] + }, + "areaCodes": [ + "201", + "202", + "203", + "205", + "206", + "207", + "208", + "209", + "210", + "212", + "213", + "214", + "215", + "216", + "217", + "218", + "219", + "224", + "225", + "226", + "228", + "229", + "231", + "234", + "239", + "240", + "248", + "251", + "252", + "253", + "254", + "256", + "260", + "262", + "267", + "269", + "270", + "276", + "281", + "301", + "302", + "303", + "304", + "305", + "307", + "308", + "309", + "310", + "312", + "313", + "314", + "315", + "316", + "317", + "318", + "319", + "320", + "321", + "323", + "330", + "334", + "336", + "337", + "339", + "347", + "351", + "352", + "360", + "361", + "386", + "401", + "402", + "404", + "405", + "406", + "407", + "408", + "409", + "410", + "412", + "413", + "414", + "415", + "417", + "419", + "423", + "424", + "425", + "434", + "435", + "440", + "443", + "469", + "478", + "479", + "480", + "484", + "501", + "502", + "503", + "504", + "505", + "507", + "508", + "509", + "510", + "512", + "513", + "515", + "516", + "517", + "518", + "520", + "530", + "540", + "541", + "551", + "559", + "561", + "562", + "563", + "567", + "570", + "571", + "573", + "574", + "580", + "585", + "586", + "601", + "602", + "603", + "605", + "606", + "607", + "608", + "609", + "610", + "612", + "614", + "615", + "616", + "617", + "618", + "619", + "620", + "623", + "626", + "630", + "631", + "636", + "641", + "646", + "650", + "651", + "660", + "661", + "662", + "678", + "682", + "701", + "702", + "703", + "704", + "706", + "707", + "708", + "712", + "713", + "714", + "715", + "716", + "717", + "718", + "719", + "720", + "724", + "727", + "731", + "732", + "734", + "740", + "754", + "757", + "760", + "763", + "765", + "770", + "772", + "773", + "774", + "775", + "781", + "785", + "786", + "801", + "802", + "803", + "804", + "805", + "806", + "808", + "810", + "812", + "813", + "814", + "815", + "816", + "817", + "818", + "828", + "830", + "831", + "832", + "843", + "845", + "847", + "848", + "850", + "856", + "857", + "858", + "859", + "860", + "862", + "863", + "864", + "865", + "870", + "878", + "901", + "903", + "904", + "906", + "907", + "908", + "909", + "910", + "912", + "913", + "914", + "915", + "916", + "917", + "918", + "919", + "920", + "925", + "928", + "931", + "936", + "937", + "940", + "941", + "947", + "949", + "952", + "954", + "956", + "970", + "971", + "972", + "973", + "978", + "979", + "980", + "985", + "989" + ], + "exchangeCodes": [ + "201", + "202", + "203", + "205", + "206", + "207", + "208", + "209", + "210", + "212", + "213", + "214", + "215", + "216", + "217", + "218", + "219", + "224", + "225", + "227", + "228", + "229", + "231", + "234", + "239", + "240", + "248", + "251", + "252", + "253", + "254", + "256", + "260", + "262", + "267", + "269", + "270", + "276", + "281", + "283", + "301", + "302", + "303", + "304", + "305", + "307", + "308", + "309", + "310", + "312", + "313", + "314", + "315", + "316", + "317", + "318", + "319", + "320", + "321", + "323", + "330", + "331", + "334", + "336", + "337", + "339", + "347", + "351", + "352", + "360", + "361", + "386", + "401", + "402", + "404", + "405", + "406", + "407", + "408", + "409", + "410", + "412", + "413", + "414", + "415", + "417", + "419", + "423", + "424", + "425", + "434", + "435", + "440", + "443", + "445", + "464", + "469", + "470", + "475", + "478", + "479", + "480", + "484", + "501", + "502", + "503", + "504", + "505", + "507", + "508", + "509", + "510", + "512", + "513", + "515", + "516", + "517", + "518", + "520", + "530", + "540", + "541", + "551", + "557", + "559", + "561", + "562", + "563", + "564", + "567", + "570", + "571", + "573", + "574", + "580", + "585", + "586", + "601", + "602", + "603", + "605", + "606", + "607", + "608", + "609", + "610", + "612", + "614", + "615", + "616", + "617", + "618", + "619", + "620", + "623", + "626", + "630", + "631", + "636", + "641", + "646", + "650", + "651", + "660", + "661", + "662", + "667", + "678", + "682", + "701", + "702", + "703", + "704", + "706", + "707", + "708", + "712", + "713", + "714", + "715", + "716", + "717", + "718", + "719", + "720", + "724", + "727", + "731", + "732", + "734", + "737", + "740", + "754", + "757", + "760", + "763", + "765", + "770", + "772", + "773", + "774", + "775", + "781", + "785", + "786", + "801", + "802", + "803", + "804", + "805", + "806", + "808", + "810", + "812", + "813", + "814", + "815", + "816", + "817", + "818", + "828", + "830", + "831", + "832", + "835", + "843", + "845", + "847", + "848", + "850", + "856", + "857", + "858", + "859", + "860", + "862", + "863", + "864", + "865", + "870", + "872", + "878", + "901", + "903", + "904", + "906", + "907", + "908", + "909", + "910", + "912", + "913", + "914", + "915", + "916", + "917", + "918", + "919", + "920", + "925", + "928", + "931", + "936", + "937", + "940", + "941", + "947", + "949", + "952", + "954", + "956", + "959", + "970", + "971", + "972", + "973", + "975", + "978", + "979", + "980", + "984", + "985", + "989" + ] +} diff --git a/doc/phone_number.md b/doc/phone_number.md new file mode 100644 index 0000000..8b00507 --- /dev/null +++ b/doc/phone_number.md @@ -0,0 +1,34 @@ +# Faker.PhoneNumber + +Phone numbers may be in any of the following formats: + + * 333-333-3333 + * (333) 333-3333 + * 1-333-333-3333 + * 333.333.3333 + * 333-333-3333 + * 333-333-3333 x3333 + * (333) 333-3333 x3333 + * 1-333-333-3333 x3333 + * 333.333.3333 x3333 + +(Don't let the example output below fool you - any format can be returned at random.) + +```js +Faker.PhoneNumber.phoneNumber() //=> "397.693.1309" + +Faker.PhoneNumber.cellPhone() //=> "(186)285-7925" + +// US only +Faker.PhoneNumber.areaCode() //=> "201" + +// US only +Faker.PhoneNumber.exchangeCode() //=> "208" + +// Optional parameter: length=4 +Faker.PhoneNumber.subscriberNumber() //=> "3873" + +Faker.PhoneNumber.subscriberNumber(2) //=> "39" + +Faker.PhoneNumber.extension() //=> "3764" +``` diff --git a/src/faker.js b/src/faker.js index 1b0d085..f370adc 100644 --- a/src/faker.js +++ b/src/faker.js @@ -37,6 +37,7 @@ import * as Matz from './faker/matz'; import * as Music from './faker/music'; import * as Name from './faker/name'; import * as Number from './faker/number'; +import * as PhoneNumber from './faker/phone-number'; import * as Placeholdit from './faker/placeholdit'; import * as Pokemon from './faker/pokemon'; import * as RickAndMorty from './faker/rick-and-morty'; @@ -94,6 +95,7 @@ module.exports = { Music, Name, Number, + PhoneNumber, Placeholdit, Pokemon, RickAndMorty, diff --git a/src/faker/phone-number.js b/src/faker/phone-number.js new file mode 100644 index 0000000..2caf99e --- /dev/null +++ b/src/faker/phone-number.js @@ -0,0 +1,31 @@ +import { itemFromCollection, randomNumber } from '../utils/random'; + +const data = require('../../data/phone-number.json'); + +export function phoneNumber() { + const format = itemFromCollection(data['phoneNumber']['formats']); + return format.replace(/#/g, _ => randomNumber(0, 9)); +} + +export function cellPhone() { + const format = itemFromCollection(data['cellPhone']['formats']); + return format.replace(/#/g, _ => randomNumber(0, 9)); +} + +export function areaCode() { + return itemFromCollection(data['areaCodes']); +} + +export function exchangeCode() { + return itemFromCollection(data['exchangeCodes']); +} + +export function subscriberNumber(length=4) { + const min = Math.pow(10, length)/10; + const max = (min * 10) - 1; + return randomNumber(min, max); +} + +export function extension(length=4) { + return subscriberNumber(length); +} diff --git a/test/faker.spec.js b/test/faker.spec.js index 8ba15fc..6115f5a 100644 --- a/test/faker.spec.js +++ b/test/faker.spec.js @@ -241,6 +241,12 @@ describe('#Faker', () => { }); }); + describe('#PhoneNumber', () => { + it('should be an object', () => { + expect(Faker.PhoneNumber).to.be.a('object'); + }); + }); + describe('#Placeholdit', () => { it('should be an object', () => { expect(Faker.Placeholdit).to.be.a('object'); diff --git a/test/faker/phone-number.spec.js b/test/faker/phone-number.spec.js new file mode 100644 index 0000000..80234c9 --- /dev/null +++ b/test/faker/phone-number.spec.js @@ -0,0 +1,50 @@ +'use strict'; +const expect = require('chai').expect; +const PhoneNumber = require('../../src/faker/phone-number'); +const data = require('../../data/phone-number.json'); + +describe('PhoneNumber', () => { + describe('#phoneNumber', () => { + it('should return a phone number with hashes replaced', () => { + expect(PhoneNumber.phoneNumber()).to.not.match(/[#]/); + }); + }); + + describe('#cellPhone', () => { + it('should return a cellphone number with hashes replaced', () => { + expect(PhoneNumber.cellPhone()).to.not.match(/[#]/); + }); + }); + + describe('#areaCode', () => { + it('should return an area code', () => { + expect(PhoneNumber.areaCode()).to.be.oneOf(data['areaCodes']); + }); + }); + + describe('#exchangeCode', () => { + it('should return an exchange code', () => { + expect(PhoneNumber.exchangeCode()).to.be.oneOf(data['exchangeCodes']); + }); + }); + + describe('#subscriberNumber', () => { + it('should return a number with a default length', () => { + expect(PhoneNumber.subscriberNumber()).to.match(/^\d{4}$/); + }); + + it('should return a number with the specified length', () => { + expect(PhoneNumber.subscriberNumber(10)).to.match(/^\d{10}$/); + }); + }); + + describe('#extension', () => { + it('should return a number with a default length', () => { + expect(PhoneNumber.extension()).to.match(/^\d{4}$/); + }); + + it('should return a number with the specified length', () => { + expect(PhoneNumber.extension(10)).to.match(/^\d{10}$/); + }); + }); +});