libphonenumber-js
Version:
A simpler (and smaller) rewrite of Google Android's libphonenumber library in javascript
210 lines (209 loc) • 9.05 kB
JavaScript
function _createForOfIteratorHelperLoose(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (t) return (t = t.call(r)).next.bind(t); if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var o = 0; return function () { return o >= r.length ? { done: !0 } : { done: !1, value: r[o++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import PatternParser from './AsYouTypeFormatter.PatternParser.js';
var PatternMatcher = /*#__PURE__*/function () {
function PatternMatcher(pattern) {
_classCallCheck(this, PatternMatcher);
this.matchTree = new PatternParser().parse(pattern);
}
return _createClass(PatternMatcher, [{
key: "match",
value: function match(string) {
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
allowOverflow = _ref.allowOverflow;
if (!string) {
throw new Error('String is required');
}
var result = _match(string.split(''), this.matchTree, true);
if (result && result.match) {
delete result.matchedChars;
}
if (result && result.overflow) {
if (!allowOverflow) {
return;
}
}
return result;
}
}]);
}();
/**
* Matches `characters` against a pattern compiled into a `tree`.
* @param {string[]} characters
* @param {Tree} tree — A pattern compiled into a `tree`. See the `*.d.ts` file for the description of the `tree` structure.
* @param {boolean} last — Whether it's the last (rightmost) subtree on its level of the match tree.
* @return {object} See the `*.d.ts` file for the description of the result object.
*/
export { PatternMatcher as default };
function _match(characters, tree, last) {
// If `tree` is a string, then `tree` is a single character.
// That's because when a pattern is parsed, multi-character-string parts
// of a pattern are compiled into arrays of single characters.
// I still wrote this piece of code for a "general" hypothetical case
// when `tree` could be a string of several characters, even though
// such case is not possible with the current implementation.
if (typeof tree === 'string') {
var characterString = characters.join('');
if (tree.indexOf(characterString) === 0) {
// `tree` is always a single character.
// If `tree.indexOf(characterString) === 0`
// then `characters.length === tree.length`.
/* istanbul ignore else */
if (characters.length === tree.length) {
return {
match: true,
matchedChars: characters
};
}
// `tree` is always a single character.
// If `tree.indexOf(characterString) === 0`
// then `characters.length === tree.length`.
/* istanbul ignore next */
return {
partialMatch: true
// matchedChars: characters
};
}
if (characterString.indexOf(tree) === 0) {
if (last) {
// The `else` path is not possible because `tree` is always a single character.
// The `else` case for `characters.length > tree.length` would be
// `characters.length <= tree.length` which means `characters.length <= 1`.
// `characters` array can't be empty, so that means `characters === [tree]`,
// which would also mean `tree.indexOf(characterString) === 0` and that'd mean
// that the `if (tree.indexOf(characterString) === 0)` condition before this
// `if` condition would be entered, and returned from there, not reaching this code.
/* istanbul ignore else */
if (characters.length > tree.length) {
return {
overflow: true
};
}
}
return {
match: true,
matchedChars: characters.slice(0, tree.length)
};
}
return;
}
if (Array.isArray(tree)) {
var restCharacters = characters.slice();
var i = 0;
while (i < tree.length) {
var subtree = tree[i];
var result = _match(restCharacters, subtree, last && i === tree.length - 1);
if (!result) {
return;
} else if (result.overflow) {
return result;
} else if (result.match) {
// Continue with the next subtree with the rest of the characters.
restCharacters = restCharacters.slice(result.matchedChars.length);
if (restCharacters.length === 0) {
if (i === tree.length - 1) {
return {
match: true,
matchedChars: characters
};
} else {
return {
partialMatch: true
// matchedChars: characters
};
}
}
} else {
/* istanbul ignore else */
if (result.partialMatch) {
return {
partialMatch: true
// matchedChars: characters
};
} else {
throw new Error("Unsupported match result:\n".concat(JSON.stringify(result, null, 2)));
}
}
i++;
}
// If `last` then overflow has already been checked
// by the last element of the `tree` array.
/* istanbul ignore if */
if (last) {
return {
overflow: true
};
}
return {
match: true,
matchedChars: characters.slice(0, characters.length - restCharacters.length)
};
}
switch (tree.op) {
case '|':
var partialMatch;
for (var _iterator = _createForOfIteratorHelperLoose(tree.args), _step; !(_step = _iterator()).done;) {
var branch = _step.value;
var _result = _match(characters, branch, last);
if (_result) {
if (_result.overflow) {
return _result;
} else if (_result.match) {
return {
match: true,
matchedChars: _result.matchedChars
};
} else {
/* istanbul ignore else */
if (_result.partialMatch) {
partialMatch = true;
} else {
throw new Error("Unsupported match result:\n".concat(JSON.stringify(_result, null, 2)));
}
}
}
}
if (partialMatch) {
return {
partialMatch: true
// matchedChars: ...
};
}
// Not even a partial match.
return;
case '[]':
for (var _iterator2 = _createForOfIteratorHelperLoose(tree.args), _step2; !(_step2 = _iterator2()).done;) {
var _char = _step2.value;
if (characters[0] === _char) {
if (characters.length === 1) {
return {
match: true,
matchedChars: characters
};
}
if (last) {
return {
overflow: true
};
}
return {
match: true,
matchedChars: [_char]
};
}
}
// No character matches.
return;
/* istanbul ignore next */
default:
throw new Error("Unsupported instruction tree: ".concat(tree));
}
}
//# sourceMappingURL=AsYouTypeFormatter.PatternMatcher.js.map