1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | 190x 190x 190x 190x 316x 190x 190x 316x 316x 316x 316x 316x 55x 55x 55x 55x 55x 55x 55x 29x 29x 29x 261x 261x 316x 316x 190x 190x 190x 555x 555x 555x 555x 555x 1x 554x 8x 546x 546x 546x 11x 535x 5x 530x 530x 530x 530x 631x 631x 631x 631x 1166x 1166x 1166x 103x 103x 6x 97x 97x 97x 1063x 1063x 1062x 161x 901x 463x 66x 273x 273x 273x 273x 273x 383x 383x 48x 34x 14x 14x 14x 335x 259x 259x 220x 259x 16x 259x 259x 259x 261x 731x 731x 731x 731x 731x 731x 619x 112x 261x 55x 337x 337x 55x 55x 190x | import { getRegExp, getPartConstructor, convertMatchItemsToFunctions, convertGenerateItemsToFunctions } from './utils.js'; import DefaultTemplate from './DefaultTemplate.js'; import MatchFragment from './MatchFragment.js'; import PathComponent from './PathComponent.js'; import RouterError from './RouterError.js'; export default class ConstPathTemplate extends DefaultTemplate { constructor (templateUri, routeParams) { super(...arguments); const matchArray = []; const generateArray = []; const templateUriPath = templateUri.getParsedUri(this._partName); const templateUriPathComponents = templateUriPath.toArray().map((value) => new PathComponent(value)); const optionalPathParamNames = []; for (let t = 0, tl = templateUriPathComponents.length; t < tl; t++) { let matchFunctions = []; let generateFunctions = []; const templateUriPathComponent = templateUriPathComponents[t]; const pathParamMatch = templateUriPathComponent.toString().match(getRegExp('param')); if (pathParamMatch) { const pathParamName = pathParamMatch[1]; const pathParamValue = routeParams.getParam(pathParamName); matchFunctions = this._getParamMatchFunctions(pathParamName, pathParamValue); generateFunctions = this._getParamGenerateFunctions(pathParamName, pathParamValue); generateFunctions.paramName = pathParamName; const isPathParamOptional = pathParamMatch[2] === '*'; if (isPathParamOptional) { optionalPathParamNames.push(pathParamName); matchFunctions.optionalIndex = optionalPathParamNames.length; generateFunctions.optionalIndex = optionalPathParamNames.length; } } else { matchFunctions = this._getConstMatchFunctions(templateUriPathComponent); generateFunctions = this._getConstGenerateFunctions(templateUriPathComponent); } matchArray[t] = matchFunctions; generateArray[t] = generateFunctions; } this._matchArray = matchArray; this._generateArray = generateArray; this._optionalPathParamNames = optionalPathParamNames; } matchParsedValue (userUri, contextOptions) { const {partName} = contextOptions; const optionalPathParamNames = this._optionalPathParamNames; const userUriPath = userUri.getParsedUri(partName); const templateUriPath = this._templateUri.getParsedUri(partName); if (!userUriPath.isAbsolute()) { throw new RouterError(RouterError.ABSOLUTE_PATH_EXPECTED, { currentPath: userUriPath.toString() }); } if (contextOptions.trailingSlashSensitive && userUriPath.hasTrailingSlash() !== templateUriPath.hasTrailingSlash()) { return null; } const userUriPathComponentCount = userUriPath.toArray().length; const templateUriPathComponentCount = templateUriPath.toArray().length; if (userUriPathComponentCount < templateUriPathComponentCount - optionalPathParamNames.length) { return null; } if (templateUriPath.isAbsolute() && userUriPathComponentCount > templateUriPathComponentCount) { return null; } const matchArray = this._matchArray; const value = userUri.getParsedUri(partName).toString(); optionalLoop: for (let o = 0, ol = Math.pow(2, optionalPathParamNames.length); o < ol; o++) { const params = {}; let optionalOffset = 0; componentLoop: for (let m = 0, ml = matchArray.length; m < ml; m++) { const matchIndex = templateUriPath.isAbsolute() ? m : ml - 1 - m; const matchFunctions = matchArray[matchIndex]; if (matchFunctions.optionalIndex && o / Math.pow(2, optionalPathParamNames.length + 1 - matchFunctions.optionalIndex) % 1 >= 0.5) { optionalOffset--; if (templateUriPath.isAbsolute() && userUriPathComponentCount > templateUriPathComponentCount + optionalOffset) { continue optionalLoop; } else { const optionalParamName = optionalPathParamNames[matchFunctions.optionalIndex - 1]; Object.assign(params, { [optionalParamName]: '' }); continue componentLoop; } } this._currentPathComponentIndex = templateUriPath.isAbsolute() ? m + optionalOffset : userUriPathComponentCount - 1 - m - optionalOffset; const pathMatchFragment = super.matchParsedValue(userUri, contextOptions, matchFunctions); if (!pathMatchFragment) { continue optionalLoop; } Object.assign(params, pathMatchFragment.params); } return new MatchFragment(value, params); } return null; } generateParsedValue (userParams, contextOptions) { const {partName} = contextOptions; const generateArray = this._generateArray; const templateUriPath = this._templateUri.getParsedUri(partName); let rawValue = []; for (let generateFunctions of generateArray) { const pathComponent = super.generateParsedValue(userParams, contextOptions, generateFunctions); if (pathComponent.isEmpty() && generateFunctions.paramName) { if (generateFunctions.optionalIndex) { continue; } else { const {paramName} = generateFunctions; const routeName = this._routeName; throw new RouterError(RouterError.PATH_COMPONENT_EXPECTED, { paramName, routeName }); } } rawValue.push(encodeURIComponent(pathComponent.toString())); } rawValue = rawValue.join('/'); if (templateUriPath.isAbsolute()) { rawValue = '/' + rawValue; } if (templateUriPath.hasTrailingSlash()) { rawValue += '/'; } const PartConstructor = getPartConstructor(partName); const parsedValue = new PartConstructor(rawValue); return parsedValue; } _getConstMatchFunctions (templateUriPathComponent) { return [(userUri, contextOptions) => { const {partName} = contextOptions; const templateUriPathComponentString = templateUriPathComponent.toLowerCase(!contextOptions.caseSensitive).toString(); const userUriRawPathComponents = userUri.getParsedUri(partName).toArray(); const userUriPathComponent = new PathComponent(userUriRawPathComponents[this._currentPathComponentIndex]); const userUriPathComponentString = userUriPathComponent.toLowerCase(!contextOptions.caseSensitive).toString(); if (userUriPathComponentString === templateUriPathComponentString) { return new MatchFragment(userUriPathComponentString); } return null; }]; } _getConstGenerateFunctions (templateUriPathComponent) { return [(userParams) => templateUriPathComponent]; } _getParamMatchFunctions (paramName, paramValue) { const getUserUriPart = (userUri) => { const userUriPath = userUri.getParsedUri('path'); return new PathComponent(userUriPath.toArray()[this._currentPathComponentIndex]); }; return convertMatchItemsToFunctions(paramValue.match, { partName: 'pathComponent', paramName, getUserUriPart }); } _getParamGenerateFunctions (paramName, paramValue) { return convertGenerateItemsToFunctions(paramValue.generate, { partName: 'pathComponent', paramName }); } _getPartName () { return 'path'; } } |