Skip to content

Commit

Permalink
fix: resolve tree node value options
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Dec 14, 2024
1 parent ef056ab commit 1bf080a
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/core/treeNodeValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,27 @@ export interface TreeNodeValueOptions extends ParseSegmentOptions {
* structure (e.g. `index`, ``, or `users/[id]`). In `path` format, routes are expected in the format of vue-router
* (e.g. `/` or '/users/:id' ).
*
* @default 'file'
* @default `'file'`
*/
format?: 'file' | 'path'
}

/**
* Resolves the options for the TreeNodeValue.
*
* @param options - options to resolve
* @returns resolved options
*/
function resolveTreeNodeValueOptions(
options: TreeNodeValueOptions
): Required<TreeNodeValueOptions> {
return {
format: 'file',
dotNesting: true,
...options,
}
}

/**
* Creates a new TreeNodeValue based on the segment. The result can be a static segment or a param segment.
*
Expand All @@ -224,12 +240,15 @@ export interface TreeNodeValueOptions extends ParseSegmentOptions {
export function createTreeNodeValue(
segment: string,
parent?: TreeNodeValue,
options: TreeNodeValueOptions = {}
opts: TreeNodeValueOptions = {}
): TreeNodeValue {
if (!segment || segment === 'index') {
return new TreeNodeValueStatic(segment, parent, '')
}

// ensure default options
const options = resolveTreeNodeValueOptions(opts)

const [pathSegment, params, subSegments] =
options.format === 'path'
? parseRawPathSegment(segment)
Expand Down Expand Up @@ -264,6 +283,7 @@ export interface ParseSegmentOptions {
/**
* Should we allow dot nesting in the param name. e.g. `users.[id]` will be parsed as `users/[id]` if this is `true`,
* nesting. Note this only works for the `file` format.
*
* @default `true`
*/
dotNesting?: boolean
Expand Down

0 comments on commit 1bf080a

Please sign in to comment.