@web-atoms/core
Version:
164 lines (163 loc) • 4.96 kB
JavaScript
System.register([], function (_export, _context) {
"use strict";
var viewModelParseWatchCache, viewModelParseWatchCache2;
function trimRegEx(t, r) {
const m = r.exec(t);
if (m && m.length) {
return [true, t.substring(m[0].length).trim(), m[0]];
}
return [false, t, ""];
}
function extractTill(text, search, returnOriginal = false) {
const index = text.indexOf(search);
if (index <= 0) {
return returnOriginal ? text : "";
}
return text.substring(0, index);
}
function parsePath(f, parseThis) {
let str = f.toString().trim();
str = str.split("\n").filter(s => !/^\/\//.test(s.trim())).join("\n");
const key = (parseThis === undefined ? "un:" : parseThis ? "_this:" : "_noThis:") + str;
const px1 = viewModelParseWatchCache[key];
if (px1) {
return px1;
}
if (str.endsWith("}")) {
str = str.substr(0, str.length - 1);
}
const functionRegEx = /^\w+\s*\(/;
const lambdaRegEx = /^\(?(\w+)?(\s?\,\s?\w+)*\)?\s?\=\>/;
let arg = "";
const original = str;
let [success, remaining] = trimRegEx(str, functionRegEx);
if (success) {
str = remaining;
remaining = extractTill(remaining, ")");
str = str.substring(remaining.length);
arg = extractTill(remaining, ",", true);
} else {
[success, str, remaining] = trimRegEx(str, lambdaRegEx);
if (success) {
remaining = remaining.trim();
remaining = remaining.substring(0, remaining.length - 2);
remaining = extractTill(remaining, ")", true);
arg = extractTill(remaining, ",", true);
if (arg.startsWith("(")) {
arg = arg.substring(1);
}
} else {
if (parseThis !== undefined && parseThis === false) {
return [];
} else {
parseThis = true;
}
}
}
str = str.trim();
const isThis = parseThis === undefined ? (arg ? false : true) || parseThis : parseThis;
const p = (isThis ? "(\\_this|this)" : arg || "").trim();
if (p.length === 0) {
const empty = [];
viewModelParseWatchCache[key] = empty;
return empty;
}
const regExp = `(?:(\\b${p})(?:(\\.[a-zA-Z_][a-zA-Z_0-9]*)+)\\s?(?:(\\(|\\=\\=\\=|\\=\\=|\\=)?))`;
const re = new RegExp(regExp, "gi");
let path = [];
const ms = str.replace(re, m => {
let px = m;
if (px.startsWith("this.")) {
if (parseThis !== true) {
px = px.substr(5);
}
} else if (px.startsWith("_this.")) {
if (parseThis !== true) {
px = px.substr(6);
} else {
px = px.substr(1);
}
} else {
px = px.substr(p.length + 1);
}
px = px.split(".").filter(s => !s.endsWith("(")).join(".");
if (!path.find(y => y === px)) {
path.push(px);
}
const filtered = [];
for (const iterator of path) {
if (iterator.endsWith("==") || !(iterator.endsWith("(") || iterator.endsWith("="))) {
filtered.push((iterator.endsWith("===") ? iterator.substr(0, iterator.length - 3) : iterator.endsWith("==") ? iterator.substr(0, iterator.length - 2) : iterator).trim());
}
}
path = filtered.filter(px11 => {
const search = px11 + ".";
for (const iterator of filtered) {
if (px11 !== iterator && iterator.indexOf(search) !== -1) {
return false;
}
}
return true;
});
return m;
});
path = path.sort((a, b) => b.localeCompare(a));
const duplicates = path;
path = [];
for (const iterator of duplicates) {
if (path.find(px2 => px2 === iterator)) {
continue;
}
path.push(iterator);
}
const rp = [];
for (const rpItem of path) {
if (rp.find(x => x.startsWith(rpItem))) {
continue;
}
rp.push(rpItem);
}
const pl = path.filter(p1 => p1).map(p1 => p1.split("."));
viewModelParseWatchCache[key] = pl;
return pl;
}
function parsePathLists(f) {
const str = f.toString().trim();
const key = str;
const px1 = viewModelParseWatchCache2[key];
if (px1) {
return px1;
}
const pl = {
pathList: parsePath(str, false),
thisPath: parsePath(str, true),
combined: []
};
if (pl.thisPath.length && pl.pathList.length) {
pl.combined = pl.thisPath.map(x => {
x[0] = "t";
x.splice(0, 0, "this");
return x;
}).concat(pl.pathList.map(x => {
x.splice(0, 0, "this", "x");
return x;
}));
pl.thisPath = [];
pl.pathList = [];
}
viewModelParseWatchCache2[key] = pl;
return pl;
}
_export({
parsePath: parsePath,
parsePathLists: parsePathLists
});
return {
setters: [],
execute: function () {
viewModelParseWatchCache = {};
viewModelParseWatchCache2 = {};
}
};
});
//# sourceMappingURL=ExpressionParser.js.map