MCPcopy
hub / github.com/TanStack/router / parseSegments

Function parseSegments

packages/router-core/src/new-process-route-tree.ts:195–429  ·  view source on GitHub ↗

* Recursively parses the segments of the given route tree and populates a segment trie. * * @param data A reusable Uint16Array for parsing segments. (non important, we're just avoiding allocations) * @param route The current route to parse. * @param start The starting index for parsing within th

(
  defaultCaseSensitive: boolean,
  data: Uint16Array,
  route: TRouteLike,
  start: number,
  node: AnySegmentNode<TRouteLike>,
  depth: number,
  onRoute?: (route: TRouteLike) => void,
)

Source from the content-addressed store, hash-verified

193 * @param onRoute Callback invoked for each route processed.
194 */
195function parseSegments<TRouteLike extends RouteLike>(
196 defaultCaseSensitive: boolean,
197 data: Uint16Array,
198 route: TRouteLike,
199 start: number,
200 node: AnySegmentNode<TRouteLike>,
201 depth: number,
202 onRoute?: (route: TRouteLike) => void,
203) {
204 onRoute?.(route)
205 let cursor = start
206 {
207 const path = route.fullPath ?? route.from
208 const length = path.length
209 const caseSensitive = route.options?.caseSensitive ?? defaultCaseSensitive
210 const skipOnParamError = !!(
211 route.options?.params?.parse &&
212 route.options?.skipRouteOnParseError?.params
213 )
214 while (cursor < length) {
215 const segment = parseSegment(path, cursor, data)
216 let nextNode: AnySegmentNode<TRouteLike>
217 const start = cursor
218 const end = segment[5]
219 cursor = end + 1
220 depth++
221 const kind = segment[0]
222 switch (kind) {
223 case SEGMENT_TYPE_PATHNAME: {
224 const value = path.substring(segment[2], segment[3])
225 if (caseSensitive) {
226 const existingNode = node.static?.get(value)
227 if (existingNode) {
228 nextNode = existingNode
229 } else {
230 node.static ??= new Map()
231 const next = createStaticNode<TRouteLike>(
232 route.fullPath ?? route.from,
233 )
234 next.parent = node
235 next.depth = depth
236 nextNode = next
237 node.static.set(value, next)
238 }
239 } else {
240 const name = value.toLowerCase()
241 const existingNode = node.staticInsensitive?.get(name)
242 if (existingNode) {
243 nextNode = existingNode
244 } else {
245 node.staticInsensitive ??= new Map()
246 const next = createStaticNode<TRouteLike>(
247 route.fullPath ?? route.from,
248 )
249 next.parent = node
250 next.depth = depth
251 nextNode = next
252 node.staticInsensitive.set(name, next)

Callers 3

processRouteMasksFunction · 0.85
findSingleMatchFunction · 0.85
processRouteTreeFunction · 0.85

Calls 7

parseSegmentFunction · 0.85
createStaticNodeFunction · 0.85
createDynamicNodeFunction · 0.85
toLowerCaseMethod · 0.80
endsWithMethod · 0.80
getMethod · 0.65
setMethod · 0.65

Tested by

no test coverage detected