libphonenumber-js 1.13.1 → 1.13.2

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/README.md CHANGED
@@ -250,9 +250,13 @@ validatePhoneNumberLength('8 (800) 555', 'RU') === 'TOO_SHORT' // Length is inva
250
250
  validatePhoneNumberLength('8 (800) 555-35-35', 'RU') === undefined // Length is valid
251
251
  ```
252
252
 
253
- `isPossiblePhoneNumber()` only validates phone number length, while `isValidPhoneNumber()` validates both phone number length and phone number digits.
253
+ `isPossiblePhoneNumber()` validates phone number length.
254
254
 
255
- `validatePhoneNumberLength()` is just a more detailed version of `isPossiblePhoneNumber()` — if the phone number length is invalid, instead of just returning `false`, it returns the actual reason why the phone number length is incorrect: `TOO_SHORT`, `TOO_LONG`, etc.
255
+ `isValidPhoneNumber()` validates both phone number length and phone number digits.
256
+
257
+ It may seem counter-intuitive but when [choosing between the two](#using-phone-number-validation-feature), I'd personally prefer `isPossiblePhoneNumber()` because its strength is in its weakness. `isValidPhoneNumber()` is a double-edged sword in terms of how strict it is, and when not updated regularly, it could get stale over time and start rejecting freshly-assigned phone number ranges.
258
+
259
+ As for `validatePhoneNumberLength()`, is just a more detailed version of `isPossiblePhoneNumber()` for those who need it — when the phone number length is invalid, instead of returning just `false`, it returns the actual reason why the phone number length is incorrect: `TOO_SHORT`, `TOO_LONG`, etc.
256
260
 
257
261
  ### Full-text search
258
262
 
@@ -660,7 +664,7 @@ Checks if the phone number is "possible". Only checks the phone number length. D
660
664
 
661
665
  Checks if the phone number is "valid". First checks the phone number length and then checks the phone number digits against all available regular expressions.
662
666
 
663
- By default, this library uses `min` ("minimal") metadata which is only `80 kB` in size but also doesn't include the precise validation regular expressions resulting in less strict validation rules (some very basic validation like number length check is still included for each country). If you don't mind the extra `65 kB` of metadata then use [`max`](#min-vs-max-vs-mobile-vs-core) metadata instead of the default (`min`) one. Google's library always uses "full" metadata so it will yield different `isValidNumber()` results compared to the "minimal" metadata used by default in this library.
667
+ By default, this package uses [`min`](#min-vs-max-vs-mobile-vs-core) ("minimal") metadata which is only `80 kB` in size but also doesn't include the precise regular expressions for validating phone number digits, i.e. it only validates phone number length, effectively causing `isValid()` to behave same way as if it was `isPossible()`. So if you need to validate phone number digits themselves and you don't mind the extra `65 kB` of metadata then use [`max`](#min-vs-max-vs-mobile-vs-core) metadata instead of the default (`min`) one. For reference, Google's original `libphonenumber` always uses `max` metadata and doesn't have a concept of `min` metadata.
664
668
 
665
669
  <details>
666
670
  <summary>See an example illustrating different results when using <code>/min</code> vs <code>/max</code> vs <code>/mobile</code> metadata</summary>
@@ -1580,9 +1584,9 @@ isValidNumberForRegion('07624369230', 'IM') === true
1580
1584
 
1581
1585
  ## Using phone number validation feature
1582
1586
 
1583
- I personally don't use strict phone number validation feature. The rationale is that telephone numbering plans can and sometimes do change, meaning that [`PhoneNumber.isValid()`](#isvalid-boolean) function may one day become outdated on a website that isn't actively maintained anymore. Imagine a "promo-site" or a "personal website" being deployed once and then running for years without any maintenance, where a client may be unable to submit a simple "Contact Us" form just because this newly-allocated pool of mobile phone numbers wasn't present in that old version of `libphonenumber-js` that was used when building the website.
1587
+ When choosing between [`.isValid()`](#isvalid-boolean) and [`.isPossible()`](#ispossible-boolean), I'd personally prefer the latter. The rationale is that telephone numbering plans can and sometimes do change, meaning that [`.isValid()`](#isvalid-boolean) function may one day become outdated on a website that isn't actively maintained anymore. Imagine a "promo-site" or a "personal website" being deployed once and then running for years without any maintenance, where a client may be unable to submit a simple "Contact Us" form just because this newly-allocated pool of mobile phone numbers wasn't present in that old version of `libphonenumber-js` that was used when building the website.
1584
1588
 
1585
- Whenever there's a "business requirement" to validate phone number input, I prefer using [`PhoneNumber.isPossible()`](#ispossible-boolean) instead of [`PhoneNumber.isValid()`](#isvalid-boolean), so that it just validates the phone number length and doesn't validate the actual phone number digits. But it doesn't mean that you shouldn't use [`PhoneNumber.isValid()`](#isvalid-boolean) — maybe in your case it would make sense.
1589
+ Whenever there's a "business requirement" to validate phone number input, I prefer using [`.isPossible()`](#ispossible-boolean) instead of [`.isValid()`](#isvalid-boolean), so that it just validates the phone number length and doesn't validate the actual phone number digits. But it doesn't mean that you shouldn't use [`.isValid()`](#isvalid-boolean) — maybe in your case it would make sense. Just be warned that by using [`.isValid()`](#isvalid-boolean) in an application or on a website, you volunteer to keep it updated for the rest of its lifespan.
1586
1590
 
1587
1591
  ## React
1588
1592
 
@@ -49,24 +49,25 @@ function isPossiblePhoneNumber(input, options, metadata) {
49
49
  }
50
50
  }
51
51
 
52
- // Old metadata (< 1.0.18) had no "possible length" data.
52
+ // Old (legacy) metadata (< 1.0.18) had no "possible length" data.
53
+ // So `isPossibleNumber()` function is supported only for non-legacy metadata.
53
54
  if (metadata.possibleLengths()) {
54
55
  return isPossibleNumber(input.phone || input.nationalNumber, input.country, metadata);
55
- } else {
56
- // There was a bug between `1.7.35` and `1.7.37` where "possible_lengths"
57
- // were missing for "non-geographical" numbering plans.
58
- // Just assume the number is possible in such cases:
59
- // it's unlikely that anyone generated their custom metadata
60
- // in that short period of time (one day).
61
- // This code can be removed in some future major version update.
62
- if (input.countryCallingCode && metadata.isNonGeographicCallingCode(input.countryCallingCode)) {
63
- // "Non-geographic entities" did't have `possibleLengths`
64
- // due to a bug in metadata generation process.
65
- return true;
66
- } else {
67
- throw new Error('Missing "possibleLengths" in metadata. Perhaps the metadata has been generated before v1.0.18.');
68
- }
69
56
  }
57
+
58
+ // There was a bug in versions from `1.7.35` to `1.7.37` of `libphonenumber-js`
59
+ // where "possible_lengths" property was missing from "non-geographical" numbering plans' metadata.
60
+ // After that, the bug was noticed and fixed.
61
+ // So for versions from `1.7.35` to `1.7.37`, just assume that all "non=geotraphical" numbers are possible.
62
+ // The reason is that the bug was noticed relatively quickly (within a day or so)
63
+ // and it's unlikely that anyone generated their custom metadata in that short time span.
64
+ // And if they didn't generate any custom metadata then a follow-up package update would've silently fixed the bug.
65
+ if (input.countryCallingCode && metadata.isNonGeographicCallingCode(input.countryCallingCode)) {
66
+ return true;
67
+ }
68
+
69
+ // `isPossibleNumber()` function is not supported.
70
+ throw new Error('Missing "possibleLengths" in metadata. Perhaps the metadata has been generated before v1.0.18.');
70
71
  }
71
72
  function isPossibleNumber(nationalNumber, country, metadata) {
72
73
  //, isInternational) {
@@ -1 +1 @@
1
- {"version":3,"file":"isPossible.js","names":["_metadata","_interopRequireDefault","require","_checkNumberLength","e","__esModule","isPossiblePhoneNumber","input","options","metadata","undefined","Metadata","v2","countryCallingCode","Error","selectNumberingPlan","phone","country","hasCountry","concat","possibleLengths","isPossibleNumber","nationalNumber","isNonGeographicCallingCode","checkNumberLength"],"sources":["../source/isPossible.js"],"sourcesContent":["import Metadata from './metadata.js'\r\nimport checkNumberLength from './helpers/checkNumberLength.js'\r\n\r\n/**\r\n * Checks if a phone number is \"possible\" (basically just checks its length).\r\n *\r\n * isPossible(phoneNumberInstance, { ..., v2: true }, metadata)\r\n *\r\n * isPossible({ phone: '8005553535', country: 'RU' }, { ... }, metadata)\r\n * isPossible({ phone: '8005553535', country: 'RU' }, undefined, metadata)\r\n *\r\n * @param {object|PhoneNumber} input — If `options.v2: true` flag is passed, the `input` should be a `PhoneNumber` instance. Otherwise, it should be an object of shape `{ phone: '...', country: '...' }`.\r\n * @param {object} [options]\r\n * @param {object} metadata\r\n * @return {string}\r\n */\r\nexport default function isPossiblePhoneNumber(input, options, metadata) {\r\n\t/* istanbul ignore if */\r\n\tif (options === undefined) {\r\n\t\toptions = {}\r\n\t}\r\n\r\n\tmetadata = new Metadata(metadata)\r\n\r\n\tif (options.v2) {\r\n\t\tif (!input.countryCallingCode) {\r\n\t\t\tthrow new Error('Invalid phone number object passed')\r\n\t\t}\r\n\t\tmetadata.selectNumberingPlan(input.countryCallingCode)\r\n\t} else {\r\n\t\tif (!input.phone) {\r\n\t\t\treturn false\r\n\t\t}\r\n\t\tif (input.country) {\r\n\t\t\tif (!metadata.hasCountry(input.country)) {\r\n\t\t\t\tthrow new Error(`Unknown country: ${input.country}`)\r\n\t\t\t}\r\n\t\t\tmetadata.selectNumberingPlan(input.country)\r\n\t\t} else {\r\n\t\t\tif (!input.countryCallingCode) {\r\n\t\t\t\tthrow new Error('Invalid phone number object passed')\r\n\t\t\t}\r\n\t\t\tmetadata.selectNumberingPlan(input.countryCallingCode)\r\n\t\t}\r\n\t}\r\n\r\n\t// Old metadata (< 1.0.18) had no \"possible length\" data.\r\n\tif (metadata.possibleLengths()) {\r\n\t\treturn isPossibleNumber(input.phone || input.nationalNumber, input.country, metadata)\r\n\t} else {\r\n\t\t// There was a bug between `1.7.35` and `1.7.37` where \"possible_lengths\"\r\n\t\t// were missing for \"non-geographical\" numbering plans.\r\n\t\t// Just assume the number is possible in such cases:\r\n\t\t// it's unlikely that anyone generated their custom metadata\r\n\t\t// in that short period of time (one day).\r\n\t\t// This code can be removed in some future major version update.\r\n\t\tif (input.countryCallingCode && metadata.isNonGeographicCallingCode(input.countryCallingCode)) {\r\n\t\t\t// \"Non-geographic entities\" did't have `possibleLengths`\r\n\t\t\t// due to a bug in metadata generation process.\r\n\t\t\treturn true\r\n\t\t} else {\r\n\t\t\tthrow new Error('Missing \"possibleLengths\" in metadata. Perhaps the metadata has been generated before v1.0.18.');\r\n\t\t}\r\n\t}\r\n}\r\n\r\nexport function isPossibleNumber(nationalNumber, country, metadata) { //, isInternational) {\r\n\tswitch (checkNumberLength(nationalNumber, country, metadata)) {\r\n\t\tcase 'IS_POSSIBLE':\r\n\t\t\treturn true\r\n\t\t// This library ignores \"local-only\" phone numbers (for simplicity).\r\n\t\t// See the readme for more info on what are \"local-only\" phone numbers.\r\n\t\t// case 'IS_POSSIBLE_LOCAL_ONLY':\r\n\t\t// \treturn !isInternational\r\n\t\tdefault:\r\n\t\t\treturn false\r\n\t}\r\n}"],"mappings":";;;;;;;AAAA,IAAAA,SAAA,GAAAC,sBAAA,CAAAC,OAAA;AACA,IAAAC,kBAAA,GAAAF,sBAAA,CAAAC,OAAA;AAA8D,SAAAD,uBAAA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ACtF,CAAC,MAAM;IACN;IACA;IACA;IACA;IACA;IACA;IACA,IAAIF,KAAK,CAACM,kBAAkB,IAAIJ,QAAQ,CAACc,0BAA0B,CAAChB,KAAK,CAACM,kBAAkB,CAAC,EAAE;MAC9F;MACA;MACA,OAAO,IAAI;IACZ,CAAC,MAAM;MACN,MAAM,IAAIC,KAAK,CAAC,gGAAgG,CAAC;IAClH;EACD;AACD;AAEO,SAASO,gBAAgBA,CAACC,cAAc,EAAEL,OAAO,EAAER,QAAQ,EAAE;EAAE;EACrE,QAAQ,IAAAe,6BAAiB,EAACF,cAAc,EAAEL,OAAO,EAAER,QAAQ,CAAC;IAC3D,KAAK,aAAa;MACjB,OAAO,IAAI;IACZ;IACA;IACA;IACA;IACA;MACC,OAAO,KAAK;EACd;AACD","ignoreList":[]}
1
+ {"version":3,"file":"isPossible.js","names":["_metadata","_interopRequireDefault","require","_checkNumberLength","e","__esModule","isPossiblePhoneNumber","input","options","metadata","undefined","Metadata","v2","countryCallingCode","Error","selectNumberingPlan","phone","country","hasCountry","concat","possibleLengths","isPossibleNumber","nationalNumber","isNonGeographicCallingCode","checkNumberLength"],"sources":["../source/isPossible.js"],"sourcesContent":["import Metadata from './metadata.js'\r\nimport checkNumberLength from './helpers/checkNumberLength.js'\r\n\r\n/**\r\n * Checks if a phone number is \"possible\" (basically just checks its length).\r\n *\r\n * isPossible(phoneNumberInstance, { ..., v2: true }, metadata)\r\n *\r\n * isPossible({ phone: '8005553535', country: 'RU' }, { ... }, metadata)\r\n * isPossible({ phone: '8005553535', country: 'RU' }, undefined, metadata)\r\n *\r\n * @param {object|PhoneNumber} input — If `options.v2: true` flag is passed, the `input` should be a `PhoneNumber` instance. Otherwise, it should be an object of shape `{ phone: '...', country: '...' }`.\r\n * @param {object} [options]\r\n * @param {object} metadata\r\n * @return {string}\r\n */\r\nexport default function isPossiblePhoneNumber(input, options, metadata) {\r\n\t/* istanbul ignore if */\r\n\tif (options === undefined) {\r\n\t\toptions = {}\r\n\t}\r\n\r\n\tmetadata = new Metadata(metadata)\r\n\r\n\tif (options.v2) {\r\n\t\tif (!input.countryCallingCode) {\r\n\t\t\tthrow new Error('Invalid phone number object passed')\r\n\t\t}\r\n\t\tmetadata.selectNumberingPlan(input.countryCallingCode)\r\n\t} else {\r\n\t\tif (!input.phone) {\r\n\t\t\treturn false\r\n\t\t}\r\n\t\tif (input.country) {\r\n\t\t\tif (!metadata.hasCountry(input.country)) {\r\n\t\t\t\tthrow new Error(`Unknown country: ${input.country}`)\r\n\t\t\t}\r\n\t\t\tmetadata.selectNumberingPlan(input.country)\r\n\t\t} else {\r\n\t\t\tif (!input.countryCallingCode) {\r\n\t\t\t\tthrow new Error('Invalid phone number object passed')\r\n\t\t\t}\r\n\t\t\tmetadata.selectNumberingPlan(input.countryCallingCode)\r\n\t\t}\r\n\t}\r\n\r\n\t// Old (legacy) metadata (< 1.0.18) had no \"possible length\" data.\r\n\t// So `isPossibleNumber()` function is supported only for non-legacy metadata.\r\n\tif (metadata.possibleLengths()) {\r\n\t\treturn isPossibleNumber(input.phone || input.nationalNumber, input.country, metadata)\r\n\t}\r\n\r\n\t// There was a bug in versions from `1.7.35` to `1.7.37` of `libphonenumber-js`\r\n\t// where \"possible_lengths\" property was missing from \"non-geographical\" numbering plans' metadata.\r\n\t// After that, the bug was noticed and fixed.\r\n\t// So for versions from `1.7.35` to `1.7.37`, just assume that all \"non=geotraphical\" numbers are possible.\r\n\t// The reason is that the bug was noticed relatively quickly (within a day or so)\r\n\t// and it's unlikely that anyone generated their custom metadata in that short time span.\r\n\t// And if they didn't generate any custom metadata then a follow-up package update would've silently fixed the bug.\r\n\tif (input.countryCallingCode && metadata.isNonGeographicCallingCode(input.countryCallingCode)) {\r\n\t\treturn true\r\n\t}\r\n\r\n\t// `isPossibleNumber()` function is not supported.\r\n\tthrow new Error('Missing \"possibleLengths\" in metadata. Perhaps the metadata has been generated before v1.0.18.');\r\n}\r\n\r\nexport function isPossibleNumber(nationalNumber, country, metadata) { //, isInternational) {\r\n\tswitch (checkNumberLength(nationalNumber, country, metadata)) {\r\n\t\tcase 'IS_POSSIBLE':\r\n\t\t\treturn true\r\n\t\t// This library ignores \"local-only\" phone numbers (for simplicity).\r\n\t\t// See the readme for more info on what are \"local-only\" phone numbers.\r\n\t\t// case 'IS_POSSIBLE_LOCAL_ONLY':\r\n\t\t// \treturn !isInternational\r\n\t\tdefault:\r\n\t\t\treturn false\r\n\t}\r\n}"],"mappings":";;;;;;;AAAA,IAAAA,SAAA,GAAAC,sBAAA,CAAAC,OAAA;AACA,IAAAC,kBAAA,GAAAF,sBAAA,CAAAC,OAAA;AAA8D,SAAAD,uBAAAG,CAAA,WAAAA,CAAA,IAAAA,CAAA,CAAAC,UAAA,GAAAD,CAAA,gBAAAA,CAAA;AAE9D;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACe,SAASE,qBAAqBA,CAACC,KAAK,EAAEC,OAAO,EAAEC,QAAQ,EAAE;EACvE;EACA,IAAID,OAAO,KAAKE,SAAS,EAAE;IAC1BF,OAAO,GAAG,CAAC,CAAC;EACb;EAEAC,QAAQ,GAAG,IAAIE,oBAAQ,CAACF,QAAQ,CAAC;EAEjC,IAAID,OAAO,CAACI,EAAE,EAAE;IACf,IAAI,CAACL,KAAK,CAACM,kBAAkB,EAAE;MAC9B,MAAM,IAAIC,KAAK,CAAC,oCAAoC,CAAC;IACtD;IACAL,QAAQ,CAACM,mBAAmB,CAACR,KAAK,CAACM,kBAAkB,CAAC;EACvD,CAAC,MAAM;IACN,IAAI,CAACN,KAAK,CAACS,KAAK,EAAE;MACjB,OAAO,KAAK;IACb;IACA,IAAIT,KAAK,CAACU,OAAO,EAAE;MAClB,IAAI,CAACR,QAAQ,CAACS,UAAU,CAACX,KAAK,CAACU,OAAO,CAAC,EAAE;QACxC,MAAM,IAAIH,KAAK,qBAAAK,MAAA,CAAqBZ,KAAK,CAACU,OAAO,CAAE,CAAC;MACrD;MACAR,QAAQ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