realm-object-server
Version:
38 lines • 1.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const pathToRegexp = require("path-to-regexp");
const errors = require("../errors");
function decodeParam(param) {
try {
return decodeURIComponent(param);
}
catch (_) {
throw new errors.JSONError({
status: 400,
title: `Failed to decode param ${param}`
});
}
}
function pathMatcher(pathPattern, pathToTest, options = {
strict: false
}) {
const keys = [];
const re = pathToRegexp(pathPattern, keys, options);
const params = {};
const m = re.exec(pathToTest);
if (!m)
return false;
let key, param;
for (let i = 0; i < keys.length; i++) {
key = keys[i];
param = m[i + 1];
if (!param)
continue;
params[key.name] = decodeParam(param);
if (key.repeat)
params[key.name] = params[key.name].split(key.delimiter);
}
return params;
}
exports.pathMatcher = pathMatcher;
//# sourceMappingURL=pathMatcher.js.map