123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- import metadata from '../../metadata.min.json' assert { type: 'json' };
- import _formatNumber from './format.js';
- function formatNumber() {
- for (var _len = arguments.length, parameters = new Array(_len), _key = 0; _key < _len; _key++) {
- parameters[_key] = arguments[_key];
- }
- parameters.push(metadata);
- return _formatNumber.apply(this, parameters);
- }
- describe('format', function () {
- it('should work with the first argument being a E.164 number', function () {
- formatNumber('+12133734253', 'NATIONAL').should.equal('(213) 373-4253');
- formatNumber('+12133734253', 'INTERNATIONAL').should.equal('+1 213 373 4253');
- formatNumber('+12111111111', 'NATIONAL').should.equal('(211) 111-1111');
- formatNumber('+11111', 'INTERNATIONAL').should.equal('+1 1111');
- formatNumber('+11111', 'NATIONAL').should.equal('1111');
- });
- it('should work with the first object argument expanded', function () {
- formatNumber('2133734253', 'US', 'NATIONAL').should.equal('(213) 373-4253');
- formatNumber('2133734253', 'US', 'INTERNATIONAL').should.equal('+1 213 373 4253');
- });
- it('should support legacy "National" / "International" formats', function () {
- formatNumber('2133734253', 'US', 'National').should.equal('(213) 373-4253');
- formatNumber('2133734253', 'US', 'International').should.equal('+1 213 373 4253');
- });
- it('should format using formats with no leading digits (`format.leadingDigitsPatterns().length === 0`)', function () {
- formatNumber({
- phone: '12345678901',
- countryCallingCode: 888
- }, 'INTERNATIONAL').should.equal('+888 123 456 78901');
- });
- it('should sort out the arguments', function () {
- var options = {
- formatExtension: function formatExtension(number, extension) {
- return "".concat(number, " \u0434\u043E\u0431. ").concat(extension);
- }
- };
- formatNumber({
- phone: '8005553535',
- country: 'RU',
- ext: '123'
- }, 'NATIONAL', options).should.equal('8 (800) 555-35-35 доб. 123');
- formatNumber('+78005553535', 'NATIONAL', options).should.equal('8 (800) 555-35-35');
- formatNumber('8005553535', 'RU', 'NATIONAL', options).should.equal('8 (800) 555-35-35');
- });
- it('should format with national prefix when specifically instructed', function () {
-
- formatNumber('88005553535', 'RU', 'NATIONAL').should.equal('8 (800) 555-35-35');
- formatNumber('88005553535', 'RU', 'NATIONAL', {
- nationalPrefix: false
- }).should.equal('800 555-35-35');
- });
- it('should format valid phone numbers', function () {
-
- formatNumber({
- country: 'CH',
- phone: '446681800'
- }, 'INTERNATIONAL').should.equal('+41 44 668 18 00');
- formatNumber({
- country: 'CH',
- phone: '446681800'
- }, 'E.164').should.equal('+41446681800');
- formatNumber({
- country: 'CH',
- phone: '446681800'
- }, 'RFC3966').should.equal('tel:+41446681800');
- formatNumber({
- country: 'CH',
- phone: '446681800'
- }, 'NATIONAL').should.equal('044 668 18 00');
- formatNumber({
- country: 'FR',
- phone: '169454850'
- }, 'NATIONAL').should.equal('01 69 45 48 50');
- formatNumber('+7 702 211 1111', 'NATIONAL').should.deep.equal('8 (702) 211 1111');
- });
- it('should format national numbers with national prefix even if it\'s optional', function () {
-
- formatNumber({
- country: 'RU',
- phone: '9991234567'
- }, 'NATIONAL').should.equal('8 (999) 123-45-67');
- });
- it('should work in edge cases', function () {
- var thrower;
- formatNumber('', 'RU', 'INTERNATIONAL').should.equal('');
- formatNumber('', 'RU', 'NATIONAL').should.equal('');
- formatNumber({
- country: 'RU',
- phone: ''
- }, 'INTERNATIONAL').should.equal('+7');
- formatNumber({
- country: 'RU',
- phone: ''
- }, 'NATIONAL').should.equal('');
- formatNumber('+121337342530', 'US', 'NATIONAL').should.equal('21337342530');
- formatNumber('28199999', 'AD', 'NATIONAL').should.equal('28199999');
- thrower = function thrower() {
- return formatNumber(89150000000, 'RU', 'NATIONAL');
- };
- thrower.should["throw"]('A phone number must either be a string or an object of shape { phone, [country] }.');
- expect(function () {
- return formatNumber('+121337342530', 'USA', 'NATIONAL');
- }).to["throw"]('Unknown country');
- expect(function () {
- return formatNumber('21337342530', 'USA', 'NATIONAL');
- }).to["throw"]('Unknown country');
- thrower = function thrower() {
- return formatNumber('+123');
- };
- thrower.should["throw"]('`format` argument not passed');
- thrower = function thrower() {
- return formatNumber('123', 'US', 'Gay');
- };
- thrower.should["throw"]('Unknown "format" argument');
- thrower = function thrower() {
- return _formatNumber('123', 'US', 'E.164');
- };
- thrower.should["throw"]('`metadata`');
- formatNumber('012345', 'AC', 'NATIONAL').should.equal('012345');
- expect(formatNumber('+78005553535', 'IDD')).to.be.undefined;
- expect(formatNumber('+78005553535', 'IDD', {
- fromCountry: 'BO'
- })).to.be.undefined;
- expect(function () {
- return formatNumber({
- phone: '123',
- country: 'USA'
- }, 'NATIONAL');
- }).to["throw"]('Unknown country');
- });
- it('should format phone number extensions', function () {
-
- formatNumber({
- country: 'US',
- phone: '2133734253',
- ext: '123'
- }, 'NATIONAL').should.equal('(213) 373-4253 ext. 123');
- formatNumber({
- country: 'US',
- phone: '2133734253',
- ext: '123'
- }, 'INTERNATIONAL').should.equal('+1 213 373 4253 ext. 123');
- formatNumber({
- country: 'US',
- phone: '2133734253',
- ext: '123'
- }, 'INTERNATIONAL').should.equal('+1 213 373 4253 ext. 123');
- formatNumber({
- country: 'US',
- phone: '2133734253',
- ext: '123'
- }, 'E.164').should.equal('+12133734253');
- formatNumber({
- country: 'US',
- phone: '2133734253',
- ext: '123'
- }, 'RFC3966').should.equal('tel:+12133734253;ext=123');
- formatNumber({
- country: 'GB',
- phone: '7912345678',
- ext: '123'
- }, 'INTERNATIONAL').should.equal('+44 7912 345678 x123');
- });
- it('should work with Argentina numbers', function () {
-
-
-
- formatNumber({
- country: 'AR',
- phone: '3435551212'
- }, 'INTERNATIONAL').should.equal('+54 3435 55 1212');
- formatNumber({
- country: 'AR',
- phone: '3435551212'
- }, 'NATIONAL').should.equal('03435 55-1212');
- });
- it('should work with Mexico numbers', function () {
-
- formatNumber({
- country: 'MX',
- phone: '4499780001'
- }, 'INTERNATIONAL').should.equal('+52 449 978 0001');
- formatNumber({
- country: 'MX',
- phone: '4499780001'
- }, 'NATIONAL').should.equal('449 978 0001');
-
-
- formatNumber({
- country: 'MX',
- phone: '3312345678'
- }, 'INTERNATIONAL').should.equal('+52 33 1234 5678');
- formatNumber({
- country: 'MX',
- phone: '3312345678'
- }, 'NATIONAL').should.equal('33 1234 5678');
- });
- it('should format possible numbers', function () {
- formatNumber({
- countryCallingCode: '7',
- phone: '1111111111'
- }, 'E.164').should.equal('+71111111111');
- formatNumber({
- countryCallingCode: '7',
- phone: '1111111111'
- }, 'NATIONAL').should.equal('1111111111');
- formatNumber({
- countryCallingCode: '7',
- phone: '1111111111'
- }, 'INTERNATIONAL').should.equal('+7 1111111111');
- });
- it('should format IDD-prefixed number', function () {
-
- expect(formatNumber('+78005553535', 'IDD')).to.be.undefined;
- expect(formatNumber('+78005553535', 'IDD', {
- fromCountry: 'BO'
- })).to.be.undefined;
- formatNumber('+12133734253', 'IDD', {
- fromCountry: 'CA',
- humanReadable: true
- }).should.equal('1 (213) 373-4253');
- formatNumber('+78005553535', 'IDD', {
- fromCountry: 'KZ',
- humanReadable: true
- }).should.equal('8 (800) 555-35-35');
- formatNumber('+78005553535', 'IDD', {
- fromCountry: 'US',
- humanReadable: true
- }).should.equal('011 7 800 555 35 35');
- });
- it('should format non-geographic numbering plan phone numbers', function () {
-
- formatNumber('+870773111632', 'INTERNATIONAL').should.equal('+870 773 111 632');
- formatNumber('+870773111632', 'NATIONAL').should.equal('773 111 632');
- });
- it('should use the default IDD prefix when formatting a phone number', function () {
-
-
- formatNumber('+390236618300', 'IDD', {
- fromCountry: 'UZ'
- }).should.equal('8~10 39 02 3661 8300');
- });
- });
|