libphonenumber-js 1.9.42 → 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.
- package/CHANGELOG.md +5 -0
- package/README.md +10 -2
- package/build/AsYouType.js +89 -32
- package/build/AsYouType.js.map +1 -1
- package/build/AsYouType.test.js +230 -0
- package/build/AsYouType.test.js.map +1 -1
- package/build/AsYouTypeFormatter.PatternMatcher.js +53 -16
- package/build/AsYouTypeFormatter.PatternMatcher.js.map +1 -1
- package/build/AsYouTypeFormatter.PatternMatcher.test.js +46 -0
- package/build/AsYouTypeFormatter.PatternMatcher.test.js.map +1 -1
- package/build/AsYouTypeFormatter.js +3 -1
- package/build/AsYouTypeFormatter.js.map +1 -1
- 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.d.ts +1 -0
- package/custom.d.ts +1 -0
- package/es6/AsYouType.js +89 -32
- package/es6/AsYouType.js.map +1 -1
- package/es6/AsYouType.test.js +230 -0
- package/es6/AsYouType.test.js.map +1 -1
- package/es6/AsYouTypeFormatter.PatternMatcher.js +53 -16
- package/es6/AsYouTypeFormatter.PatternMatcher.js.map +1 -1
- package/es6/AsYouTypeFormatter.PatternMatcher.test.js +46 -0
- package/es6/AsYouTypeFormatter.PatternMatcher.test.js.map +1 -1
- package/es6/AsYouTypeFormatter.js +3 -1
- package/es6/AsYouTypeFormatter.js.map +1 -1
- package/examples.mobile.json +1 -1
- package/examples.mobile.json.js +1 -1
- package/exports/README.md +27 -7
- package/index.d.ts +1 -0
- package/metadata.full.json +1 -1
- package/metadata.full.json.js +1 -1
- package/metadata.max.json +1 -1
- package/metadata.max.json.js +1 -1
- package/metadata.min.json +1 -1
- package/metadata.min.json.js +1 -1
- package/metadata.mobile.json +1 -1
- package/metadata.mobile.json.js +1 -1
- package/min/index.d.ts +1 -0
- package/package.json +1 -1
- package/source/AsYouType.js +90 -28
- package/source/AsYouType.test.js +234 -0
- package/source/AsYouTypeFormatter.PatternMatcher.js +43 -13
- package/source/AsYouTypeFormatter.PatternMatcher.test.js +37 -0
- package/source/AsYouTypeFormatter.js +1 -1
package/min/index.d.ts
CHANGED
package/package.json
CHANGED
package/source/AsYouType.js
CHANGED
|
@@ -132,13 +132,23 @@ export default class AsYouType {
|
|
|
132
132
|
}
|
|
133
133
|
|
|
134
134
|
/**
|
|
135
|
-
* Returns the "
|
|
136
|
-
*
|
|
137
|
-
*
|
|
135
|
+
* Returns the "calling code" part of the phone number when it's being input
|
|
136
|
+
* in an international format.
|
|
137
|
+
* If no valid calling code has been entered so far, returns `undefined`.
|
|
138
138
|
* @return {string} [callingCode]
|
|
139
139
|
*/
|
|
140
140
|
getCallingCode() {
|
|
141
|
-
|
|
141
|
+
// If the number is being input in national format and some "default calling code"
|
|
142
|
+
// has been passed to `AsYouType` constructor, then `this.state.callingCode`
|
|
143
|
+
// is equal to that "default calling code".
|
|
144
|
+
//
|
|
145
|
+
// If the number is being input in national format and no "default calling code"
|
|
146
|
+
// has been passed to `AsYouType` constructor, then returns `undefined`,
|
|
147
|
+
// even if a "default country" has been passed to `AsYouType` constructor.
|
|
148
|
+
//
|
|
149
|
+
if (this.isInternational()) {
|
|
150
|
+
return this.state.callingCode
|
|
151
|
+
}
|
|
142
152
|
}
|
|
143
153
|
|
|
144
154
|
// A legacy alias.
|
|
@@ -153,23 +163,29 @@ export default class AsYouType {
|
|
|
153
163
|
* @return {string} [country]
|
|
154
164
|
*/
|
|
155
165
|
getCountry() {
|
|
156
|
-
const { digits
|
|
157
|
-
//
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
if (!digits) {
|
|
161
|
-
return
|
|
166
|
+
const { digits } = this.state
|
|
167
|
+
// Return `undefined` if no digits have been input yet.
|
|
168
|
+
if (digits) {
|
|
169
|
+
return this._getCountry()
|
|
162
170
|
}
|
|
163
|
-
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Returns a two-letter country code of the phone number.
|
|
175
|
+
* Returns `undefined` for "non-geographic" phone numbering plans.
|
|
176
|
+
* @return {string} [country]
|
|
177
|
+
*/
|
|
178
|
+
_getCountry() {
|
|
179
|
+
const { country } = this.state
|
|
164
180
|
/* istanbul ignore if */
|
|
165
181
|
if (USE_NON_GEOGRAPHIC_COUNTRY_CODE) {
|
|
166
182
|
// `AsYouType.getCountry()` returns `undefined`
|
|
167
183
|
// for "non-geographic" phone numbering plans.
|
|
168
|
-
if (
|
|
169
|
-
|
|
184
|
+
if (country === '001') {
|
|
185
|
+
return
|
|
170
186
|
}
|
|
171
187
|
}
|
|
172
|
-
return
|
|
188
|
+
return country
|
|
173
189
|
}
|
|
174
190
|
|
|
175
191
|
determineTheCountryIfNeeded() {
|
|
@@ -259,6 +275,51 @@ export default class AsYouType {
|
|
|
259
275
|
))
|
|
260
276
|
}
|
|
261
277
|
|
|
278
|
+
/**
|
|
279
|
+
* Returns a E.164 phone number value for the user's input.
|
|
280
|
+
*
|
|
281
|
+
* For example, for country `"US"` and input `"(222) 333-4444"`
|
|
282
|
+
* it will return `"+12223334444"`.
|
|
283
|
+
*
|
|
284
|
+
* For international phone number input, it will also auto-correct
|
|
285
|
+
* some minor errors such as using a national prefix when writing
|
|
286
|
+
* an international phone number. For example, if the user inputs
|
|
287
|
+
* `"+44 0 7400 000000"` then it will return an auto-corrected
|
|
288
|
+
* `"+447400000000"` phone number value.
|
|
289
|
+
*
|
|
290
|
+
* Will return `undefined` if no digits have been input,
|
|
291
|
+
* or when inputting a phone number in national format and no
|
|
292
|
+
* default country or default "country calling code" have been set.
|
|
293
|
+
*
|
|
294
|
+
* @return {string} [value]
|
|
295
|
+
*/
|
|
296
|
+
getNumberValue() {
|
|
297
|
+
const {
|
|
298
|
+
digits,
|
|
299
|
+
callingCode,
|
|
300
|
+
country,
|
|
301
|
+
nationalSignificantNumber
|
|
302
|
+
} = this.state
|
|
303
|
+
|
|
304
|
+
// Will return `undefined` if no digits have been input.
|
|
305
|
+
if (!digits) {
|
|
306
|
+
return
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
if (this.isInternational()) {
|
|
310
|
+
if (callingCode) {
|
|
311
|
+
return '+' + callingCode + nationalSignificantNumber
|
|
312
|
+
} else {
|
|
313
|
+
return '+' + digits
|
|
314
|
+
}
|
|
315
|
+
} else {
|
|
316
|
+
if (country || callingCode) {
|
|
317
|
+
const callingCode_ = country ? this.metadata.countryCallingCode() : callingCode
|
|
318
|
+
return '+' + callingCode_ + nationalSignificantNumber
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
262
323
|
/**
|
|
263
324
|
* Returns an instance of `PhoneNumber` class.
|
|
264
325
|
* Will return `undefined` if no national (significant) number
|
|
@@ -266,26 +327,27 @@ export default class AsYouType {
|
|
|
266
327
|
* set and the user enters a phone number not in international format.
|
|
267
328
|
*/
|
|
268
329
|
getNumber() {
|
|
269
|
-
|
|
330
|
+
const {
|
|
270
331
|
nationalSignificantNumber,
|
|
271
|
-
carrierCode
|
|
332
|
+
carrierCode,
|
|
333
|
+
callingCode
|
|
272
334
|
} = this.state
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
return
|
|
280
|
-
}
|
|
281
|
-
}
|
|
335
|
+
|
|
336
|
+
// `this._getCountry()` is basically same as `this.state.country`
|
|
337
|
+
// with the only change that it return `undefined` in case of a
|
|
338
|
+
// "non-geographic" numbering plan instead of `"001"` "internal use" value.
|
|
339
|
+
const country = this._getCountry()
|
|
340
|
+
|
|
282
341
|
if (!nationalSignificantNumber) {
|
|
283
342
|
return
|
|
284
343
|
}
|
|
285
|
-
|
|
286
|
-
|
|
344
|
+
|
|
345
|
+
if (!country && !callingCode) {
|
|
346
|
+
return
|
|
347
|
+
}
|
|
348
|
+
|
|
287
349
|
const phoneNumber = new PhoneNumber(
|
|
288
|
-
|
|
350
|
+
country || callingCode,
|
|
289
351
|
nationalSignificantNumber,
|
|
290
352
|
this.metadata.metadata
|
|
291
353
|
)
|
package/source/AsYouType.test.js
CHANGED
|
@@ -1080,6 +1080,240 @@ describe('AsYouType', () => {
|
|
|
1080
1080
|
formatter.input('123')
|
|
1081
1081
|
formatter.getChars().should.equal('123')
|
|
1082
1082
|
})
|
|
1083
|
+
|
|
1084
|
+
// A test confirming the case when input `"11"` for country `"US"`
|
|
1085
|
+
// produces `value` `"+11"`.
|
|
1086
|
+
// https://gitlab.com/catamphetamine/react-phone-number-input/-/issues/113
|
|
1087
|
+
it('should determine the national (significant) part correctly when input with national prefix in US', () => {
|
|
1088
|
+
const formatter = new AsYouType('US')
|
|
1089
|
+
// As soon as the user has input `"11"`, no `format` matches
|
|
1090
|
+
// those "national number" digits in the `"US"` country metadata.
|
|
1091
|
+
// Since no `format` matches, the number doesn't seem like a valid one,
|
|
1092
|
+
// so it attempts to see if the user "forgot" to input a `"+"` at the start.
|
|
1093
|
+
// And it looks like they might've to.
|
|
1094
|
+
// So it acts as if the leading `"+"` is there,
|
|
1095
|
+
// as if the user's input is `"+11"`.
|
|
1096
|
+
// See `AsYouType.fixMissingPlus()` function.
|
|
1097
|
+
formatter.input('1 122 222 2222 3').should.equal('1 1 222 222 2223')
|
|
1098
|
+
formatter.getNumber().nationalNumber.should.equal('2222222223')
|
|
1099
|
+
})
|
|
1100
|
+
})
|
|
1101
|
+
|
|
1102
|
+
describe('AsYouType.getNumberValue()', () => {
|
|
1103
|
+
it('should return E.164 number value (national number, with national prefix, default country: US)', () => {
|
|
1104
|
+
const formatter = new AsYouType('US')
|
|
1105
|
+
expect(formatter.getNumberValue()).to.be.undefined
|
|
1106
|
+
formatter.input('')
|
|
1107
|
+
expect(formatter.getNumberValue()).to.be.undefined
|
|
1108
|
+
formatter.input('1')
|
|
1109
|
+
formatter.getNumberValue().should.equal('+1')
|
|
1110
|
+
formatter.input('2')
|
|
1111
|
+
formatter.getNumberValue().should.equal('+12')
|
|
1112
|
+
formatter.input('1')
|
|
1113
|
+
formatter.getNumberValue().should.equal('+121')
|
|
1114
|
+
formatter.input('3')
|
|
1115
|
+
formatter.getNumberValue().should.equal('+1213')
|
|
1116
|
+
formatter.input('373-4253')
|
|
1117
|
+
formatter.getNumberValue().should.equal('+12133734253')
|
|
1118
|
+
formatter.input('4')
|
|
1119
|
+
formatter.getNumberValue().should.equal('+121337342534')
|
|
1120
|
+
})
|
|
1121
|
+
|
|
1122
|
+
it('should return E.164 number value (national number, with national prefix, default calling code: 1)', () => {
|
|
1123
|
+
const formatter = new AsYouType({ defaultCallingCode: '1' })
|
|
1124
|
+
expect(formatter.getNumberValue()).to.be.undefined
|
|
1125
|
+
formatter.input('')
|
|
1126
|
+
expect(formatter.getNumberValue()).to.be.undefined
|
|
1127
|
+
formatter.input('1')
|
|
1128
|
+
formatter.getNumberValue().should.equal('+1')
|
|
1129
|
+
formatter.input('2')
|
|
1130
|
+
formatter.getNumberValue().should.equal('+12')
|
|
1131
|
+
formatter.input('1')
|
|
1132
|
+
formatter.getNumberValue().should.equal('+121')
|
|
1133
|
+
formatter.input('3')
|
|
1134
|
+
formatter.getNumberValue().should.equal('+1213')
|
|
1135
|
+
formatter.input('373-4253')
|
|
1136
|
+
formatter.getNumberValue().should.equal('+12133734253')
|
|
1137
|
+
formatter.input('4')
|
|
1138
|
+
formatter.getNumberValue().should.equal('+121337342534')
|
|
1139
|
+
})
|
|
1140
|
+
|
|
1141
|
+
it('should return E.164 number value (national number, default country: US)', () => {
|
|
1142
|
+
const formatter = new AsYouType('US')
|
|
1143
|
+
expect(formatter.getNumberValue()).to.be.undefined
|
|
1144
|
+
formatter.input('')
|
|
1145
|
+
expect(formatter.getNumberValue()).to.be.undefined
|
|
1146
|
+
formatter.input('2')
|
|
1147
|
+
formatter.getNumberValue().should.equal('+12')
|
|
1148
|
+
formatter.input('1')
|
|
1149
|
+
formatter.getNumberValue().should.equal('+121')
|
|
1150
|
+
formatter.input('3')
|
|
1151
|
+
formatter.getNumberValue().should.equal('+1213')
|
|
1152
|
+
formatter.input('373-4253')
|
|
1153
|
+
formatter.getNumberValue().should.equal('+12133734253')
|
|
1154
|
+
formatter.input('4')
|
|
1155
|
+
formatter.getNumberValue().should.equal('+121337342534')
|
|
1156
|
+
})
|
|
1157
|
+
|
|
1158
|
+
it('should return E.164 number value (national number, default calling code: 1)', () => {
|
|
1159
|
+
const formatter = new AsYouType({ defaultCallingCode: '1' })
|
|
1160
|
+
expect(formatter.getNumberValue()).to.be.undefined
|
|
1161
|
+
formatter.input('')
|
|
1162
|
+
expect(formatter.getNumberValue()).to.be.undefined
|
|
1163
|
+
formatter.input('2')
|
|
1164
|
+
formatter.getNumberValue().should.equal('+12')
|
|
1165
|
+
formatter.input('1')
|
|
1166
|
+
formatter.getNumberValue().should.equal('+121')
|
|
1167
|
+
formatter.input('3')
|
|
1168
|
+
formatter.getNumberValue().should.equal('+1213')
|
|
1169
|
+
formatter.input('373-4253')
|
|
1170
|
+
formatter.getNumberValue().should.equal('+12133734253')
|
|
1171
|
+
formatter.input('4')
|
|
1172
|
+
formatter.getNumberValue().should.equal('+121337342534')
|
|
1173
|
+
})
|
|
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
|
+
|
|
1186
|
+
it('should return E.164 number value (international number, default country: US)', () => {
|
|
1187
|
+
const formatter = new AsYouType('US')
|
|
1188
|
+
expect(formatter.getNumberValue()).to.be.undefined
|
|
1189
|
+
formatter.input('')
|
|
1190
|
+
expect(formatter.getNumberValue()).to.be.undefined
|
|
1191
|
+
formatter.input('+')
|
|
1192
|
+
expect(formatter.getNumberValue()).to.be.undefined
|
|
1193
|
+
formatter.input('1')
|
|
1194
|
+
formatter.getNumberValue().should.equal('+1')
|
|
1195
|
+
formatter.input('1')
|
|
1196
|
+
formatter.getNumberValue().should.equal('+1')
|
|
1197
|
+
formatter.input('2')
|
|
1198
|
+
formatter.getNumberValue().should.equal('+12')
|
|
1199
|
+
formatter.input('1')
|
|
1200
|
+
formatter.getNumberValue().should.equal('+121')
|
|
1201
|
+
formatter.input('3')
|
|
1202
|
+
formatter.getNumberValue().should.equal('+1213')
|
|
1203
|
+
formatter.input('373-4253')
|
|
1204
|
+
formatter.getNumberValue().should.equal('+12133734253')
|
|
1205
|
+
formatter.input('4')
|
|
1206
|
+
formatter.getNumberValue().should.equal('+121337342534')
|
|
1207
|
+
})
|
|
1208
|
+
|
|
1209
|
+
it('should return E.164 number value (international number, other default country: RU)', () => {
|
|
1210
|
+
const formatter = new AsYouType('RU')
|
|
1211
|
+
expect(formatter.getNumberValue()).to.be.undefined
|
|
1212
|
+
formatter.input('')
|
|
1213
|
+
expect(formatter.getNumberValue()).to.be.undefined
|
|
1214
|
+
formatter.input('+')
|
|
1215
|
+
expect(formatter.getNumberValue()).to.be.undefined
|
|
1216
|
+
formatter.input('1')
|
|
1217
|
+
formatter.getNumberValue().should.equal('+1')
|
|
1218
|
+
formatter.input('1')
|
|
1219
|
+
formatter.getNumberValue().should.equal('+1')
|
|
1220
|
+
formatter.input('2')
|
|
1221
|
+
formatter.getNumberValue().should.equal('+12')
|
|
1222
|
+
formatter.input('1')
|
|
1223
|
+
formatter.getNumberValue().should.equal('+121')
|
|
1224
|
+
formatter.input('3')
|
|
1225
|
+
formatter.getNumberValue().should.equal('+1213')
|
|
1226
|
+
formatter.input('373-4253')
|
|
1227
|
+
formatter.getNumberValue().should.equal('+12133734253')
|
|
1228
|
+
formatter.input('4')
|
|
1229
|
+
formatter.getNumberValue().should.equal('+121337342534')
|
|
1230
|
+
})
|
|
1231
|
+
|
|
1232
|
+
it('should return E.164 number value (international number, default calling code: 1)', () => {
|
|
1233
|
+
const formatter = new AsYouType('US', { defaultCallingCode: '1' })
|
|
1234
|
+
expect(formatter.getNumberValue()).to.be.undefined
|
|
1235
|
+
formatter.input('')
|
|
1236
|
+
expect(formatter.getNumberValue()).to.be.undefined
|
|
1237
|
+
formatter.input('+')
|
|
1238
|
+
expect(formatter.getNumberValue()).to.be.undefined
|
|
1239
|
+
formatter.input('1')
|
|
1240
|
+
formatter.getNumberValue().should.equal('+1')
|
|
1241
|
+
formatter.input('1')
|
|
1242
|
+
formatter.getNumberValue().should.equal('+1')
|
|
1243
|
+
formatter.input('2')
|
|
1244
|
+
formatter.getNumberValue().should.equal('+12')
|
|
1245
|
+
formatter.input('1')
|
|
1246
|
+
formatter.getNumberValue().should.equal('+121')
|
|
1247
|
+
formatter.input('3')
|
|
1248
|
+
formatter.getNumberValue().should.equal('+1213')
|
|
1249
|
+
formatter.input('373-4253')
|
|
1250
|
+
formatter.getNumberValue().should.equal('+12133734253')
|
|
1251
|
+
formatter.input('4')
|
|
1252
|
+
formatter.getNumberValue().should.equal('+121337342534')
|
|
1253
|
+
})
|
|
1254
|
+
|
|
1255
|
+
it('should return E.164 number value (international number, other default calling code: 7)', () => {
|
|
1256
|
+
const formatter = new AsYouType('US', { defaultCallingCode: '7' })
|
|
1257
|
+
expect(formatter.getNumberValue()).to.be.undefined
|
|
1258
|
+
formatter.input('')
|
|
1259
|
+
expect(formatter.getNumberValue()).to.be.undefined
|
|
1260
|
+
formatter.input('+')
|
|
1261
|
+
expect(formatter.getNumberValue()).to.be.undefined
|
|
1262
|
+
formatter.input('1')
|
|
1263
|
+
formatter.getNumberValue().should.equal('+1')
|
|
1264
|
+
formatter.input('1')
|
|
1265
|
+
formatter.getNumberValue().should.equal('+1')
|
|
1266
|
+
formatter.input('2')
|
|
1267
|
+
formatter.getNumberValue().should.equal('+12')
|
|
1268
|
+
formatter.input('1')
|
|
1269
|
+
formatter.getNumberValue().should.equal('+121')
|
|
1270
|
+
formatter.input('3')
|
|
1271
|
+
formatter.getNumberValue().should.equal('+1213')
|
|
1272
|
+
formatter.input('373-4253')
|
|
1273
|
+
formatter.getNumberValue().should.equal('+12133734253')
|
|
1274
|
+
formatter.input('4')
|
|
1275
|
+
formatter.getNumberValue().should.equal('+121337342534')
|
|
1276
|
+
})
|
|
1277
|
+
|
|
1278
|
+
it('should return E.164 number value (international number)', () => {
|
|
1279
|
+
const formatter = new AsYouType()
|
|
1280
|
+
expect(formatter.getNumberValue()).to.be.undefined
|
|
1281
|
+
formatter.input('')
|
|
1282
|
+
expect(formatter.getNumberValue()).to.be.undefined
|
|
1283
|
+
formatter.input('+')
|
|
1284
|
+
expect(formatter.getNumberValue()).to.be.undefined
|
|
1285
|
+
formatter.input('1')
|
|
1286
|
+
formatter.getNumberValue().should.equal('+1')
|
|
1287
|
+
formatter.input('1')
|
|
1288
|
+
formatter.getNumberValue().should.equal('+1')
|
|
1289
|
+
formatter.input('2')
|
|
1290
|
+
formatter.getNumberValue().should.equal('+12')
|
|
1291
|
+
formatter.input('1')
|
|
1292
|
+
formatter.getNumberValue().should.equal('+121')
|
|
1293
|
+
formatter.input('3')
|
|
1294
|
+
formatter.getNumberValue().should.equal('+1213')
|
|
1295
|
+
formatter.input('373-4253')
|
|
1296
|
+
formatter.getNumberValue().should.equal('+12133734253')
|
|
1297
|
+
formatter.input('4')
|
|
1298
|
+
formatter.getNumberValue().should.equal('+121337342534')
|
|
1299
|
+
})
|
|
1300
|
+
|
|
1301
|
+
it('should return E.164 number value (national number) (no default country or calling code)', () => {
|
|
1302
|
+
const formatter = new AsYouType()
|
|
1303
|
+
expect(formatter.getNumberValue()).to.be.undefined
|
|
1304
|
+
formatter.input('')
|
|
1305
|
+
expect(formatter.getNumberValue()).to.be.undefined
|
|
1306
|
+
formatter.input('1')
|
|
1307
|
+
expect(formatter.getNumberValue()).to.be.undefined
|
|
1308
|
+
formatter.input('12')
|
|
1309
|
+
expect(formatter.getNumberValue()).to.be.undefined
|
|
1310
|
+
formatter.input('3')
|
|
1311
|
+
expect(formatter.getNumberValue()).to.be.undefined
|
|
1312
|
+
formatter.input('373-4253')
|
|
1313
|
+
expect(formatter.getNumberValue()).to.be.undefined
|
|
1314
|
+
formatter.input('4')
|
|
1315
|
+
expect(formatter.getNumberValue()).to.be.undefined
|
|
1316
|
+
})
|
|
1083
1317
|
})
|
|
1084
1318
|
|
|
1085
1319
|
function type(something) {
|
|
@@ -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
|
|
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
|
-
|
|
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
|
|
115
|
-
|
|
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('
|
|
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
|