Skip to content

Commit 5dc528f

Browse files
committed
Export static utils for path generation
1 parent 503307e commit 5dc528f

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

lib/index.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,37 @@ function otherwise(tags: string[]) {
180180
return (fn: any) => Object.fromEntries(tags.map((tag) => [tag, fn]));
181181
}
182182

183+
type AnyRoute = { type: string, tag: string, value: Record<string, any>, context: RouteContext }
184+
185+
function toPathSafeExternal(route: AnyRoute): Either<Error, string>{
186+
return toPathInternalSafe(route, route.context.parentPatterns, route.context.patterns)
187+
}
188+
189+
export { toPathSafeExternal as toPathSafe }
190+
191+
function toPathOrExternal(otherwise: string, route: AnyRoute): string {
192+
const answer = toPathInternalSafe(route, route.context.parentPatterns, route.context.patterns)
193+
if (answer.tag === 'Left') {
194+
return otherwise
195+
} else {
196+
return answer.value
197+
}
198+
}
199+
200+
export { toPathOrExternal as toPathOr }
201+
202+
function toPathExternal(route: AnyRoute): string {
203+
const answer = toPathInternalSafe(route, route.context.parentPatterns, route.context.patterns)
204+
if (answer.tag === 'Left') {
205+
throw answer.value
206+
} else {
207+
return answer.value
208+
}
209+
}
210+
211+
export { toPathExternal as toPath }
212+
213+
183214
function toPathInternalSafe(
184215
route: any,
185216
parentPatterns: string[],

0 commit comments

Comments
 (0)