UNPKG

@web-atoms/core

Version:
185 lines (184 loc) 6.22 kB
System.register(["./StringHelper"], function (_export, _context) { "use strict"; var StringHelper, Variable, Route; _export({ Variable: void 0, default: void 0 }); return { setters: [function (_StringHelper) { StringHelper = _StringHelper.StringHelper; }], execute: function () { _export("Variable", Variable = class Variable { get regex() { if (this.catchAll) { return `(/(?<${this.variable}>.+)?)?`; } let r = "[^\\/]{1,500}"; if (this.parseAsNumber) { r = "[0-9]{1,500}"; } if (this.optional) { return `(/(?<${this.variable}>${r}))?`; } return `/${StringHelper.escapeRegExp(this.prefix)}(?<${this.variable}>${r})${StringHelper.escapeRegExp(this.suffix)}`; } constructor(variable, name) { var _a; this.variable = variable; this.name = name; this.prefix = ""; this.suffix = ""; this.convert = v => v; if (variable.startsWith("*")) { this.catchAll = true; variable = variable.substring(1); } if (variable.endsWith("?")) { this.optional = true; variable = variable.substring(0, variable.length - 1); } const index = variable.indexOf(":"); if (index !== -1) { const parseAs = variable.substring(index + 1); variable = variable.substring(0, index); switch (parseAs) { case "number": this.parseAsNumber = true; this.convert = v => { const r = parseFloat(v); if (Number.isNaN(r)) { return void 0; } return r; }; break; case "boolean": this.convert = v => { if (v === "true") { return true; } if (v === "false") { return false; } }; break; } } this.variable = variable; (_a = this.name) !== null && _a !== void 0 ? _a : this.name = variable; } }); _export("default", Route = class Route { static encodeUrl(url) { return url; } static create(route, queries, order = 0) { if (!route.startsWith("/")) { throw new Error("String Route must start with /"); } return new Route(route, queries, order); } constructor(route, queries, order = 0) { this.route = route; this.order = order; this.queries = new Map(); this.variables = []; this.substitutions = []; if (queries) { for (const iterator of queries) { let [name, variable = name] = iterator.split("="); if (variable.startsWith("{")) { variable = variable.substring(1, variable.length - 1); } this.queries.set(name, variable); } } const tokens = route.substring(1).split(/\//g); let regex = "^"; this.substitutions.push("/"); let catchAll = false; for (const iterator of tokens) { if (regex.length > 2) { this.substitutions.push("/"); } const match = /\{(\*?[\p{L}:]{1,50}\??)\}/u.exec(iterator); if (!match) { this.substitutions.push(iterator); regex += StringHelper.escapeRegExp("/"); regex += StringHelper.escapeRegExp(iterator); continue; } const start = match.index; const index = match[0].length; const name = match[1]; const prefix = iterator.substring(0, start); const suffix = iterator.substring(index + 1); const v = new Variable(name); v.prefix = prefix; v.suffix = suffix; regex += v.regex; this.variables.push(v); this.substitutions.push(v); catchAll || (catchAll = v.catchAll); } if (!catchAll) { regex += "\\/?$"; } this.regex = new RegExp(regex, "i"); } matches(route, q) { var _a; const matches = this.regex.exec(route); if ((matches === null || matches === void 0 ? void 0 : matches.length) > 0) { const result = {}; const { groups } = matches; for (const iterator of this.variables) { const v = groups[iterator.variable]; if (v !== void 0 && v !== null) { const converted = iterator.convert(decodeURIComponent(v)); if (converted === void 0) { return null; } result[iterator.variable] = converted; } } if (q) { for (let [key, value] of q.entries()) { const variable = (_a = this.queries.get(key)) !== null && _a !== void 0 ? _a : key; result[variable] = value; } } return result; } return null; } substitute(vars) { var _a; let result = ""; for (const iterator of this.substitutions) { if (typeof iterator === "string") { result += iterator; continue; } result += (_a = vars[iterator.variable]) !== null && _a !== void 0 ? _a : ""; } if (this.queries.size > 0) { result += "?"; for (const [key, variable] of this.queries.entries()) { const value = vars[variable]; if (value) { result += `${encodeURIComponent(key)}=${encodeURIComponent(value)}&`; } } } return result; } }); } }; }); //# sourceMappingURL=Route.js.map