libphonenumber-js 1.9.25 → 1.9.26
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 +16 -0
- package/README.md +55 -5
- package/build/helpers/isViablePhoneNumber.js +22 -1
- package/build/helpers/isViablePhoneNumber.js.map +1 -1
- package/build/parsePhoneNumber.test.js +14 -4
- package/build/parsePhoneNumber.test.js.map +1 -1
- package/build/parse_.js +20 -3
- package/build/parse_.js.map +1 -1
- package/build/validatePhoneNumberLength.js +53 -0
- package/build/validatePhoneNumberLength.js.map +1 -0
- package/build/validatePhoneNumberLength.test.js +37 -0
- package/build/validatePhoneNumberLength.test.js.map +1 -0
- package/bundle/libphonenumber-js.min.js +1 -1
- package/bundle/libphonenumber-js.min.js.map +1 -1
- package/bundle/libphonenumber-max.js +1 -1
- package/bundle/libphonenumber-max.js.map +1 -1
- package/bundle/libphonenumber-min.js +1 -1
- package/bundle/libphonenumber-min.js.map +1 -1
- package/bundle/libphonenumber-mobile.js +1 -1
- package/bundle/libphonenumber-mobile.js.map +1 -1
- package/core/index.commonjs.js +1 -0
- package/core/index.d.ts +7 -2
- package/core/index.js +1 -0
- package/es6/helpers/isViablePhoneNumber.js +20 -1
- package/es6/helpers/isViablePhoneNumber.js.map +1 -1
- package/es6/parsePhoneNumber.test.js +14 -4
- package/es6/parsePhoneNumber.test.js.map +1 -1
- package/es6/parse_.js +18 -3
- package/es6/parse_.js.map +1 -1
- package/es6/validatePhoneNumberLength.js +39 -0
- package/es6/validatePhoneNumberLength.js.map +1 -0
- package/es6/validatePhoneNumberLength.test.js +32 -0
- package/es6/validatePhoneNumberLength.test.js.map +1 -0
- package/index.common.js +4 -0
- package/index.d.ts +5 -2
- package/index.es6.js +1 -0
- package/max/exports/validatePhoneNumberLength.js +6 -0
- package/max/index.commonjs.js +4 -0
- package/max/index.js +1 -0
- package/min/exports/validatePhoneNumberLength.js +6 -0
- package/min/index.commonjs.js +4 -0
- package/min/index.d.ts +5 -2
- package/min/index.js +1 -0
- package/mobile/exports/validatePhoneNumberLength.js +6 -0
- package/mobile/index.commonjs.js +4 -0
- package/mobile/index.js +1 -0
- package/package.json +1 -1
- package/source/helpers/isViablePhoneNumber.js +29 -0
- package/source/parsePhoneNumber.test.js +5 -3
- package/source/parse_.js +12 -3
- package/source/validatePhoneNumberLength.js +31 -0
- package/source/validatePhoneNumberLength.test.js +30 -0
- package/types.d.ts +4 -0
|
@@ -47,6 +47,25 @@ export const VALID_PHONE_NUMBER =
|
|
|
47
47
|
VALID_DIGITS +
|
|
48
48
|
']*'
|
|
49
49
|
|
|
50
|
+
// This regular expression isn't present in Google's `libphonenumber`
|
|
51
|
+
// and is only used to determine whether the phone number being input
|
|
52
|
+
// is too short for it to even consider it a "valid" number.
|
|
53
|
+
// This is just a way to differentiate between a really invalid phone
|
|
54
|
+
// number like "abcde" and a valid phone number that a user has just
|
|
55
|
+
// started inputting, like "+1" or "1": both these cases would be
|
|
56
|
+
// considered `NOT_A_NUMBER` by Google's `libphonenumber`, but this
|
|
57
|
+
// library can provide a more detailed error message — whether it's
|
|
58
|
+
// really "not a number", or is it just a start of a valid phone number.
|
|
59
|
+
const VALID_PHONE_NUMBER_START_REG_EXP = new RegExp(
|
|
60
|
+
'^' +
|
|
61
|
+
'[' + PLUS_CHARS + ']{0,1}' +
|
|
62
|
+
'(?:' +
|
|
63
|
+
'[' + VALID_PUNCTUATION + ']*' +
|
|
64
|
+
'[' + VALID_DIGITS + ']' +
|
|
65
|
+
'){1,2}' +
|
|
66
|
+
'$'
|
|
67
|
+
, 'i')
|
|
68
|
+
|
|
50
69
|
export const VALID_PHONE_NUMBER_WITH_EXTENSION =
|
|
51
70
|
VALID_PHONE_NUMBER +
|
|
52
71
|
// Phone number extensions
|
|
@@ -76,4 +95,14 @@ const VALID_PHONE_NUMBER_PATTERN = new RegExp(
|
|
|
76
95
|
export default function isViablePhoneNumber(number) {
|
|
77
96
|
return number.length >= MIN_LENGTH_FOR_NSN &&
|
|
78
97
|
VALID_PHONE_NUMBER_PATTERN.test(number)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// This is just a way to differentiate between a really invalid phone
|
|
101
|
+
// number like "abcde" and a valid phone number that a user has just
|
|
102
|
+
// started inputting, like "+1" or "1": both these cases would be
|
|
103
|
+
// considered `NOT_A_NUMBER` by Google's `libphonenumber`, but this
|
|
104
|
+
// library can provide a more detailed error message — whether it's
|
|
105
|
+
// really "not a number", or is it just a start of a valid phone number.
|
|
106
|
+
export function isViablePhoneNumberStart(number) {
|
|
107
|
+
return VALID_PHONE_NUMBER_START_REG_EXP.test(number)
|
|
79
108
|
}
|
|
@@ -67,9 +67,11 @@ describe('parsePhoneNumber', () => {
|
|
|
67
67
|
|
|
68
68
|
it('should throw parse errors', () => {
|
|
69
69
|
expect(() => parsePhoneNumber('8005553535', 'XX')).to.throw('INVALID_COUNTRY')
|
|
70
|
-
expect(() => parsePhoneNumber('
|
|
71
|
-
|
|
72
|
-
|
|
70
|
+
expect(() => parsePhoneNumber('+', 'RU')).to.throw('NOT_A_NUMBER')
|
|
71
|
+
expect(() => parsePhoneNumber('a', 'RU')).to.throw('NOT_A_NUMBER')
|
|
72
|
+
expect(() => parsePhoneNumber('1', 'RU')).to.throw('TOO_SHORT')
|
|
73
|
+
expect(() => parsePhoneNumber('+4')).to.throw('TOO_SHORT')
|
|
74
|
+
expect(() => parsePhoneNumber('+44')).to.throw('TOO_SHORT')
|
|
73
75
|
expect(() => parsePhoneNumber('+443')).to.throw('TOO_SHORT')
|
|
74
76
|
expect(() => parsePhoneNumber('+370')).to.throw('TOO_SHORT')
|
|
75
77
|
expect(() => parsePhoneNumber('88888888888888888888', 'RU')).to.throw('TOO_LONG')
|
package/source/parse_.js
CHANGED
|
@@ -12,7 +12,7 @@ import {
|
|
|
12
12
|
|
|
13
13
|
import ParseError from './ParseError'
|
|
14
14
|
import Metadata from './metadata'
|
|
15
|
-
import isViablePhoneNumber from './helpers/isViablePhoneNumber'
|
|
15
|
+
import isViablePhoneNumber, { isViablePhoneNumberStart } from './helpers/isViablePhoneNumber'
|
|
16
16
|
import extractExtension from './helpers/extension/extractExtension'
|
|
17
17
|
import parseIncompletePhoneNumber from './parseIncompletePhoneNumber'
|
|
18
18
|
import getCountryCallingCode from './getCountryCallingCode'
|
|
@@ -67,11 +67,14 @@ export default function parse(text, options, metadata) {
|
|
|
67
67
|
}
|
|
68
68
|
|
|
69
69
|
// Parse the phone number.
|
|
70
|
-
const { number: formattedPhoneNumber, ext } = parseInput(text, options.v2, options.extract)
|
|
70
|
+
const { number: formattedPhoneNumber, ext, error } = parseInput(text, options.v2, options.extract)
|
|
71
71
|
|
|
72
72
|
// If the phone number is not viable then return nothing.
|
|
73
73
|
if (!formattedPhoneNumber) {
|
|
74
74
|
if (options.v2) {
|
|
75
|
+
if (error === 'TOO_SHORT') {
|
|
76
|
+
throw new ParseError('TOO_SHORT')
|
|
77
|
+
}
|
|
75
78
|
throw new ParseError('NOT_A_NUMBER')
|
|
76
79
|
}
|
|
77
80
|
return {}
|
|
@@ -220,7 +223,13 @@ function parseInput(text, v2, extract) {
|
|
|
220
223
|
}
|
|
221
224
|
let number = extractFormattedPhoneNumber(text, extract, v2)
|
|
222
225
|
// If the phone number is not viable, then abort.
|
|
223
|
-
if (!number
|
|
226
|
+
if (!number) {
|
|
227
|
+
return {}
|
|
228
|
+
}
|
|
229
|
+
if (!isViablePhoneNumber(number)) {
|
|
230
|
+
if (isViablePhoneNumberStart(number)) {
|
|
231
|
+
return { error: 'TOO_SHORT' }
|
|
232
|
+
}
|
|
224
233
|
return {}
|
|
225
234
|
}
|
|
226
235
|
// Attempt to parse extension first, since it doesn't require region-specific
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { normalizeArguments } from './parsePhoneNumber'
|
|
2
|
+
import parsePhoneNumber from './parsePhoneNumber_'
|
|
3
|
+
import ParseError from './ParseError'
|
|
4
|
+
import Metadata from './metadata'
|
|
5
|
+
import checkNumberLength from './helpers/checkNumberLength'
|
|
6
|
+
|
|
7
|
+
export default function validatePhoneNumberLength() {
|
|
8
|
+
let { text, options, metadata } = normalizeArguments(arguments)
|
|
9
|
+
options = {
|
|
10
|
+
...options,
|
|
11
|
+
extract: false
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// Parse phone number.
|
|
15
|
+
try {
|
|
16
|
+
const phoneNumber = parsePhoneNumber(text, options, metadata)
|
|
17
|
+
metadata = new Metadata(metadata)
|
|
18
|
+
metadata.selectNumberingPlan(phoneNumber.countryCallingCode)
|
|
19
|
+
const result = checkNumberLength(phoneNumber.nationalNumber, metadata)
|
|
20
|
+
if (result !== 'IS_POSSIBLE') {
|
|
21
|
+
return result
|
|
22
|
+
}
|
|
23
|
+
} catch (error) {
|
|
24
|
+
/* istanbul ignore else */
|
|
25
|
+
if (error instanceof ParseError) {
|
|
26
|
+
return error.message
|
|
27
|
+
} else {
|
|
28
|
+
throw error
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import _validatePhoneNumberLength from './validatePhoneNumberLength'
|
|
2
|
+
import metadata from '../metadata.min.json'
|
|
3
|
+
|
|
4
|
+
function validatePhoneNumberLength(...parameters) {
|
|
5
|
+
parameters.push(metadata)
|
|
6
|
+
return _validatePhoneNumberLength.apply(this, parameters)
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
describe('validatePhoneNumberLength', () => {
|
|
10
|
+
it('should detect whether a phone number length is valid', () => {
|
|
11
|
+
// Not a phone number.
|
|
12
|
+
validatePhoneNumberLength('+').should.equal('NOT_A_NUMBER')
|
|
13
|
+
validatePhoneNumberLength('abcde').should.equal('NOT_A_NUMBER')
|
|
14
|
+
|
|
15
|
+
// No country supplied for a national number.
|
|
16
|
+
validatePhoneNumberLength('123').should.equal('INVALID_COUNTRY')
|
|
17
|
+
|
|
18
|
+
// Too short while the number is not considered "viable"
|
|
19
|
+
// by Google's `libphonenumber`.
|
|
20
|
+
validatePhoneNumberLength('2', 'US').should.equal('TOO_SHORT')
|
|
21
|
+
validatePhoneNumberLength('+1', 'US').should.equal('TOO_SHORT')
|
|
22
|
+
validatePhoneNumberLength('+12', 'US').should.equal('TOO_SHORT')
|
|
23
|
+
|
|
24
|
+
// Test national (significant) number length.
|
|
25
|
+
validatePhoneNumberLength('444 1 44', 'TR').should.equal('TOO_SHORT')
|
|
26
|
+
expect(validatePhoneNumberLength('444 1 444', 'TR')).to.be.undefined
|
|
27
|
+
validatePhoneNumberLength('444 1 4444', 'TR').should.equal('INVALID_LENGTH')
|
|
28
|
+
validatePhoneNumberLength('444 1 4444444444', 'TR').should.equal('TOO_LONG')
|
|
29
|
+
})
|
|
30
|
+
})
|
package/types.d.ts
CHANGED
|
@@ -91,3 +91,7 @@ export interface NumberFoundLegacy {
|
|
|
91
91
|
export class ParseError {
|
|
92
92
|
message: string;
|
|
93
93
|
}
|
|
94
|
+
|
|
95
|
+
// The rationale for having a separate type for the result "enum" instead of just a `string`:
|
|
96
|
+
// https://github.com/catamphetamine/libphonenumber-js/issues/170#issuecomment-363156068
|
|
97
|
+
export type ValidatePhoneNumberLengthResult = 'INVALID_COUNTRY' | 'NOT_A_NUMBER' | 'TOO_SHORT' | 'TOO_LONG' | 'INVALID_LENGTH';
|