Source

routing/matchRoutePathname.js

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PARAM_REGEX_STR = void 0;
exports.PARAM_REGEX_STR = '[a-zA-Z0-9_-]+';
/**
 *
 * Determine if pathname and config path are equal and get the matched route pathname (config path).
 * If pathnames includes path params, it will be determined by equivalent path position,
 * e.g. "/shops/:id/settings" is equal to "/shops/123/settings"
 *
 * @param path The actual pathname displayed on browser navigation bar
 * @param route The route config path for a route of a router like React-Router
 * @param isExact
 * @category routing
 * @module matchRoutePathname
 */
var matchRoutePathname = function (path, route, isExact) {
    if (isExact === void 0) { isExact = false; }
    var pathRegxStr = route
        // Replace optional path params with regex (e.g. "/:key?", ((/[a-zA-Z0-9_-]+)?))
        .replace(new RegExp("\\/\\:".concat(exports.PARAM_REGEX_STR, "\\?"), 'gi'), "((/".concat(exports.PARAM_REGEX_STR, ")?)"))
        // Replace path params with regex (e.g. ":key" -> ([[a-zA-Z0-9_-]+]))
        .replace(new RegExp("\\:".concat(exports.PARAM_REGEX_STR), 'gi'), "(".concat(exports.PARAM_REGEX_STR, ")"));
    var endingSuffix = isExact ? '$' : '';
    var configPathRegex = new RegExp("^".concat(pathRegxStr).concat(endingSuffix), 'i');
    // Return the route if it is matched
    if (configPathRegex.test(path))
        return route;
    // Return undefined if it is not matched
    return undefined;
};
exports.default = matchRoutePathname;