UNPKG

geostyler

Version:
159 lines (158 loc) 4.84 kB
import { isGeoStylerStringFunction as u, isGeoStylerNumberFunction as l, isGeoStylerBooleanFunction as p, isGeoStylerUnknownFunction as h, isGeoStylerFunction as o } from "geostyler-style"; class n { static evaluateFunction(e, t) { var r, a; if ((e == null ? void 0 : e.name) === "property") { if (!t) throw new Error(`Could not evalute 'property' function. Feature ${t} is not defined.`); if (u(e.args[0]) && (t != null && t.properties)) { const s = n.evaluateStringFunction(e.args[0], t); return (r = t == null ? void 0 : t.properties) == null ? void 0 : r[s]; } else { const s = e.args[0]; return (a = t == null ? void 0 : t.properties) == null ? void 0 : a[s]; } } if (u(e)) return n.evaluateStringFunction(e, t); if (l(e)) return n.evaluateNumberFunction(e, t); if (p(e)) return n.evaluateBooleanFunction(e, t); if (h(e)) return n.evaluateUnknownFunction(e, t); } static evaluateBooleanFunction(e, t) { const r = e.args.map((a) => o(a) ? n.evaluateFunction(a, t) : a); switch (e.name) { case "between": return r[0] >= r[1] && r[0] <= r[2]; case "double2bool": return Math.round(r[0]) === 1; case "in": return r.slice(1).includes(r[0]); case "parseBoolean": return !!r[0]; case "strEndsWith": return r[0].endsWith(r[1]); case "strEqualsIgnoreCase": return r[0].toLowerCase() === r[1].toLowerCase(); case "strMatches": { const s = r[1].match(/\/(.*?)\/([gimy]{0,4})$/); return s && s.length === 3 ? new RegExp(s[1], s[2]).test(r[0]) : !1; } case "strStartsWith": return r[0].startsWith(r[1]); default: return !1; } } static evaluateNumberFunction(e, t) { if (e.name === "pi") return Math.PI; if (e.name === "random") return Math.random(); const r = e.args.map((a) => o(a) ? n.evaluateFunction(a, t) : a); switch (e.name) { case "abs": return Math.abs(r[0]); case "acos": return Math.acos(r[0]); case "asin": return Math.asin(r[0]); case "atan": return Math.atan(r[0]); case "atan2": return r[0]; case "ceil": return Math.ceil(r[0]); case "cos": return Math.cos(r[0]); case "exp": return Math.exp(r[0]); case "floor": return Math.floor(r[0]); case "log": return Math.log(r[0]); case "max": return Math.max(...r); case "min": return Math.min(...r); case "modulo": return r[0] % r[1]; case "pow": return Math.pow(r[0], r[1]); case "rint": return r[0]; case "round": return Math.round(r[0]); case "sin": return Math.sin(r[0]); case "sqrt": return Math.sqrt(r[0]); case "strIndexOf": return r[0].indexOf(r[1]); case "strLastIndexOf": return r[0].lastIndexOf(r[1]); case "strLength": return r[0].length; case "tan": return Math.tan(r[0]); case "toDegrees": return r[0] * (180 / Math.PI); case "toRadians": return r[0] * (Math.PI / 180); default: return r[0]; } } static evaluateUnknownFunction(e, t) { var a; const r = e.args.map((s) => o(s) ? n.evaluateFunction(s, t) : s); switch (e.name) { case "property": return (a = t == null ? void 0 : t.properties) == null ? void 0 : a[r[0]]; default: return r[0]; } } static evaluateStringFunction(e, t) { var a; const r = e.args.map((s) => o(s) ? n.evaluateFunction(s, t) : s); switch (e.name) { case "numberFormat": return r[0]; case "strAbbreviate": return r[0]; case "strCapitalize": { const s = r[0].toLowerCase().split(" "), i = []; for (const c of s) i.push(c.charAt(0).toUpperCase() + c.substring(1)); return i.join(" "); } case "strConcat": return r.join(""); case "strDefaultIfBlank": return !r[0] || ((a = r[0]) == null ? void 0 : a.length) < 1 ? r[1] : r[0]; case "strReplace": return r[3] === !0 ? r[0].replaceAll(r[1], r[2]) : r[0].replace(r[1], r[2]); case "strStripAccents": return r[0].normalize("NFKD").replace(/[\u0300-\u036f]/g, ""); case "strSubstring": return r[0].substring(r[1], r[2]); case "strSubstringStart": return r[0].substring(r[1]); case "strToLowerCase": return r[0].toLowerCase(); case "strToUpperCase": return r[0].toUpperCase(); case "strTrim": return r[0].trim(); default: return r[0]; } } } export { n as default };