@camar/http
Version:
To use __superconductor__ you need to build sources by `yarn build`
65 lines (64 loc) • 1.91 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
class Template {
/*
* Путь может создержать wildcards
*/
constructor(draft) {
this.stringRepresentation = this.normalizeTemplate(draft);
this.regexp = this.buildExpression();
}
normalizeTemplate(rawTemplate) {
if (rawTemplate[0] == '/') {
rawTemplate = rawTemplate.slice(1);
}
const li = rawTemplate.length - 1;
if (rawTemplate[li] == '/') {
rawTemplate = rawTemplate.slice(0, li);
}
return rawTemplate;
}
buildExpression() {
let re = '';
const spl = this.stringRepresentation.split('/');
if (spl.length > 0) {
re += '^';
spl.forEach(s => {
re += '/';
if (s.startsWith('{')) {
s = s.slice(1);
s = s.slice(0, s.length - 1);
re += `(?<${s}>[a-zA-Zа-яёА-ЯЁ0-9:#_%\\.\\-]+)`;
}
else {
re += s;
}
});
re += '($|\/|\\?{1}.+)';
}
return new RegExp(re);
}
getParams(pathname) {
const decodedPathname = decodeURI(pathname);
if (this.check(decodedPathname)) {
const t = this.regexp.exec(decodedPathname);
if (!t.groups) {
return {};
}
for (const key of Object.keys(t.groups)) {
t.groups[key] = decodeURIComponent(t.groups[key]);
}
return t.groups;
}
else {
return null;
}
}
check(pathname) {
return !!this.regexp.exec(pathname);
}
get length() {
return this.stringRepresentation.replace(/\{[^}]+\}/g, '').length;
}
}
exports.default = Template;
;