libphonenumber-js 1.9.31 → 1.9.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -67,6 +67,11 @@ And edit the README:
67
67
 
68
68
  This changelog [only](https://gitlab.com/catamphetamine/libphonenumber-js/-/issues/16#note_594165443) mentions the changes in the code. See [Google's changelog](https://github.com/google/libphonenumber/blob/master/release_notes.txt) for metadata changes.
69
69
 
70
+ 1.9.36 / 05.10.2021
71
+ ==================
72
+
73
+ * Added a [`setExt(ext: string)`](https://gitlab.com/catamphetamine/libphonenumber-js/#setextext-string) function of a `PhoneNumber` class instance. It could be useful when formatting phone numbers stored as two separate fields: the phone number itself and the extension part.
74
+
70
75
  1.9.27 / 09.09.2021
71
76
  ==================
72
77
 
package/README.md CHANGED
@@ -438,6 +438,24 @@ const phoneNumber = new PhoneNumber('RU', '8005553535', metadata)
438
438
 
439
439
  `PhoneNumber` class instance provides the following methods:
440
440
 
441
+ #### `setExt(ext: string)`
442
+
443
+ Sets a [phone number extension](https://en.wikipedia.org/wiki/Extension_(telephone)) of a phone number. Could be useful when formatting phone numbers stored as two separate fields: the phone number itself and the extension part.
444
+
445
+ ```js
446
+ const phone = "+12133734253"
447
+ const phoneExt = "1234"
448
+
449
+ const phoneNumber = parsePhoneNumber(phone)
450
+ if (phoneNumber) {
451
+ if (phoneExt) {
452
+ phoneNumber.setExt(phoneExt)
453
+ }
454
+ // Returns "(213) 373-4253 ext. 1234"
455
+ return phoneNumber.formatNational()
456
+ }
457
+ ```
458
+
441
459
  #### `format(format: string, [options]): string`
442
460
 
443
461
  Formats the phone number into a string according to a `format`.
@@ -1315,7 +1333,7 @@ When reporting `findPhoneNumbersInText()` bugs one should know that `findPhoneNu
1315
1333
 
1316
1334
  ## TypeScript
1317
1335
 
1318
- [TypeScript support](https://gitlab.com/catamphetamine/libphonenumber-js/blob/master/index.d.ts) for this library is entirely community-driven. I myself don't use TypeScript. Send your pull requests.
1336
+ This library comes with TypeScript "typings". If you happen to find any bugs in those, create an issue.
1319
1337
 
1320
1338
  <!--
1321
1339
  https://codesandbox.io/s/damp-fast-26c7z?file=/src/index.ts
@@ -75,6 +75,11 @@ function () {
75
75
  }
76
76
 
77
77
  _createClass(PhoneNumber, [{
78
+ key: "setExt",
79
+ value: function setExt(ext) {
80
+ this.ext = ext;
81
+ }
82
+ }, {
78
83
  key: "isPossible",
79
84
  value: function isPossible() {
80
85
  return (0, _isPossibleNumber_["default"])(this, {
@@ -1 +1 @@
1
- {"version":3,"sources":["../source/PhoneNumber.js"],"names":["USE_NON_GEOGRAPHIC_COUNTRY_CODE","PhoneNumber","countryCallingCode","nationalNumber","metadata","TypeError","_metadata","Metadata","isCountryCode","country","isNonGeographicCallingCode","number","v2","phoneNumber","ext","format","options","value","test"],"mappings":";;;;;;;AAAA;;AACA;;AACA;;AACA;;AACA;;AACA;;;;;;;;;;;;;;AAEA,IAAMA,+BAA+B,GAAG,KAAxC;;IAEqBC,W;;;AACpB,uBAAYC,kBAAZ,EAAgCC,cAAhC,EAAgDC,QAAhD,EAA0D;AAAA;;AACzD,QAAI,CAACF,kBAAL,EAAyB;AACxB,YAAM,IAAIG,SAAJ,CAAc,8CAAd,CAAN;AACA;;AACD,QAAI,CAACF,cAAL,EAAqB;AACpB,YAAM,IAAIE,SAAJ,CAAc,6BAAd,CAAN;AACA;;AACD,QAAI,CAACD,QAAL,EAAe;AACd,YAAM,IAAIC,SAAJ,CAAc,uBAAd,CAAN;AACA;;AACD,QAAMC,SAAS,GAAG,IAAIC,qBAAJ,CAAaH,QAAb,CAAlB,CAVyD,CAWzD;AACA;;;AACA,QAAII,aAAa,CAACN,kBAAD,CAAjB,EAAuC;AACtC,WAAKO,OAAL,GAAeP,kBAAf;;AACAI,MAAAA,SAAS,CAACG,OAAV,CAAkBP,kBAAlB;;AACAA,MAAAA,kBAAkB,GAAGI,SAAS,CAACJ,kBAAV,EAArB;AACA,KAJD,MAIO;A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AxB,CAAP;AACA;;;wCAEmBA,O,EAAS;AAC5B,aAAO,KAAKD,MAAL,CAAY,eAAZ,EAA6BC,OAA7B,CAAP;AACA;;;2BAEMA,O,EAAS;AACf,aAAO,KAAKD,MAAL,CAAY,SAAZ,EAAuBC,OAAvB,CAAP;AACA;;;;;;;;AAGF,IAAMR,aAAa,GAAG,SAAhBA,aAAgB,CAACS,KAAD;AAAA,SAAW,aAAaC,IAAb,CAAkBD,KAAlB,CAAX;AAAA,CAAtB","sourcesContent":["import Metadata from './metadata'\r\nimport isPossibleNumber from './isPossibleNumber_'\r\nimport isValidNumber from './validate_'\r\nimport isValidNumberForRegion from './isValidNumberForRegion_'\r\nimport getNumberType from './helpers/getNumberType'\r\nimport formatNumber from './format_'\r\n\r\nconst USE_NON_GEOGRAPHIC_COUNTRY_CODE = false\r\n\r\nexport default class PhoneNumber {\r\n\tconstructor(countryCallingCode, nationalNumber, metadata) {\r\n\t\tif (!countryCallingCode) {\r\n\t\t\tthrow new TypeError('`country` or `countryCallingCode` not passed')\r\n\t\t}\r\n\t\tif (!nationalNumber) {\r\n\t\t\tthrow new TypeError('`nationalNumber` not passed')\r\n\t\t}\r\n\t\tif (!metadata) {\r\n\t\t\tthrow new TypeError('`metadata` not passed')\r\n\t\t}\r\n\t\tconst _metadata = new Metadata(metadata)\r\n\t\t// If country code is passed then derive `countryCallingCode` from it.\r\n\t\t// Also store the country code as `.country`.\r\n\t\tif (isCountryCode(countryCallingCode)) {\r\n\t\t\tthis.country = countryCallingCode\r\n\t\t\t_metadata.country(countryCallingCode)\r\n\t\t\tcountryCallingCode = _metadata.countryCallingCode()\r\n\t\t} else {\r\n\t\t\t/* istanbul ignore if */\r\n\t\t\tif (USE_NON_GEOGRAPHIC_COUNTRY_CODE) {\r\n\t\t\t\tif (_metadata.isNonGeographicCallingCode(countryCallingCode)) {\r\n\t\t\t\t\tthis.country = '001'\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t\tthis.countryCallingCode = countryCallingCode\r\n\t\tthis.nationalNumber = nationalNumber\r\n\t\tthis.number = '+' + this.countryCallingCode + this.nationalNumber\r\n\t\tthis.metadata = metadata\r\n\t}\r\n\r\n\tisPossible() {\r\n\t\treturn isPossibleNumber(this, { v2: true }, this.metadata)\r\n\t}\r\n\r\n\tisValid() {\r\n\t\treturn isValidNumber(this, { v2: true }, this.metadata)\r\n\t}\r\n\r\n\tisNonGeographic() {\r\n\t\tconst metadata = new Metadata(this.metadata)\r\n\t\treturn metadata.isNonGeographicCallingCode(this.countryCallingCode)\r\n\t}\r\n\r\n\tisEqual(phoneNumber) {\r\n\t\treturn this.number === phoneNumber.number && this.ext === phoneNumber.ext\r\n\t}\r\n\r\n\t// // Is just an alias for `this.isValid() && this.country === country`.\r\n\t// // https://github.com/googlei18n/libphonenumber/blob/master/FAQ.md#when-should-i-use-isvalidnumberforregion\r\n\t// isValidForRegion(country) {\r\n\t// \treturn isValidNumberForRegion(this, country, { v2: true }, this.metadata)\r\n\t// }\r\n\r\n\tgetType() {\r\n\t\treturn getNumberType(this, { v2: true }, this.metadata)\r\n\t}\r\n\r\n\tformat(format, options) {\r\n\t\treturn formatNumber(\r\n\t\t\tthis,\r\n\t\t\tformat,\r\n\t\t\toptions ? { ...options, v2: true } : { v2: true },\r\n\t\t\tthis.metadata\r\n\t\t)\r\n\t}\r\n\r\n\tformatNational(options) {\r\n\t\treturn this.format('NATIONAL', options)\r\n\t}\r\n\r\n\tformatInternational(options) {\r\n\t\treturn this.format('INTERNATIONAL', options)\r\n\t}\r\n\r\n\tgetURI(options) {\r\n\t\treturn this.format('RFC3966', options)\r\n\t}\r\n}\r\n\r\nconst isCountryCode = (value) => /^[A-Z]{2}$/.test(value)"],"file":"PhoneNumber.js"}
1
+ {"version":3,"sources":["../source/PhoneNumber.js"],"names":["USE_NON_GEOGRAPHIC_COUNTRY_CODE","PhoneNumber","countryCallingCode","nationalNumber","metadata","TypeError","_metadata","Metadata","isCountryCode","country","isNonGeographicCallingCode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ACA;;;8BAES;AACT,aAAO,2BAAc,IAAd,EAAoB;AAAES,QAAAA,EAAE,EAAE;AAAN,OAApB,EAAkC,KAAKT,QAAvC,CAAP;AACA;;;sCAEiB;AACjB,UAAMA,QAAQ,GAAG,IAAIG,qBAAJ,CAAa,KAAKH,QAAlB,CAAjB;AACA,aAAOA,QAAQ,CAACM,0BAAT,CAAoC,KAAKR,kBAAzC,CAAP;AACA;;;4BAEOY,W,EAAa;AACpB,aAAO,KAAKH,MAAL,KAAgBG,WAAW,CAACH,MAA5B,IAAsC,KAAKC,GAAL,KAAaE,WAAW,CAACF,GAAtE;AACA,K,CAED;AACA;AACA;AACA;AACA;;;;8BAEU;AACT,aAAO,+BAAc,IAAd,EAAoB;AAAEC,QAAAA,EAAE,EAAE;AAAN,OAApB,EAAkC,KAAKT,QAAvC,CAAP;AACA;;;2BAEMW,O,EAAQC,O,EAAS;AACvB,aAAO,yBACN,IADM,EAEND,OAFM,EAGNC,OAAO,qBAAQA,OAAR;AAAiBH,QAAAA,EAAE,EAAE;AAArB,WAA8B;AAAEA,QAAAA,EAAE,EAAE;AAAN,OAH/B,EAIN,KAAKT,QAJC,CAAP;AAMA;;;mCAEcY,O,EAAS;AACvB,aAAO,KAAKD,MAAL,CAAY,UAAZ,EAAwBC,OAAxB,CAAP;AACA;;;wCAEmBA,O,EAAS;AAC5B,aAAO,KAAKD,MAAL,CAAY,eAAZ,EAA6BC,OAA7B,CAAP;AACA;;;2BAEMA,O,EAAS;AACf,aAAO,KAAKD,MAAL,CAAY,SAAZ,EAAuBC,OAAvB,CAAP;AACA;;;;;;;;AAGF,IAAMR,aAAa,GAAG,SAAhBA,aAAgB,CAACS,KAAD;AAAA,SAAW,aAAaC,IAAb,CAAkBD,KAAlB,CAAX;AAAA,CAAtB","sourcesContent":["import Metadata from './metadata'\r\nimport isPossibleNumber from './isPossibleNumber_'\r\nimport isValidNumber from './validate_'\r\nimport isValidNumberForRegion from './isValidNumberForRegion_'\r\nimport getNumberType from './helpers/getNumberType'\r\nimport formatNumber from './format_'\r\n\r\nconst USE_NON_GEOGRAPHIC_COUNTRY_CODE = false\r\n\r\nexport default class PhoneNumber {\r\n\tconstructor(countryCallingCode, nationalNumber, metadata) {\r\n\t\tif (!countryCallingCode) {\r\n\t\t\tthrow new TypeError('`country` or `countryCallingCode` not passed')\r\n\t\t}\r\n\t\tif (!nationalNumber) {\r\n\t\t\tthrow new TypeError('`nationalNumber` not passed')\r\n\t\t}\r\n\t\tif (!metadata) {\r\n\t\t\tthrow new TypeError('`metadata` not passed')\r\n\t\t}\r\n\t\tconst _metadata = new Metadata(metadata)\r\n\t\t// If country code is passed then derive `countryCallingCode` from it.\r\n\t\t// Also store the country code as `.country`.\r\n\t\tif (isCountryCode(countryCallingCode)) {\r\n\t\t\tthis.country = countryCallingCode\r\n\t\t\t_metadata.country(countryCallingCode)\r\n\t\t\tcountryCallingCode = _metadata.countryCallingCode()\r\n\t\t} else {\r\n\t\t\t/* istanbul ignore if */\r\n\t\t\tif (USE_NON_GEOGRAPHIC_COUNTRY_CODE) {\r\n\t\t\t\tif (_metadata.isNonGeographicCallingCode(countryCallingCode)) {\r\n\t\t\t\t\tthis.country = '001'\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t\tthis.countryCallingCode = countryCallingCode\r\n\t\tthis.nationalNumber = nationalNumber\r\n\t\tthis.number = '+' + this.countryCallingCode + this.nationalNumber\r\n\t\tthis.metadata = metadata\r\n\t}\r\n\r\n\tsetExt(ext) {\r\n\t\tthis.ext = ext\r\n\t}\r\n\r\n\tisPossible() {\r\n\t\treturn isPossibleNumber(this, { v2: true }, this.metadata)\r\n\t}\r\n\r\n\tisValid() {\r\n\t\treturn isValidNumber(this, { v2: true }, this.metadata)\r\n\t}\r\n\r\n\tisNonGeographic() {\r\n\t\tconst metadata = new Metadata(this.metadata)\r\n\t\treturn metadata.isNonGeographicCallingCode(this.countryCallingCode)\r\n\t}\r\n\r\n\tisEqual(phoneNumber) {\r\n\t\treturn this.number === phoneNumber.number && this.ext === phoneNumber.ext\r\n\t}\r\n\r\n\t// // Is just an alias for `this.isValid() && this.country === country`.\r\n\t// // https://github.com/googlei18n/libphonenumber/blob/master/FAQ.md#when-should-i-use-isvalidnumberforregion\r\n\t// isValidForRegion(country) {\r\n\t// \treturn isValidNumberForRegion(this, country, { v2: true }, this.metadata)\r\n\t// }\r\n\r\n\tgetType() {\r\n\t\treturn getNumberType(this, { v2: true }, this.metadata)\r\n\t}\r\n\r\n\tformat(format, options) {\r\n\t\treturn formatNumber(\r\n\t\t\tthis,\r\n\t\t\tformat,\r\n\t\t\toptions ? { ...options, v2: true } : { v2: true },\r\n\t\t\tthis.metadata\r\n\t\t)\r\n\t}\r\n\r\n\tformatNational(options) {\r\n\t\treturn this.format('NATIONAL', options)\r\n\t}\r\n\r\n\tformatInternational(options) {\r\n\t\treturn this.format('INTERNATIONAL', options)\r\n\t}\r\n\r\n\tgetURI(options) {\r\n\t\treturn this.format('RFC3966', options)\r\n\t}\r\n}\r\n\r\nconst isCountryCode = (value) => /^[A-Z]{2}$/.test(value)"],"file":"PhoneNumber.js"}