MCPcopy
hub / github.com/sveltejs/kit / exec

Function exec

packages/kit/src/utils/routing.js:139–204  ·  view source on GitHub ↗
(match, params, matchers)

Source from the content-addressed store, hash-verified

137 * @param {Record<string, import('@sveltejs/kit').ParamMatcher>} matchers
138 */
139export function exec(match, params, matchers) {
140 /** @type {Record<string, string>} */
141 const result = {};
142
143 const values = match.slice(1);
144 const values_needing_match = values.filter((value) => value !== undefined);
145
146 let buffered = 0;
147
148 for (let i = 0; i < params.length; i += 1) {
149 const param = params[i];
150 let value = values[i - buffered];
151
152 // in the `[[a=b]]/.../[...rest]` case, if one or more optional parameters
153 // weren't matched, roll the skipped values into the rest
154 if (param.chained && param.rest && buffered) {
155 value = values
156 .slice(i - buffered, i + 1)
157 .filter((s) => s)
158 .join('/');
159
160 buffered = 0;
161 }
162
163 // if `value` is undefined, it means this is an optional or rest parameter
164 if (value === undefined) {
165 if (param.rest) result[param.name] = '';
166 continue;
167 }
168
169 if (!param.matcher || matchers[param.matcher](value)) {
170 result[param.name] = value;
171
172 // Now that the params match, reset the buffer if the next param isn't the [...rest]
173 // and the next value is defined, otherwise the buffer will cause us to skip values
174 const next_param = params[i + 1];
175 const next_value = values[i + 1];
176 if (next_param && !next_param.rest && next_param.optional && next_value && param.chained) {
177 buffered = 0;
178 }
179
180 // There are no more params and no more values, but all non-empty values have been matched
181 if (
182 !next_param &&
183 !next_value &&
184 Object.keys(result).length === values_needing_match.length
185 ) {
186 buffered = 0;
187 }
188 continue;
189 }
190
191 // in the `/[[a=b]]/...` case, if the value didn't satisfy the matcher,
192 // keep track of the number of skipped optional parameters and continue
193 if (param.optional && param.chained) {
194 buffered++;
195 continue;
196 }

Callers 4

routing.spec.jsFile · 0.90
parseFunction · 0.90
respondFunction · 0.90
resolve_routeFunction · 0.90

Calls 1

filterMethod · 0.65

Tested by

no test coverage detected