@formily/path
Version:
> Path System
1,331 lines (1,313 loc) • 74.9 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define('formily.path', ['exports'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.Formily = global.Formily || {}, global.Formily.Path = {})));
})(this, (function (exports) { 'use strict';
(function() {
const env = {"NODE_ENV":"development"};
try {
if (process) {
process.env = Object.assign({}, process.env);
Object.assign(process.env, env);
return;
}
} catch (e) {} // avoid ReferenceError: process is not defined
globalThis.process = { env:env };
})();
/******************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
/* global Reflect, Promise, SuppressedError, Symbol */
var extendStatics = function(d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
function __extends(d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
}
var __assign = function() {
__assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
function __read(o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
}
function __spreadArray(to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
}
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
var e = new Error(message);
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
};
var ContextType = function (flag, props) {
return __assign({ flag: flag }, props);
};
var bracketContext = ContextType('[]');
var bracketArrayContext = ContextType('[\\d]');
var bracketDContext = ContextType('[[]]');
var parenContext = ContextType('()');
var braceContext = ContextType('{}');
var destructorContext = ContextType('{x}');
var TokenType = function (flag, props) {
return __assign({ flag: flag }, props);
};
var nameTok = TokenType('name', {
expectNext: function (next) {
if (this.includesContext(destructorContext)) {
return (next === nameTok ||
next === commaTok ||
next === bracketRTok ||
next === braceRTok ||
next === colonTok);
}
return (next === dotTok ||
next === commaTok ||
next === eofTok ||
next === bracketRTok ||
next === parenRTok ||
next === colonTok ||
next === expandTok ||
next === bracketLTok);
},
});
var starTok = TokenType('*', {
expectNext: function (next) {
return (next === dotTok ||
next === parenLTok ||
next === bracketLTok ||
next === eofTok ||
next === commaTok ||
next === parenRTok);
},
});
var dbStarTok = TokenType('**', {
expectNext: function (next) {
return (next === dotTok ||
next === bracketLTok ||
next === eofTok ||
next === commaTok ||
next === parenRTok);
},
});
var dotTok = TokenType('.', {
expectNext: function (next) {
return (next === dotTok ||
next === nameTok ||
next === bracketDLTok ||
next === starTok ||
next === dbStarTok ||
next === bracketLTok ||
next === braceLTok ||
next === eofTok);
},
expectPrev: function (prev) {
return (prev === dotTok ||
prev === nameTok ||
prev === bracketDRTok ||
prev === starTok ||
prev === parenRTok ||
prev === bracketRTok ||
prev === expandTok ||
prev === braceRTok);
},
});
var bangTok = TokenType('!', {
expectNext: function (next) {
return next === nameTok || next === bracketDLTok;
},
});
var colonTok = TokenType(':', {
expectNext: function (next) {
if (this.includesContext(destructorContext)) {
return next === nameTok || next === braceLTok || next === bracketLTok;
}
return next === nameTok || next === bracketDLTok || next === bracketRTok;
},
});
var braceLTok = TokenType('{', {
expectNext: function (next) {
return next === nameTok;
},
expectPrev: function (prev) {
if (this.includesContext(destructorContext)) {
return prev === colonTok || prev === commaTok || prev === bracketLTok;
}
return prev === dotTok || prev === colonTok || prev === parenLTok;
},
updateContext: function () {
this.state.context.push(braceContext);
},
});
var braceRTok = TokenType('}', {
expectNext: function (next) {
if (this.includesContext(destructorContext)) {
return (next === commaTok ||
next === braceRTok ||
next === eofTok ||
next === bracketRTok);
}
return next === dotTok || next === eofTok || next === commaTok;
},
expectPrev: function (prev) {
return prev === nameTok || prev === braceRTok || prev === bracketRTok;
},
updateContext: function () {
this.state.context.pop(braceContext);
},
});
var bracketLTok = TokenType('[', {
expectNext: function (next) {
if (this.includesContext(destructorContext)) {
return (next === nameTok ||
next === bracketLTok ||
next === braceLTok ||
next === bracketRTok);
}
return (next === nameTok ||
next === bracketDLTok ||
next === colonTok ||
next === bracketLTok ||
next === ignoreTok ||
next === bracketRTok);
},
expectPrev: function (prev) {
if (this.includesContext(destructorContext)) {
return prev === colonTok || prev === commaTok || prev === bracketLTok;
}
return (prev === starTok ||
prev === bracketLTok ||
prev === dotTok ||
prev === nameTok ||
prev === parenLTok ||
// never reach
prev == commaTok);
},
updateContext: function () {
this.state.context.push(bracketContext);
},
});
var bracketRTok = TokenType(']', {
expectNext: function (next) {
if (this.includesContext(destructorContext)) {
return (next === commaTok ||
next === braceRTok ||
next === bracketRTok ||
next === eofTok);
}
return (next === dotTok ||
next === eofTok ||
next === commaTok ||
next === parenRTok ||
next === bracketRTok);
},
updateContext: function () {
if (this.includesContext(bracketArrayContext))
return;
if (!this.includesContext(bracketContext))
throw this.unexpect();
this.state.context.pop();
},
});
var bracketDLTok = TokenType('[[', {
updateContext: function () {
this.state.context.push(bracketDContext);
},
});
var bracketDRTok = TokenType(']]', {
updateContext: function () {
if (this.curContext() !== bracketDContext)
throw this.unexpect();
this.state.context.pop();
},
});
var parenLTok = TokenType('(', {
expectNext: function (next) {
return (next === nameTok ||
next === bracketDLTok ||
next === braceLTok ||
next === bangTok ||
next === bracketLTok);
},
expectPrev: function (prev) {
return prev === starTok;
},
updateContext: function () {
this.state.context.push(parenContext);
},
});
var parenRTok = TokenType(')', {
expectNext: function (next) {
return (next === dotTok ||
next === eofTok ||
next === commaTok ||
next === parenRTok);
},
updateContext: function () {
if (this.curContext() !== parenContext)
throw this.unexpect();
this.state.context.pop();
},
});
var commaTok = TokenType(',', {
expectNext: function (next) {
return (next === nameTok ||
next === bracketDLTok ||
next === bracketLTok ||
next === braceLTok);
},
});
var ignoreTok = TokenType('ignore', {
expectNext: function (next) {
return next === bracketDRTok;
},
expectPrev: function (prev) {
return prev == bracketDLTok;
},
});
var expandTok = TokenType('expandTok', {
expectNext: function (next) {
return (next === dotTok ||
next === eofTok ||
next === commaTok ||
next === parenRTok);
},
});
var eofTok = TokenType('eof');
var nonASCIIWhitespace = /[\u1680\u180e\u2000-\u200a\u202f\u205f\u3000\ufeff]/;
var fullCharCodeAtPos = function (input, pos) {
if (String.fromCharCode)
return input.codePointAt(pos);
var code = input.charCodeAt(pos);
if (code <= 0xd7ff || code >= 0xe000)
return code;
var next = input.charCodeAt(pos + 1);
return (code << 10) + next - 0x35fdc00;
};
var isRewordCode = function (code) {
return code === 42 ||
code === 46 ||
code === 33 ||
code === 91 ||
code === 93 ||
code === 40 ||
code === 41 ||
code === 44 ||
code === 58 ||
code === 126 ||
code === 123 ||
code === 125;
};
var getError = function (message, props) {
var err = new Error(message);
Object.assign(err, props);
return err;
};
var slice = function (string, start, end) {
var str = '';
for (var i = start; i < end; i++) {
var ch = string.charAt(i);
if (ch !== '\\') {
str += ch;
}
}
return str;
};
var Tokenizer = /** @class */ (function () {
function Tokenizer(input) {
this.input = input;
this.state = {
context: [],
type: null,
pos: 0,
};
this.type_ = null;
}
Tokenizer.prototype.curContext = function () {
return this.state.context[this.state.context.length - 1];
};
Tokenizer.prototype.includesContext = function (context) {
for (var len = this.state.context.length - 1; len >= 0; len--) {
if (this.state.context[len] === context) {
return true;
}
}
return false;
};
Tokenizer.prototype.unexpect = function (type) {
type = type || this.state.type;
return getError("Unexpect token \"".concat(type.flag, "\" in ").concat(this.state.pos, " char."), {
pos: this.state.pos,
});
};
Tokenizer.prototype.expectNext = function (type, next) {
if (type && type.expectNext) {
if (next && !type.expectNext.call(this, next)) {
throw getError("Unexpect token \"".concat(next.flag, "\" token should not be behind \"").concat(type.flag, "\" token.(").concat(this.state.pos, "th char)"), {
pos: this.state.pos,
});
}
}
};
Tokenizer.prototype.expectPrev = function (type, prev) {
if (type && type.expectPrev) {
if (prev && !type.expectPrev.call(this, prev)) {
throw getError("Unexpect token \"".concat(type.flag, "\" should not be behind \"").concat(prev.flag, "\"(").concat(this.state.pos, "th char)."), {
pos: this.state.pos,
});
}
}
};
Tokenizer.prototype.match = function (type) {
return this.state.type === type;
};
Tokenizer.prototype.skipSpace = function () {
if (this.curContext() === bracketDContext)
return;
loop: while (this.state.pos < this.input.length) {
var ch = this.input.charCodeAt(this.state.pos);
switch (ch) {
case 32:
case 160:
++this.state.pos;
break;
case 13:
if (this.input.charCodeAt(this.state.pos + 1) === 10) {
++this.state.pos;
}
case 10:
case 8232:
case 8233:
++this.state.pos;
break;
default:
if ((ch > 8 && ch < 14) ||
(ch >= 5760 && nonASCIIWhitespace.test(String.fromCharCode(ch)))) {
++this.state.pos;
}
else {
break loop;
}
}
}
};
Tokenizer.prototype.next = function () {
this.type_ = this.state.type;
if (this.input.length <= this.state.pos) {
return this.finishToken(eofTok);
}
this.skipSpace();
this.readToken(this.getCode(), this.state.pos > 0 ? this.getCode(this.state.pos - 1) : -Infinity);
};
Tokenizer.prototype.getCode = function (pos) {
if (pos === void 0) { pos = this.state.pos; }
return fullCharCodeAtPos(this.input, pos);
};
Tokenizer.prototype.eat = function (type) {
if (this.match(type)) {
this.next();
return true;
}
else {
return false;
}
};
Tokenizer.prototype.readKeyWord = function () {
var startPos = this.state.pos, string = '';
while (true) {
var code = this.getCode();
var prevCode = this.getCode(this.state.pos - 1);
if (this.input.length === this.state.pos) {
string = slice(this.input, startPos, this.state.pos + 1);
break;
}
if (!isRewordCode(code) || prevCode === 92) {
if (code === 32 ||
code === 160 ||
code === 10 ||
code === 8232 ||
code === 8233) {
string = slice(this.input, startPos, this.state.pos);
break;
}
if (code === 13 && this.input.charCodeAt(this.state.pos + 1) === 10) {
string = slice(this.input, startPos, this.state.pos);
break;
}
if ((code > 8 && code < 14) ||
(code >= 5760 && nonASCIIWhitespace.test(String.fromCharCode(code)))) {
string = slice(this.input, startPos, this.state.pos);
break;
}
this.state.pos++;
}
else {
string = slice(this.input, startPos, this.state.pos);
break;
}
}
this.finishToken(nameTok, string);
};
Tokenizer.prototype.readIgnoreString = function () {
var startPos = this.state.pos, prevCode, string = '';
while (true) {
var code = this.getCode();
if (this.state.pos >= this.input.length)
break;
if ((code === 91 || code === 93) && prevCode === 92) {
this.state.pos++;
prevCode = '';
}
else if (code == 93 && prevCode === 93) {
string = this.input
.slice(startPos, this.state.pos - 1)
.replace(/\\([\[\]])/g, '$1');
this.state.pos++;
break;
}
else {
this.state.pos++;
prevCode = code;
}
}
this.finishToken(ignoreTok, string);
this.finishToken(bracketDRTok);
};
Tokenizer.prototype.finishToken = function (type, value) {
var preType = this.state.type;
this.state.type = type;
if (value !== undefined)
this.state.value = value;
this.expectNext(preType, type);
this.expectPrev(type, preType);
if (type.updateContext) {
type.updateContext.call(this, preType);
}
};
Tokenizer.prototype.readToken = function (code, prevCode) {
if (prevCode === 92) {
return this.readKeyWord();
}
if (this.input.length <= this.state.pos) {
this.finishToken(eofTok);
}
else if (this.curContext() === bracketDContext) {
this.readIgnoreString();
}
else if (code === 123) {
this.state.pos++;
this.finishToken(braceLTok);
}
else if (code === 125) {
this.state.pos++;
this.finishToken(braceRTok);
}
else if (code === 42) {
this.state.pos++;
if (this.getCode() === 42) {
this.state.pos++;
return this.finishToken(dbStarTok);
}
this.finishToken(starTok);
}
else if (code === 33) {
this.state.pos++;
this.finishToken(bangTok);
}
else if (code === 46) {
this.state.pos++;
this.finishToken(dotTok);
}
else if (code === 91) {
this.state.pos++;
if (this.getCode() === 91) {
this.state.pos++;
return this.finishToken(bracketDLTok);
}
this.finishToken(bracketLTok);
}
else if (code === 126) {
this.state.pos++;
this.finishToken(expandTok);
}
else if (code === 93) {
this.state.pos++;
this.finishToken(bracketRTok);
}
else if (code === 40) {
this.state.pos++;
this.finishToken(parenLTok);
}
else if (code === 41) {
this.state.pos++;
this.finishToken(parenRTok);
}
else if (code === 44) {
this.state.pos++;
this.finishToken(commaTok);
}
else if (code === 58) {
this.state.pos++;
this.finishToken(colonTok);
}
else {
this.readKeyWord();
}
};
return Tokenizer;
}());
var isType$1 = function (type) {
return function (obj) {
return obj && obj.type === type;
};
};
var isIdentifier = isType$1('Identifier');
var isIgnoreExpression = isType$1('IgnoreExpression');
var isDotOperator = isType$1('DotOperator');
var isWildcardOperator = isType$1('WildcardOperator');
var isExpandOperator = isType$1('ExpandOperator');
var isGroupExpression = isType$1('GroupExpression');
var isRangeExpression = isType$1('RangeExpression');
var isDestructorExpression = isType$1('DestructorExpression');
var isObjectPattern = isType$1('ObjectPattern');
var isArrayPattern = isType$1('ArrayPattern');
var toString = Object.prototype.toString;
var isType = function (type) {
return function (obj) {
return toString.call(obj) === "[object ".concat(type, "]");
};
};
var isFn = isType('Function');
var isArr = Array.isArray || isType('Array');
var isStr = isType('String');
var isNum = isType('Number');
var isObj = function (val) { return typeof val === 'object'; };
var isRegExp = isType('RegExp');
var isNumberLike = function (t) {
return isNum(t) || /^(\d+)(\.\d+)?$/.test(t);
};
var isArray = isArr;
var keyList = Object.keys;
var hasProp = Object.prototype.hasOwnProperty;
var toArr = function (val) {
return Array.isArray(val) ? val : val !== undefined ? [val] : [];
};
var isEqual = function (a, b) {
if (a === b) {
return true;
}
if (a && b && typeof a === 'object' && typeof b === 'object') {
var arrA = isArray(a);
var arrB = isArray(b);
var i = void 0;
var length = void 0;
var key = void 0;
if (arrA && arrB) {
length = a.length;
if (length !== b.length) {
return false;
}
for (i = length; i-- !== 0;) {
if (!isEqual(a[i], b[i])) {
return false;
}
}
return true;
}
if (arrA !== arrB) {
return false;
}
var keys = keyList(a);
length = keys.length;
if (length !== keyList(b).length) {
return false;
}
for (i = length; i-- !== 0;) {
if (!hasProp.call(b, keys[i])) {
return false;
}
}
for (i = length; i-- !== 0;) {
key = keys[i];
if (!isEqual(a[key], b[key])) {
return false;
}
}
return true;
}
return a !== a && b !== b;
};
var isSegmentEqual = function (a, b) {
a = typeof a === 'symbol' ? a : "".concat(a);
b = typeof b === 'symbol' ? b : "".concat(b);
return a === b;
};
var DestructorCache = new Map();
var isValid$1 = function (val) { return val !== undefined && val !== null; };
var getDestructor = function (source) {
return DestructorCache.get(source);
};
var setDestructor = function (source, rules) {
DestructorCache.set(source, rules);
};
var parseDestructorRules = function (node) {
var rules = [];
if (isObjectPattern(node)) {
var index_1 = 0;
node.properties.forEach(function (child) {
rules[index_1] = {
path: [],
};
rules[index_1].key = child.key.value;
rules[index_1].path.push(child.key.value);
if (isIdentifier(child.value)) {
rules[index_1].key = child.value.value;
}
var basePath = rules[index_1].path;
var childRules = parseDestructorRules(child.value);
var k = index_1;
childRules.forEach(function (rule) {
if (rules[k]) {
rules[k].key = rule.key;
rules[k].path = basePath.concat(rule.path);
}
else {
rules[k] = {
key: rule.key,
path: basePath.concat(rule.path),
};
}
k++;
});
if (k > index_1) {
index_1 = k;
}
else {
index_1++;
}
});
return rules;
}
else if (isArrayPattern(node)) {
var index_2 = 0;
node.elements.forEach(function (child, key) {
rules[index_2] = {
path: [],
};
rules[index_2].key = key;
rules[index_2].path.push(key);
if (isIdentifier(child)) {
rules[index_2].key = child.value;
}
var basePath = rules[index_2].path;
var childRules = parseDestructorRules(child);
var k = index_2;
childRules.forEach(function (rule) {
if (rules[k]) {
rules[k].key = rule.key;
rules[k].path = basePath.concat(rule.path);
}
else {
rules[k] = {
key: rule.key,
path: basePath.concat(rule.path),
};
}
k++;
});
if (k > index_2) {
index_2 = k;
}
else {
index_2++;
}
});
return rules;
}
if (isDestructorExpression(node)) {
return parseDestructorRules(node.value);
}
return rules;
};
var setInByDestructor = function (source, rules, value, mutators) {
rules.forEach(function (_a) {
var key = _a.key, path = _a.path;
mutators.setIn([key], source, mutators.getIn(path, value));
});
};
var getInByDestructor = function (source, rules, mutators) {
var response = {};
if (rules.length) {
if (isNum(rules[0].path[0])) {
response = [];
}
}
source = isValid$1(source) ? source : {};
rules.forEach(function (_a) {
var key = _a.key, path = _a.path;
mutators.setIn(path, response, source[key]);
});
return response;
};
var deleteInByDestructor = function (source, rules, mutators) {
rules.forEach(function (_a) {
var key = _a.key;
mutators.deleteIn([key], source);
});
};
var existInByDestructor = function (source, rules, start, mutators) {
return rules.every(function (_a) {
var key = _a.key;
return mutators.existIn([key], source, start);
});
};
var createTreeBySegments = function (segments, afterNode) {
if (segments === void 0) { segments = []; }
var segLen = segments.length;
var build = function (start) {
if (start === void 0) { start = 0; }
var after = start < segLen - 1 ? build(start + 1) : afterNode;
var dot = after && {
type: 'DotOperator',
after: after,
};
return {
type: 'Identifier',
value: segments[start],
after: dot,
};
};
return build();
};
var calculate = function (a, b, operator) {
if (isNumberLike(a) && isNumberLike(b)) {
if (operator === '+')
return String(Number(a) + Number(b));
if (operator === '-')
return String(Number(a) - Number(b));
if (operator === '*')
return String(Number(a) * Number(b));
if (operator === '/')
return String(Number(a) / Number(b));
}
else {
if (operator === '+')
return String(a) + String(b);
if (operator === '-')
return 'NaN';
if (operator === '*')
return 'NaN';
if (operator === '/')
return 'NaN';
}
return String(Number(b));
};
var Parser = /** @class */ (function (_super) {
__extends(Parser, _super);
function Parser(input, base) {
var _this = _super.call(this, input) || this;
_this.isMatchPattern = false;
_this.isWildMatchPattern = false;
_this.haveExcludePattern = false;
_this.haveRelativePattern = false;
_this.base = base;
return _this;
}
Parser.prototype.parse = function () {
var node;
this.data = {
segments: [],
};
if (!this.eat(eofTok)) {
this.next();
node = this.parseAtom(this.state.type);
}
this.data.tree = node;
return node;
};
Parser.prototype.append = function (parent, node) {
if (parent && node) {
parent.after = node;
}
};
Parser.prototype.parseAtom = function (type) {
switch (type) {
case braceLTok:
case bracketLTok:
if (this.includesContext(destructorContext)) {
if (type === braceLTok) {
return this.parseObjectPattern();
}
else {
return this.parseArrayPattern();
}
}
return this.parseDestructorExpression();
case nameTok:
return this.parseIdentifier();
case expandTok:
return this.parseExpandOperator();
case dbStarTok:
case starTok:
return this.parseWildcardOperator();
case bracketDLTok:
return this.parseIgnoreExpression();
case dotTok:
return this.parseDotOperator();
}
};
Parser.prototype.pushSegments = function (key) {
this.data.segments.push(key);
};
Parser.prototype.parseIdentifier = function () {
var node = {
type: 'Identifier',
value: this.state.value,
};
var hasNotInDestructor = !this.includesContext(destructorContext) &&
!this.isMatchPattern &&
!this.isWildMatchPattern;
this.next();
if (this.includesContext(bracketArrayContext)) {
if (this.state.type !== bracketRTok) {
throw this.unexpect();
}
else {
this.state.context.pop();
this.next();
}
}
else if (hasNotInDestructor) {
this.pushSegments(node.value);
}
if (this.state.type === bracketLTok) {
this.next();
if (this.state.type !== nameTok) {
throw this.unexpect();
}
this.state.context.push(bracketArrayContext);
var isNumberKey = false;
if (/^\d+$/.test(this.state.value)) {
isNumberKey = true;
}
var value = this.state.value;
this.pushSegments(isNumberKey ? Number(value) : value);
var after = this.parseAtom(this.state.type);
if (isNumberKey) {
after.arrayIndex = true;
}
this.append(node, after);
}
else {
this.append(node, this.parseAtom(this.state.type));
}
return node;
};
Parser.prototype.parseExpandOperator = function () {
var node = {
type: 'ExpandOperator',
};
this.isMatchPattern = true;
this.isWildMatchPattern = true;
this.data.segments = [];
this.next();
this.append(node, this.parseAtom(this.state.type));
return node;
};
Parser.prototype.parseWildcardOperator = function () {
var node = {
type: 'WildcardOperator',
};
if (this.state.type === dbStarTok) {
node.optional = true;
}
this.isMatchPattern = true;
this.isWildMatchPattern = true;
this.data.segments = [];
this.next();
if (this.state.type === parenLTok) {
node.filter = this.parseGroupExpression(node);
}
else if (this.state.type === bracketLTok) {
node.filter = this.parseRangeExpression(node);
}
this.append(node, this.parseAtom(this.state.type));
return node;
};
Parser.prototype.parseDestructorExpression = function () {
var _this = this;
var node = {
type: 'DestructorExpression',
};
this.state.context.push(destructorContext);
var startPos = this.state.pos - 1;
node.value =
this.state.type === braceLTok
? this.parseObjectPattern()
: this.parseArrayPattern();
var endPos = this.state.pos;
this.state.context.pop();
node.source = this.input
.substring(startPos, endPos)
.replace(/\[\s*([\+\-\*\/])?\s*([^,\]\s]*)\s*\]/, function (match, operator, target) {
if (_this.relative !== undefined) {
if (operator) {
if (target) {
return calculate(_this.relative, target, operator);
}
else {
return calculate(_this.relative, 1, operator);
}
}
else {
if (target) {
return calculate(_this.relative, target, '+');
}
else {
return String(_this.relative);
}
}
}
return match;
})
.replace(/\s*\.\s*/g, '')
.replace(/\s*/g, '');
if (this.relative === undefined) {
setDestructor(node.source, parseDestructorRules(node));
}
this.relative = undefined;
this.pushSegments(node.source);
this.next();
this.append(node, this.parseAtom(this.state.type));
return node;
};
Parser.prototype.parseArrayPattern = function () {
var node = {
type: 'ArrayPattern',
elements: [],
};
this.next();
node.elements = this.parseArrayPatternElements();
return node;
};
Parser.prototype.parseArrayPatternElements = function () {
var nodes = [];
while (this.state.type !== bracketRTok && this.state.type !== eofTok) {
nodes.push(this.parseAtom(this.state.type));
if (this.state.type === bracketRTok) {
if (this.includesContext(destructorContext)) {
this.next();
}
return nodes;
}
this.next();
}
return nodes;
};
Parser.prototype.parseObjectPattern = function () {
var node = {
type: 'ObjectPattern',
properties: [],
};
this.next();
node.properties = this.parseObjectProperties();
return node;
};
Parser.prototype.parseObjectProperties = function () {
var nodes = [];
while (this.state.type !== braceRTok && this.state.type !== eofTok) {
var node = {
type: 'ObjectPatternProperty',
key: this.parseAtom(this.state.type),
};
nodes.push(node);
if (this.state.type === colonTok) {
this.next();
node.value = this.parseAtom(this.state.type);
}
if (this.state.type === braceRTok) {
if (this.includesContext(destructorContext)) {
this.next();
}
return nodes;
}
this.next();
}
return nodes;
};
Parser.prototype.parseDotOperator = function () {
var node = {
type: 'DotOperator',
};
var prevToken = this.type_;
if (!prevToken && this.base) {
if (this.base.isMatchPattern) {
throw new Error('Base path must be an absolute path.');
}
this.data.segments = this.base.toArr();
while (this.state.type === dotTok) {
this.relative = this.data.segments.pop();
this.haveRelativePattern = true;
this.next();
}
return createTreeBySegments(this.data.segments.slice(), this.parseAtom(this.state.type));
}
else {
this.next();
}
this.append(node, this.parseAtom(this.state.type));
return node;
};
Parser.prototype.parseIgnoreExpression = function () {
this.next();
var value = String(this.state.value).replace(/\s*/g, '');
var node = {
type: 'IgnoreExpression',
value: value,
};
this.pushSegments(value);
this.next();
this.append(node, this.parseAtom(this.state.type));
this.next();
return node;
};
Parser.prototype.parseGroupExpression = function (parent) {
var node = {
type: 'GroupExpression',
value: [],
};
this.isMatchPattern = true;
this.data.segments = [];
this.next();
loop: while (true) {
switch (this.state.type) {
case commaTok:
this.next();
break;
case bangTok:
node.isExclude = true;
this.haveExcludePattern = true;
this.next();
break;
case eofTok:
break loop;
case parenRTok:
break loop;
default:
node.value.push(this.parseAtom(this.state.type));
}
}
this.next();
this.append(parent, this.parseAtom(this.state.type));
return node;
};
Parser.prototype.parseRangeExpression = function (parent) {
var node = {
type: 'RangeExpression',
};
this.next();
this.isMatchPattern = true;
this.data.segments = [];
var start = false, hasColon = false;
loop: while (true) {
switch (this.state.type) {
case colonTok:
hasColon = true;
start = true;
this.next();
break;
case bracketRTok:
if (!hasColon && !node.end) {
node.end = node.start;
}
break loop;
case commaTok:
// never reach
throw this.unexpect();
case eofTok:
// never reach
break loop;
default:
if (!start) {
node.start = this.parseAtom(this.state.type);
}
else {
node.end = this.parseAtom(this.state.type);
}
}
}
this.next();
this.append(parent, this.parseAtom(this.state.type));
return node;
};
return Parser;
}(Tokenizer));
var Matcher = /** @class */ (function () {
function Matcher(tree, record) {
this.tree = tree;
this.stack = [];
this.excluding = false;
this.wildcards = [];
this.record = record;
}
Matcher.prototype.next = function (node, pos) {
// const isOverToken = pos > this.path.length
if (node.after) {
// if (isOverToken) {
// return false
// }
return this.matchNode(node.after, pos);
}
if (isWildcardOperator(node) && !node.filter) {
if (this.excluding) {
return false;
}
else {
if (pos === 0 || node.optional)
return true;
return !!this.take(pos);
}
}
var isLastToken = pos === this.path.length - 1;
if (isLastToken) {
return !!this.take(pos);
}
else {
var wildcard = this.wildcards.pop();
if (wildcard && wildcard.after) {
return this.next(wildcard, pos);
}
}
return false;
};
Matcher.prototype.shot = function () {
var _a;
if (((_a = this.record) === null || _a === void 0 ? void 0 : _a.score) >= 0) {
this.record.score++;
}
};
Matcher.prototype.take = function (pos) {
var _a;
return String((_a = this.path[pos]) !== null && _a !== void 0 ? _a : '');
};
Matcher.prototype.matchExcludeIdentifier = function (matched, node, pos) {
var isLastToken = pos === this.path.length - 1;
var isContainToken = pos < this.path.length;
if (!node.after) {
this.excluding = false;
}
if (matched) {
if (node.after) {
return this.next(node, pos);
}
if (isLastToken) {
return false;
}
}
if (isLastToken) {
return true;
}
return isContainToken;
};
Matcher.prototype.matchIdentifier = function (node, pos) {
var current = this.take(pos);
var matched = false;
if (isExpandOperator(node.after)) {
if (current.indexOf(node.value) === 0) {
this.shot();
matched = true;
}
if (this.excluding) {
return this.matchExcludeIdentifier(matched, node.after, pos);
}
else {
return matched && this.next(node.after, pos);
}
}
else if (current === node.value) {
this.shot();
matched = true;
}
if (this.excluding) {
return this.matchExcludeIdentifier(matched, node, pos);
}
else {
return matched && this.next(node, pos);
}
};
Matcher.prototype.matchIgnoreExpression = function (node, pos) {
return isEqual(node.value, this.take(pos)) && this.next(node, pos);
};
Matcher.prototype.matchDestructorExpression = function (node, pos) {
return isEqual(node.source, this.take(pos)) && this.next(node, pos);
};
Matcher.prototype.matchExpandOperator = function (node, pos) {
return this.next(node, pos);
};
Matcher.prototype.matchWildcardOperator = function (node, pos) {
var matched = false;
if (node.filter) {
this.stack.push(node);
matched = this.matchNode(node.filter, pos);
this.stack.pop();
}
else {
matched = this.next(node, pos);
}
return matched;
};
Matcher.prototype.matchGroupExpression = function (node, pos) {
var _this = this;
var excluding = false;
if (node.isExclude) {
excluding = !this.excluding;
}
return toArr(node.value)[excluding ? 'every' : 'some'](function (item) {
_this.wildcards = _this.stack.slice();
_this.excluding = excluding;
return _this.matchNode(item, pos);
});
};
Matcher.prototype.matchRangeExpression = function (node, pos) {
var current = Number(this.take(pos));
if (node.start) {
if (node.end