libphonenumber-js 1.9.46 → 1.9.47

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.
Files changed (39) hide show
  1. package/build/AsYouType.test.js +10 -0
  2. package/build/AsYouType.test.js.map +1 -1
  3. package/build/AsYouTypeFormatter.PatternMatcher.js +53 -16
  4. package/build/AsYouTypeFormatter.PatternMatcher.js.map +1 -1
  5. package/build/AsYouTypeFormatter.PatternMatcher.test.js +46 -0
  6. package/build/AsYouTypeFormatter.PatternMatcher.test.js.map +1 -1
  7. package/build/AsYouTypeFormatter.js +3 -1
  8. package/build/AsYouTypeFormatter.js.map +1 -1
  9. package/bundle/libphonenumber-js.min.js +1 -1
  10. package/bundle/libphonenumber-js.min.js.map +1 -1
  11. package/bundle/libphonenumber-max.js +1 -1
  12. package/bundle/libphonenumber-max.js.map +1 -1
  13. package/bundle/libphonenumber-min.js +1 -1
  14. package/bundle/libphonenumber-min.js.map +1 -1
  15. package/bundle/libphonenumber-mobile.js +1 -1
  16. package/bundle/libphonenumber-mobile.js.map +1 -1
  17. package/es6/AsYouType.test.js +10 -0
  18. package/es6/AsYouType.test.js.map +1 -1
  19. package/es6/AsYouTypeFormatter.PatternMatcher.js +53 -16
  20. package/es6/AsYouTypeFormatter.PatternMatcher.js.map +1 -1
  21. package/es6/AsYouTypeFormatter.PatternMatcher.test.js +46 -0
  22. package/es6/AsYouTypeFormatter.PatternMatcher.test.js.map +1 -1
  23. package/es6/AsYouTypeFormatter.js +3 -1
  24. package/es6/AsYouTypeFormatter.js.map +1 -1
  25. package/examples.mobile.json +1 -1
  26. package/examples.mobile.json.js +1 -1
  27. package/metadata.full.json +1 -1
  28. package/metadata.full.json.js +1 -1
  29. package/metadata.max.json +1 -1
  30. package/metadata.max.json.js +1 -1
  31. package/metadata.min.json +1 -1
  32. package/metadata.min.json.js +1 -1
  33. package/metadata.mobile.json +1 -1
  34. package/metadata.mobile.json.js +1 -1
  35. package/package.json +1 -1
  36. package/source/AsYouType.test.js +11 -0
  37. package/source/AsYouTypeFormatter.PatternMatcher.js +43 -13
  38. package/source/AsYouTypeFormatter.PatternMatcher.test.js +37 -0
  39. package/source/AsYouTypeFormatter.js +1 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "libphonenumber-js",
3
- "version": "1.9.46",
3
+ "version": "1.9.47",
4
4
  "description": "A simpler (and smaller) rewrite of Google Android's libphonenumber library in javascript",
5
5
  "main": "index.common.js",
6
6
  "module": "index.es6.js",
@@ -1172,6 +1172,17 @@ describe('AsYouType.getNumberValue()', () => {
1172
1172
  formatter.getNumberValue().should.equal('+121337342534')
1173
1173
  })
1174
1174
 
1175
+ it('should return E.164 number value (international number, not a valid calling code)', () => {
1176
+ const formatter = new AsYouType()
1177
+ expect(formatter.getNumberValue()).to.be.undefined
1178
+ formatter.input('')
1179
+ expect(formatter.getNumberValue()).to.be.undefined
1180
+ formatter.input('+')
1181
+ expect(formatter.getNumberValue()).to.be.undefined
1182
+ formatter.input('2150')
1183
+ formatter.getNumberValue().should.equal('+2150')
1184
+ })
1185
+
1175
1186
  it('should return E.164 number value (international number, default country: US)', () => {
1176
1187
  const formatter = new AsYouType('US')
1177
1188
  expect(formatter.getNumberValue()).to.be.undefined
@@ -1,7 +1,6 @@
1
1
  export default class PatternMatcher {
2
2
  constructor(pattern) {
3
3
  this.matchTree = new PatternParser().parse(pattern)
4
- // console.log(JSON.stringify(this.matchTree, null, 2))
5
4
  }
6
5
 
7
6
  match(string, { allowOverflow } = {}) {
@@ -24,6 +23,7 @@ export default class PatternMatcher {
24
23
  function match(characters, tree, last) {
25
24
  if (typeof tree === 'string') {
26
25
  if (last) {
26
+ // `tree` is always a single character.
27
27
  if (characters.length > tree.length) {
28
28
  return {
29
29
  overflow: true
@@ -32,12 +32,20 @@ function match(characters, tree, last) {
32
32
  }
33
33
  const characterString = characters.join('')
34
34
  if (tree.indexOf(characterString) === 0) {
35
+ // `tree` is always a single character.
36
+ // If `tree.indexOf(characterString) === 0`
37
+ // then `characters.length === tree.length`.
38
+ /* istanbul ignore else */
35
39
  if (characters.length === tree.length) {
36
40
  return {
37
41
  match: true,
38
42
  matchedChars: characters
39
43
  }
40
44
  }
45
+ // `tree` is always a single character.
46
+ // If `tree.indexOf(characterString) === 0`
47
+ // then `characters.length === tree.length`.
48
+ /* istanbul ignore next */
41
49
  return {
42
50
  partialMatch: true,
43
51
  // matchedChars: characters
@@ -58,10 +66,11 @@ function match(characters, tree, last) {
58
66
  while (i < tree.length) {
59
67
  const subtree = tree[i]
60
68
  const result = match(restCharacters, subtree, last && (i === tree.length - 1))
61
- if (!result || result.overflow) {
69
+ if (!result) {
70
+ return
71
+ } else if (result.overflow) {
62
72
  return result
63
- }
64
- if (result.match) {
73
+ } else if (result.match) {
65
74
  // Continue with the next subtree with the rest of the characters.
66
75
  restCharacters = restCharacters.slice(result.matchedChars.length)
67
76
  if (restCharacters.length === 0) {
@@ -77,16 +86,22 @@ function match(characters, tree, last) {
77
86
  }
78
87
  }
79
88
  }
80
- } else if (result.partialMatch) {
81
- return {
82
- partialMatch: true,
83
- // matchedChars: characters
84
- }
85
89
  } else {
86
- throw new Error(`Unsupported match result:\n${JSON.stringify(result, null, 2)}`)
90
+ /* istanbul ignore else */
91
+ if (result.partialMatch) {
92
+ return {
93
+ partialMatch: true,
94
+ // matchedChars: characters
95
+ }
96
+ } else {
97
+ throw new Error(`Unsupported match result:\n${JSON.stringify(result, null, 2)}`)
98
+ }
87
99
  }
88
100
  i++
89
101
  }
102
+ // If `last` then overflow has already been checked
103
+ // by the last element of the `tree` array.
104
+ /* istanbul ignore if */
90
105
  if (last) {
91
106
  return {
92
107
  overflow: true
@@ -111,8 +126,13 @@ function match(characters, tree, last) {
111
126
  match: true,
112
127
  matchedChars: result.matchedChars
113
128
  }
114
- } else if (result.partialMatch) {
115
- partialMatch = true
129
+ } else {
130
+ /* istanbul ignore else */
131
+ if (result.partialMatch) {
132
+ partialMatch = true
133
+ } else {
134
+ throw new Error(`Unsupported match result:\n${JSON.stringify(result, null, 2)}`)
135
+ }
116
136
  }
117
137
  }
118
138
  }
@@ -148,6 +168,7 @@ function match(characters, tree, last) {
148
168
  // No character matches.
149
169
  return
150
170
 
171
+ /* istanbul ignore next */
151
172
  default:
152
173
  throw new Error(`Unsupported instruction tree: ${tree}`)
153
174
  }
@@ -177,6 +198,8 @@ const OPERATOR = new RegExp(
177
198
  ')'
178
199
  )
179
200
 
201
+ const ILLEGAL_CHARACTER_REGEXP = /[\(\)\[\]\?\:\|]/
202
+
180
203
  class PatternParser {
181
204
  parse(pattern) {
182
205
  this.context = [{
@@ -199,6 +222,7 @@ class PatternParser {
199
222
  }]
200
223
  }
201
224
 
225
+ /* istanbul ignore if */
202
226
  if (instructions.length === 0) {
203
227
  throw new Error('Pattern is required')
204
228
  }
@@ -220,11 +244,14 @@ class PatternParser {
220
244
 
221
245
  parsePattern(pattern) {
222
246
  if (!pattern) {
223
- throw new Error('Empty pattern passed')
247
+ throw new Error('Pattern is required')
224
248
  }
225
249
 
226
250
  const match = pattern.match(OPERATOR)
227
251
  if (!match) {
252
+ if (ILLEGAL_CHARACTER_REGEXP.test(pattern)) {
253
+ throw new Error(`Illegal characters found in a pattern: ${pattern}`)
254
+ }
228
255
  this.getContext().instructions = this.getContext().instructions.concat(
229
256
  pattern.split('')
230
257
  )
@@ -277,6 +304,8 @@ class PatternParser {
277
304
  }
278
305
  // The top-level is an implicit "or" group, if required.
279
306
  if (!this.getContext().branches) {
307
+ // `branches` are not defined only for the root implicit "or" operator.
308
+ /* istanbul ignore else */
280
309
  if (this.context.length === 1) {
281
310
  this.getContext().branches = []
282
311
  } else {
@@ -309,6 +338,7 @@ class PatternParser {
309
338
  })
310
339
  break
311
340
 
341
+ /* istanbul ignore next */
312
342
  default:
313
343
  throw new Error(`Unknown operator: ${operator}`)
314
344
  }
@@ -1,6 +1,43 @@
1
1
  import PatternMatcher from './AsYouTypeFormatter.PatternMatcher'
2
2
 
3
3
  describe('AsYouTypeFormatter.PatternMatcher', function() {
4
+ it('should throw when no pattern is passed', function() {
5
+ expect(() => new PatternMatcher()).to.throw('Pattern is required')
6
+ })
7
+
8
+ it('should throw when no string is passed', function() {
9
+ const matcher = new PatternMatcher('1')
10
+ expect(() => matcher.match()).to.throw('String is required')
11
+ })
12
+
13
+ it('should throw on illegal characters', function() {
14
+ expect(() => new PatternMatcher('4(5|6)7')).to.throw('Illegal characters')
15
+ })
16
+
17
+ it('should throw on an illegal ] operator', function() {
18
+ expect(() => new PatternMatcher('4]7')).to.throw('"]" operator must be preceded by "[" operator')
19
+ })
20
+
21
+ it('should throw on an illegal - operator in a one-of set', function() {
22
+ expect(() => new PatternMatcher('[-5]')).to.throw('Couldn\'t parse a one-of set pattern: -5')
23
+ })
24
+
25
+ it('should throw on a non-finalized context', function() {
26
+ expect(() => new PatternMatcher('4(?:5|7')).to.throw('Non-finalized contexts left when pattern parse ended')
27
+ })
28
+
29
+ it('should throw on an illegal (|) operator', function() {
30
+ expect(() => new PatternMatcher('4(?:5|)7')).to.throw('No instructions found after "|" operator in an "or" group')
31
+ })
32
+
33
+ it('should throw on an illegal ) operator', function() {
34
+ expect(() => new PatternMatcher('4[56)]7')).to.throw('")" operator must be preceded by "(?:" operator')
35
+ })
36
+
37
+ it('should throw on an illegal | operator', function() {
38
+ expect(() => new PatternMatcher('4[5|6]7')).to.throw('operator can only be used inside "or" groups')
39
+ })
40
+
4
41
  it('should match a one-digit pattern', function() {
5
42
  const matcher = new PatternMatcher('4')
6
43
 
@@ -323,7 +323,7 @@ export default class AsYouTypeFormatter {
323
323
  // After leading digits < 3 matching was implemented:
324
324
  try {
325
325
  return new PatternMatcher(leadingDigitsPattern).match(leadingDigits, { allowOverflow: true }) !== undefined
326
- } catch (error) {
326
+ } catch (error) /* istanbul ignore next */ {
327
327
  // There's a slight possibility that there could be some undiscovered bug
328
328
  // in the pattern matcher code. Since the "leading digits < 3 matching"
329
329
  // feature is not "essential" for operation, it can fall back to the old way