UNPKG

16.6 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports["default"] = void 0;
7
8var _PhoneNumber = _interopRequireDefault(require("./PhoneNumber"));
9
10var _constants = require("./constants");
11
12var _createExtensionPattern = _interopRequireDefault(require("./helpers/extension/createExtensionPattern"));
13
14var _RegExpCache = _interopRequireDefault(require("./findNumbers/RegExpCache"));
15
16var _util = require("./findNumbers/util");
17
18var _utf = require("./findNumbers/utf-8");
19
20var _Leniency = _interopRequireDefault(require("./findNumbers/Leniency"));
21
22var _parsePreCandidate = _interopRequireDefault(require("./findNumbers/parsePreCandidate"));
23
24var _isValidPreCandidate = _interopRequireDefault(require("./findNumbers/isValidPreCandidate"));
25
26var _isValidCandidate = _interopRequireWildcard(require("./findNumbers/isValidCandidate"));
27
28var _metadata = require("./metadata");
29
30var _parse_ = _interopRequireDefault(require("./parse_"));
31
32function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj["default"] = obj; return newObj; } }
33
34function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
35
36function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
37
38function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
39
40function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
41
42function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
43
44function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
45
46var EXTN_PATTERNS_FOR_MATCHING = (0, _createExtensionPattern["default"])('matching');
47/**
48 * Patterns used to extract phone numbers from a larger phone-number-like pattern. These are
49 * ordered according to specificity. For example, white-space is last since that is frequently
50 * used in numbers, not just to separate two numbers. We have separate patterns since we don't
51 * want to break up the phone-number-like text on more than one different kind of symbol at one
52 * time, although symbols of the same type (e.g. space) can be safely grouped together.
53 *
54 * Note that if there is a match, we will always check any text found up to the first match as
55 * well.
56 */
57
58var INNER_MATCHES = [// Breaks on the slash - e.g. "651-234-2345/332-445-1234"
59'\\/+(.*)/', // Note that the bracket here is inside the capturing group, since we consider it part of the
60// phone number. Will match a pattern like "(650) 223 3345 (754) 223 3321".
61'(\\([^(]*)', // Breaks on a hyphen - e.g. "12345 - 332-445-1234 is my number."
62// We require a space on either side of the hyphen for it to be considered a separator.
63"(?:".concat(_utf.pZ, "-|-").concat(_utf.pZ, ")").concat(_utf.pZ, "*(.+)"), // Various types of wide hyphens. Note we have decided not to enforce a space here, since it's
64// possible that it's supposed to be used to break two numbers without spaces, and we haven't
65// seen many instances of it used within a number.
66"[\u2012-\u2015\uFF0D]".concat(_utf.pZ, "*(.+)"), // Breaks on a full stop - e.g. "12345. 332-445-1234 is my number."
67"\\.+".concat(_utf.pZ, "*([^.]+)"), // Breaks on space - e.g. "3324451234 8002341234"
68"".concat(_utf.pZ, "+(").concat(_utf.PZ, "+)")]; // Limit on the number of leading (plus) characters.
69
70var leadLimit = (0, _util.limit)(0, 2); // Limit on the number of consecutive punctuation characters.
71
72var punctuationLimit = (0, _util.limit)(0, 4);
73/* The maximum number of digits allowed in a digit-separated block. As we allow all digits in a
74 * single block, set high enough to accommodate the entire national number and the international
75 * country code. */
76
77var digitBlockLimit = _constants.MAX_LENGTH_FOR_NSN + _constants.MAX_LENGTH_COUNTRY_CODE; // Limit on the number of blocks separated by punctuation.
78// Uses digitBlockLimit since some formats use spaces to separate each digit.
79
80var blockLimit = (0, _util.limit)(0, digitBlockLimit);
81/* A punctuation sequence allowing white space. */
82
83var punctuation = "[".concat(_constants.VALID_PUNCTUATION, "]") + punctuationLimit; // A digits block without punctuation.
84
85var digitSequence = _utf.pNd + (0, _util.limit)(1, digitBlockLimit);
86/**
87 * Phone number pattern allowing optional punctuation.
88 * The phone number pattern used by `find()`, similar to
89 * VALID_PHONE_NUMBER, but with the following differences:
90 * <ul>
91 * <li>All captures are limited in order to place an upper bound to the text matched by the
92 * pattern.
93 * <ul>
94 * <li>Leading punctuation / plus signs are limited.
95 * <li>Consecutive occurrences of punctuation are limited.
96 * <li>Number of digits is limited.
97 * </ul>
98 * <li>No whitespace is allowed at the start or end.
99 * <li>No alpha digits (vanity numbers such as 1-800-SIX-FLAGS) are currently supported.
100 * </ul>
101 */
102
103var PATTERN = '(?:' + _isValidCandidate.LEAD_CLASS + punctuation + ')' + leadLimit + digitSequence + '(?:' + punctuation + digitSequence + ')' + blockLimit + '(?:' + EXTN_PATTERNS_FOR_MATCHING + ')?'; // Regular expression of trailing characters that we want to remove.
104// We remove all characters that are not alpha or numerical characters.
105// The hash character is retained here, as it may signify
106// the previous block was an extension.
107//
108// // Don't know what does '&&' mean here.
109// const UNWANTED_END_CHAR_PATTERN = new RegExp(`[[\\P{N}&&\\P{L}]&&[^#]]+$`)
110//
111
112var UNWANTED_END_CHAR_PATTERN = new RegExp("[^".concat(_utf._pN).concat(_utf._pL, "#]+$"));
113var NON_DIGITS_PATTERN = /(\D+)/;
114var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || Math.pow(2, 53) - 1;
115/**
116 * A stateful class that finds and extracts telephone numbers from {@linkplain CharSequence text}.
117 * Instances can be created using the {@linkplain PhoneNumberUtil#findNumbers factory methods} in
118 * {@link PhoneNumberUtil}.
119 *
120 * <p>Vanity numbers (phone numbers using alphabetic digits such as <tt>1-800-SIX-FLAGS</tt> are
121 * not found.
122 *
123 * <p>This class is not thread-safe.
124 */
125
126var PhoneNumberMatcher =
127/*#__PURE__*/
128function () {
129 /** The iteration tristate. */
130
131 /** The next index to start searching at. Undefined in {@link State#DONE}. */
132 // A cache for frequently used country-specific regular expressions. Set to 32 to cover ~2-3
133 // countries being used for the same doc with ~10 patterns for each country. Some pages will have
134 // a lot more countries in use, but typically fewer numbers for each so expanding the cache for
135 // that use-case won't have a lot of benefit.
136
137 /**
138 * Creates a new instance. See the factory methods in {@link PhoneNumberUtil} on how to obtain a
139 * new instance.
140 *
141 * @param util the phone number util to use
142 * @param text the character sequence that we will search, null for no text
143 * @param country the country to assume for phone numbers not written in international format
144 * (with a leading plus, or with the international dialing prefix of the specified region).
145 * May be null or "ZZ" if only numbers with a leading plus should be
146 * considered.
147 * @param leniency the leniency to use when evaluating candidate phone numbers
148 * @param maxTries the maximum number of invalid numbers to try before giving up on the text.
149 * This is to cover degenerate cases where the text has a lot of false positives in it. Must
150 * be {@code >= 0}.
151 */
152 function PhoneNumberMatcher() {
153 var text = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
154 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
155 var metadata = arguments.length > 2 ? arguments[2] : undefined;
156
157 _classCallCheck(this, PhoneNumberMatcher);
158
159 _defineProperty(this, "state", 'NOT_READY');
160
161 _defineProperty(this, "searchIndex", 0);
162
163 _defineProperty(this, "regExpCache", new _RegExpCache["default"](32));
164
165 options = _objectSpread({}, options, {
166 defaultCallingCode: options.defaultCallingCode,
167 defaultCountry: options.defaultCountry && (0, _metadata.isSupportedCountry)(options.defaultCountry, metadata) ? options.defaultCountry : undefined,
168 leniency: options.leniency || options.extended ? 'POSSIBLE' : 'VALID',
169 maxTries: options.maxTries || MAX_SAFE_INTEGER
170 });
171
172 if (!options.leniency) {
173 throw new TypeError('`Leniency` not supplied');
174 }
175
176 if (options.maxTries < 0) {
177 throw new TypeError('`maxTries` not supplied');
178 }
179
180 this.text = text;
181 this.options = options;
182 this.metadata = metadata;
183 /** The degree of validation requested. */
184
185 this.leniency = _Leniency["default"][options.leniency];
186
187 if (!this.leniency) {
188 throw new TypeError("Unknown leniency: ".concat(options.leniency, "."));
189 }
190 /** The maximum number of retries after matching an invalid number. */
191
192
193 this.maxTries = options.maxTries;
194 this.PATTERN = new RegExp(PATTERN, 'ig');
195 }
196 /**
197 * Attempts to find the next subsequence in the searched sequence on or after {@code searchIndex}
198 * that represents a phone number. Returns the next match, null if none was found.
199 *
200 * @param index the search index to start searching at
201 * @return the phone number match found, null if none can be found
202 */
203
204
205 _createClass(PhoneNumberMatcher, [{
206 key: "find",
207 value: function find() {
208 // // Reset the regular expression.
209 // this.PATTERN.lastIndex = index
210 var matches;
211
212 while (this.maxTries > 0 && (matches = this.PATTERN.exec(this.text)) !== null) {
213 var candidate = matches[0];
214 var offset = matches.index;
215 candidate = (0, _parsePreCandidate["default"])(candidate);
216
217 if ((0, _isValidPreCandidate["default"])(candidate, offset, this.text)) {
218 var match = // Try to come up with a valid match given the entire candidate.
219 this.parseAndVerify(candidate, offset, this.text) // If that failed, try to find an "inner match" -
220 // there might be a phone number within this candidate.
221 || this.extractInnerMatch(candidate, offset, this.text);
222
223 if (match) {
224 if (this.options.v2) {
225 var phoneNumber = new _PhoneNumber["default"](match.country || match.countryCallingCode, match.phone, this.metadata);
226
227 if (match.ext) {
228 phoneNumber.ext = match.ext;
229 }
230
231 return {
232 startsAt: match.startsAt,
233 endsAt: match.endsAt,
234 number: phoneNumber
235 };
236 }
237
238 return match;
239 }
240 }
241
242 this.maxTries--;
243 }
244 }
245 /**
246 * Attempts to extract a match from `substring`
247 * if the substring itself does not qualify as a match.
248 */
249
250 }, {
251 key: "extractInnerMatch",
252 value: function extractInnerMatch(substring, offset, text) {
253 for (var _i = 0, _INNER_MATCHES = INNER_MATCHES; _i < _INNER_MATCHES.length; _i++) {
254 var innerMatchPattern = _INNER_MATCHES[_i];
255 var isFirstMatch = true;
256 var candidateMatch = void 0;
257 var innerMatchRegExp = new RegExp(innerMatchPattern, 'g');
258
259 while (this.maxTries > 0 && (candidateMatch = innerMatchRegExp.exec(substring)) !== null) {
260 if (isFirstMatch) {
261 // We should handle any group before this one too.
262 var _candidate = (0, _util.trimAfterFirstMatch)(UNWANTED_END_CHAR_PATTERN, substring.slice(0, candidateMatch.index));
263
264 var _match = this.parseAndVerify(_candidate, offset, text);
265
266 if (_match) {
267 return _match;
268 }
269
270 this.maxTries--;
271 isFirstMatch = false;
272 }
273
274 var candidate = (0, _util.trimAfterFirstMatch)(UNWANTED_END_CHAR_PATTERN, candidateMatch[1]); // Java code does `groupMatcher.start(1)` here,
275 // but there's no way in javascript to get a `candidate` start index,
276 // therefore resort to using this kind of an approximation.
277 // (`groupMatcher` is called `candidateInSubstringMatch` in this javascript port)
278 // https://stackoverflow.com/questions/15934353/get-index-of-each-capture-in-a-javascript-regex
279
280 var candidateIndexGuess = substring.indexOf(candidate, candidateMatch.index);
281 var match = this.parseAndVerify(candidate, offset + candidateIndexGuess, text);
282
283 if (match) {
284 return match;
285 }
286
287 this.maxTries--;
288 }
289 }
290 }
291 /**
292 * Parses a phone number from the `candidate` using `parseNumber` and
293 * verifies it matches the requested `leniency`. If parsing and verification succeed,
294 * a corresponding `PhoneNumberMatch` is returned, otherwise this method returns `null`.
295 *
296 * @param candidate the candidate match
297 * @param offset the offset of {@code candidate} within {@link #text}
298 * @return the parsed and validated phone number match, or null
299 */
300
301 }, {
302 key: "parseAndVerify",
303 value: function parseAndVerify(candidate, offset, text) {
304 if (!(0, _isValidCandidate["default"])(candidate, offset, text, this.options.leniency)) {
305 return;
306 }
307
308 var number = (0, _parse_["default"])(candidate, {
309 extended: true,
310 defaultCountry: this.options.defaultCountry,
311 defaultCallingCode: this.options.defaultCallingCode
312 }, this.metadata);
313
314 if (!number.possible) {
315 return;
316 }
317
318 if (this.leniency(number, candidate, this.metadata, this.regExpCache)) {
319 // // We used parseAndKeepRawInput to create this number,
320 // // but for now we don't return the extra values parsed.
321 // // TODO: stop clearing all values here and switch all users over
322 // // to using rawInput() rather than the rawString() of PhoneNumberMatch.
323 // number.clearCountryCodeSource()
324 // number.clearRawInput()
325 // number.clearPreferredDomesticCarrierCode()
326 var result = {
327 startsAt: offset,
328 endsAt: offset + candidate.length,
329 phone: number.phone
330 };
331
332 if (number.country && number.country !== '001') {
333 result.country = number.country;
334 } else {
335 result.countryCallingCode = number.countryCallingCode;
336 }
337
338 if (number.ext) {
339 result.ext = number.ext;
340 }
341
342 return result;
343 }
344 }
345 }, {
346 key: "hasNext",
347 value: function hasNext() {
348 if (this.state === 'NOT_READY') {
349 this.lastMatch = this.find(); // (this.searchIndex)
350
351 if (this.lastMatch) {
352 // this.searchIndex = this.lastMatch.endsAt
353 this.state = 'READY';
354 } else {
355 this.state = 'DONE';
356 }
357 }
358
359 return this.state === 'READY';
360 }
361 }, {
362 key: "next",
363 value: function next() {
364 // Check the state and find the next match as a side-effect if necessary.
365 if (!this.hasNext()) {
366 throw new Error('No next element');
367 } // Don't retain that memory any longer than necessary.
368
369
370 var result = this.lastMatch;
371 this.lastMatch = null;
372 this.state = 'NOT_READY';
373 return result;
374 }
375 }]);
376
377 return PhoneNumberMatcher;
378}();
379
380exports["default"] = PhoneNumberMatcher;
381//# sourceMappingURL=PhoneNumberMatcher.js.map
\No newline at end of file