Debian Codegraph @

node/autolinker/dist/commonjs/matcher/phone-matcher.js

raw
1 "use strict";
2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.PhoneMatcher = void 0;
4 var tslib_1 = require("tslib");
5 var matcher_1 = require("./matcher");
6 var phone_match_1 = require("../match/phone-match");
7 var regex_lib_1 = require("../regex-lib");
8 // RegExp objects which are shared by all instances of PhoneMatcher. These are
9 // here to avoid re-instantiating the RegExp objects if `Autolinker.link()` is
10 // called multiple times, thus instantiating PhoneMatcher and its RegExp
11 // objects each time (which is very expensive - see https://github.com/gregjacobs/Autolinker.js/issues/314).
12 // See descriptions of the properties where they are used for details about them
13 // Over the years, many people have added to this regex, but it should have been
14 // split up by country. Maybe one day we can break this down.
15 var mostPhoneNumbers = /(?:(?:(?:(\+)?\d{1,3}[-\040.]?)?\(?\d{3}\)?[-\040.]?\d{3}[-\040.]?\d{4})|(?:(\+)(?:9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|4[987654310]|3[9643210]|2[70]|7|1)[-\040.]?(?:\d[-\040.]?){6,12}\d+))([,;]+[0-9]+#?)*/;
16 // Regex for Japanese phone numbers
17 var japanesePhoneRe = /(0([1-9]{1}-?[1-9]\d{3}|[1-9]{2}-?\d{3}|[1-9]{2}\d{1}-?\d{2}|[1-9]{2}\d{2}-?\d{1})-?\d{4}|0[789]0-?\d{4}-?\d{4}|050-?\d{4}-?\d{4})/;
18 // Combined regex
19 var phoneMatcherRegex = new RegExp("".concat(mostPhoneNumbers.source, "|").concat(japanesePhoneRe.source), 'g');
20 /**
21 * @class Autolinker.matcher.Phone
22 * @extends Autolinker.matcher.Matcher
23 *
24 * Matcher to find Phone number matches in an input string.
25 *
26 * See this class's superclass ({@link Autolinker.matcher.Matcher}) for more
27 * details.
28 */
29 var PhoneMatcher = /** @class */ (function (_super) {
30 (0, tslib_1.__extends)(PhoneMatcher, _super);
31 function PhoneMatcher() {
32 var _this = _super !== null && _super.apply(this, arguments) || this;
33 /**
34 * The regular expression to match Phone numbers. Example matches:
35 *
36 * (123) 456-7890
37 * 123 456 7890
38 * 123-456-7890
39 * +18004441234,,;,10226420346#
40 * +1 (800) 444 1234
41 * 10226420346#
42 * 1-800-444-1234,1022,64,20346#
43 *
44 * This regular expression has the following capturing groups:
45 *
46 * 1 or 2. The prefixed '+' sign, if there is one.
47 *
48 * @protected
49 * @property {RegExp} matcherRegex
50 */
51 _this.matcherRegex = phoneMatcherRegex;
52 return _this;
53 }
54 /**
55 * @inheritdoc
56 */
57 PhoneMatcher.prototype.parseMatches = function (text) {
58 var matcherRegex = this.matcherRegex, tagBuilder = this.tagBuilder, matches = [], match;
59 while ((match = matcherRegex.exec(text)) !== null) {
60 // Remove non-numeric values from phone number string
61 var matchedText = match[0], cleanNumber = matchedText.replace(/[^0-9,;#]/g, ''), // strip out non-digit characters exclude comma semicolon and #
62 plusSign = !!(match[1] || match[2]), // match[ 1 ] or match[ 2 ] is the prefixed plus sign, if there is one
63 before = match.index == 0 ? '' : text.substr(match.index - 1, 1), after = text.substr(match.index + matchedText.length, 1), contextClear = !before.match(/\d/) && !after.match(/\d/);
64 if (this.testMatch(match[3]) && this.testMatch(matchedText) && contextClear) {
65 matches.push(new phone_match_1.PhoneMatch({
66 tagBuilder: tagBuilder,
67 matchedText: matchedText,
68 offset: match.index,
69 number: cleanNumber,
70 plusSign: plusSign,
71 }));
72 }
73 }
74 return matches;
75 };
76 PhoneMatcher.prototype.testMatch = function (text) {
77 return regex_lib_1.nonDigitRe.test(text);
78 };
79 return PhoneMatcher;
80 }(matcher_1.Matcher));
81 exports.PhoneMatcher = PhoneMatcher;
82 //# sourceMappingURL=phone-matcher.js.map