UNPKG

next

Version:

The React Framework

119 lines (118 loc) 3.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); 0 && (module.exports = { PARAMETER_PATTERN: null, getDynamicParam: null, parseMatchedParameter: null, parseParameter: null }); function _export(target, all) { for(var name in all)Object.defineProperty(target, name, { enumerable: true, get: all[name] }); } _export(exports, { PARAMETER_PATTERN: function() { return PARAMETER_PATTERN; }, getDynamicParam: function() { return getDynamicParam; }, parseMatchedParameter: function() { return parseMatchedParameter; }, parseParameter: function() { return parseParameter; } }); function getDynamicParam(params, segmentKey, dynamicParamType, pagePath, fallbackRouteParams) { let value = params[segmentKey]; if (fallbackRouteParams && fallbackRouteParams.has(segmentKey)) { value = fallbackRouteParams.get(segmentKey); } else if (Array.isArray(value)) { value = value.map((i)=>encodeURIComponent(i)); } else if (typeof value === 'string') { value = encodeURIComponent(value); } if (!value) { const isCatchall = dynamicParamType === 'c'; const isOptionalCatchall = dynamicParamType === 'oc'; if (isCatchall || isOptionalCatchall) { // handle the case where an optional catchall does not have a value, // e.g. `/dashboard/[[...slug]]` when requesting `/dashboard` if (isOptionalCatchall) { return { param: segmentKey, value: null, type: dynamicParamType, treeSegment: [ segmentKey, '', dynamicParamType ] }; } // handle the case where a catchall or optional catchall does not have a value, // e.g. `/foo/bar/hello` and `@slot/[...catchall]` or `@slot/[[...catchall]]` is matched value = pagePath.split('/')// remove the first empty string .slice(1)// replace any dynamic params with the actual values .flatMap((pathSegment)=>{ const param = parseParameter(pathSegment); var _params_param_key; // if the segment matches a param, return the param value // otherwise, it's a static segment, so just return that return (_params_param_key = params[param.key]) != null ? _params_param_key : param.key; }); return { param: segmentKey, value, type: dynamicParamType, // This value always has to be a string. treeSegment: [ segmentKey, value.join('/'), dynamicParamType ] }; } } return { param: segmentKey, // The value that is passed to user code. value: value, // The value that is rendered in the router tree. treeSegment: [ segmentKey, Array.isArray(value) ? value.join('/') : value, dynamicParamType ], type: dynamicParamType }; } const PARAMETER_PATTERN = /^([^[]*)\[((?:\[[^\]]*\])|[^\]]+)\](.*)$/; function parseParameter(param) { const match = param.match(PARAMETER_PATTERN); if (!match) { return parseMatchedParameter(param); } return parseMatchedParameter(match[2]); } function parseMatchedParameter(param) { const optional = param.startsWith('[') && param.endsWith(']'); if (optional) { param = param.slice(1, -1); } const repeat = param.startsWith('...'); if (repeat) { param = param.slice(3); } return { key: param, repeat, optional }; } //# sourceMappingURL=get-dynamic-param.js.map