UNPKG

@maskedeng-tom/ssrsx

Version:
51 lines 2.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.matchPath = void 0; const log_1 = require("../../lib/log"); const path_to_regexp_1 = require("path-to-regexp"); const joinPath_1 = require("./joinPath"); //////////////////////////////////////////////////////////////////////////////// const createParams = (keys, match) => { const params = {}; for (let i = 0; i < keys.length; i++) { const key = keys[i]; params[key.name] = match[i + 1]; } return params; }; //////////////////////////////////////////////////////////////////////////////// const matchPath = (matchPath, url, option) => { try { // match const keys = []; const regexp = (0, path_to_regexp_1.pathToRegexp)(matchPath, keys, Object.assign({}, option)); const match = regexp.exec(url); if (match) { const toPath = (0, path_to_regexp_1.compile)(matchPath, { encode: encodeURIComponent }); const params = createParams(keys, match); const targetPath = toPath(params); if (targetPath !== url) { return { subMatch: true, offsetPath: targetPath, params: params }; } return { match: true, params: params }; } // sub match const subKeys = []; const subRegexp = (0, path_to_regexp_1.pathToRegexp)((0, joinPath_1.joinPath)(matchPath, '(.*)'), subKeys, Object.assign({}, option)); const subMatch = subRegexp.exec(url); if (!subMatch) { return { params: {} }; } const params = createParams(subKeys, subMatch); const toPath = (0, path_to_regexp_1.compile)(matchPath, { encode: encodeURIComponent }); const offsetPath = toPath(params); return { subMatch: true, offsetPath: offsetPath, params: params }; // } catch (e) { (0, log_1.logError)('matchPath error:', e); return { params: {} }; } }; exports.matchPath = matchPath; //# sourceMappingURL=matchPath.js.map