Source

routing/replacePathParams.js

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var isArr_1 = __importDefault(require("../array/isArr"));
// The prefix of the params in pathname.
// E.g. /shop/:id/home
// ** Note that this is defined by React-router
var PARAM_PFX = ':';
var PARAM_REGEX = new RegExp("".concat(PARAM_PFX, "[a-z0-9-_\\?]+"), 'i');
/**
 * Replace the route pathname (config path) with params.
 * The length of the keys
 *
 * @param path The route config path for a route of a router like React-Router
 * @param params The param or an array of params in order.
 *   e.g. For /shops/:id/orders/:id -> ['shop123', 'order4325']
 * @category routing
 * @module replacePathParams
 */
function replacePathParams(path, params) {
    var thisParams = (0, isArr_1.default)(params) ? params : [params];
    return thisParams
        .reduce(function (prev, param) { return prev.replace(PARAM_REGEX, "".concat(param)); }, path);
}
exports.default = replacePathParams;