Skip to content

Commit 0b27f4e

Browse files
committed
uri
1 parent 484c2a3 commit 0b27f4e

2 files changed

Lines changed: 206 additions & 171 deletions

File tree

example/uri/index.ts

Lines changed: 66 additions & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -1,194 +1,89 @@
1+
// deno-fmt-ignore-file
2+
13
import { Static } from '@sinclair/parsebox'
4+
import { TExpression, TSimple, TParam, TQuery, TFragment, TReserved, TLiteral, TLabel, TPath, TMatrix, TContinuation } from './types.ts'
5+
6+
type R = Static.Parse<Template, '/a/b/{x}{y}{+rest}{?a,v}'>[0]
27

38
// -------------------------------------------------------------------
4-
// ParamWithTypeOptional
5-
// -------------------------------------------------------------------
6-
interface ParamWithTypeOptionalMapping extends Static.IMapping {
7-
output: this['input'] extends [infer Name extends string, '?', ':', infer Type extends string]
8-
? TParam<Name, Type, false>
9-
: never
10-
}
11-
type ParamWithTypeOptional = Static.Tuple<[
12-
Static.Ident,
13-
Static.Const<'?'>,
14-
Static.Const<':'>,
15-
Static.Ident
16-
], ParamWithTypeOptionalMapping>
17-
// -------------------------------------------------------------------
18-
// ParamWithTypeOptional
19-
// -------------------------------------------------------------------
20-
interface ParamWithNameOptionalMapping extends Static.IMapping {
21-
output: this['input'] extends [infer Name extends string, '?']
22-
? TParam<Name, 'string', false>
23-
: never
24-
}
25-
type ParamWithNameOptional = Static.Tuple<[
26-
Static.Ident,
27-
Static.Const<'?'>
28-
], ParamWithNameOptionalMapping>
29-
// -------------------------------------------------------------------
30-
// ParamWithTypeOptional
31-
// -------------------------------------------------------------------
32-
interface ParamWithTypeMapping extends Static.IMapping {
33-
output: this['input'] extends [infer Name extends string, ':',infer Type extends string]
34-
? TParam<Name, Type, true>
35-
: never
36-
}
37-
type ParamWithType = Static.Tuple<[
38-
Static.Ident,
39-
Static.Const<':'>,
40-
Static.Ident
41-
], ParamWithTypeMapping>
42-
// -------------------------------------------------------------------
43-
// ParamWithName
9+
// ParamList
4410
// -------------------------------------------------------------------
45-
interface ParamWithNameMapping extends Static.IMapping {
46-
output: this['input'] extends [infer Name extends string]
47-
? TParam<Name, 'string', true>
48-
: never
11+
interface ParamListMapping extends Static.IMapping {
12+
output: (
13+
this['input'] extends [infer Param extends TParam, ',', infer Rest extends TParam[]] ? [Param, ...Rest] :
14+
this['input'] extends [infer Param extends TParam] ? [Param] :
15+
[]
16+
)
4917
}
50-
type ParamWithName = Static.Tuple<[
51-
Static.Ident
52-
], ParamWithNameMapping>
18+
type ParamList = Static.Union<[
19+
Static.Tuple<[Param, Static.Const<','>, ParamList]>,
20+
Static.Tuple<[Param]>,
21+
Static.Tuple<[]>
22+
], ParamListMapping>
5323
// -------------------------------------------------------------------
5424
// Param
5525
// -------------------------------------------------------------------
56-
type Param = Static.Union<[
57-
ParamWithTypeOptional,
58-
ParamWithNameOptional,
59-
ParamWithType,
60-
ParamWithName
61-
]>
62-
// -------------------------------------------------------------------
63-
// ParamExpression
64-
// -------------------------------------------------------------------
65-
interface ParamExpressionMapping extends Static.IMapping {
66-
output: this['input'] extends ['{', infer Param extends TParam, '}'] ? Param : never
67-
}
68-
type ParamExpr = Static.Tuple<[
69-
Static.Const<'{'>,
70-
Param,
71-
Static.Const<'}'>
72-
],ParamExpressionMapping>
73-
// -------------------------------------------------------------------
74-
// RestExpression
75-
// -------------------------------------------------------------------
76-
interface RestExpressionMapping extends Static.IMapping {
77-
output: this['input'] extends ['{', '+', infer Param extends TParam, '}'] ? Param : never
78-
}
79-
type RestExpr = Static.Tuple<[
80-
Static.Const<'{'>,
81-
Static.Const<'+'>,
82-
Param,
83-
Static.Const<'}'>
84-
], RestExpressionMapping>
85-
// -------------------------------------------------------------------
86-
// QueryExpression
87-
// -------------------------------------------------------------------
88-
interface QueryExpressionMapping extends Static.IMapping {
89-
output: this['input'] extends ['{', '?', infer Param extends TParam, '}'] ? Param : never
90-
}
91-
type QueryExpr = Static.Tuple<[
92-
Static.Const<'{'>,
93-
Static.Const<'?'>,
94-
Param,
95-
Static.Const<'}'>
96-
], QueryExpressionMapping>
97-
// -------------------------------------------------------------------
98-
// Expr
99-
// -------------------------------------------------------------------
100-
type Expr = Static.Union<[ParamExpr, RestExpr, QueryExpr]>
101-
// -------------------------------------------------------------------
102-
// Span
103-
// -------------------------------------------------------------------
104-
interface SpanMapping extends Static.IMapping {
105-
output: this['input'] extends infer Text extends string ? TSpan<Text> : never
26+
interface ParamMapping extends Static.IMapping {
27+
output: (
28+
this['input'] extends [infer Name extends string, ':', infer Type extends string] ? TParam<Name, Type> :
29+
this['input'] extends [infer Name extends string] ? TParam<Name, 'string'> :
30+
never
31+
)
10632
}
107-
type Span = Static.Until_1<['{'], SpanMapping>
33+
type Param = Static.Union<[
34+
Static.Tuple<[Static.Ident, Static.Const<':'>, Static.Ident]>,
35+
Static.Tuple<[Static.Ident]>
36+
], ParamMapping>
37+
// -------------------------------------------------------------------
38+
// Expression
39+
// -------------------------------------------------------------------
40+
export interface ExpressionMapping extends Static.IMapping {
41+
output: (
42+
this['input'] extends ['{', '+', infer Params extends TParam[], '}'] ? TReserved<Params> :
43+
this['input'] extends ['{', '#', infer Params extends TParam[], '}'] ? TFragment<Params> :
44+
this['input'] extends ['{', '.', infer Params extends TParam[], '}'] ? TLabel<Params> :
45+
this['input'] extends ['{', '/', infer Params extends TParam[], '}'] ? TPath<Params> :
46+
this['input'] extends ['{', ';', infer Params extends TParam[], '}'] ? TMatrix<Params> :
47+
this['input'] extends ['{', '?', infer Params extends TParam[], '}'] ? TQuery<Params> :
48+
this['input'] extends ['{', '&', infer Params extends TParam[], '}'] ? TContinuation<Params> :
49+
this['input'] extends ['{', infer Params extends TParam[], '}'] ? TSimple<Params> :
50+
this['input'] extends [infer Literal extends TLiteral] ? Literal :
51+
never
52+
)
53+
}
54+
type Expression = Static.Union<[
55+
Static.Tuple<[Static.Const<'{'>, Static.Const<'+'>, ParamList, Static.Const<'}'>]>,
56+
Static.Tuple<[Static.Const<'{'>, Static.Const<'#'>, ParamList, Static.Const<'}'>]>,
57+
Static.Tuple<[Static.Const<'{'>, Static.Const<'.'>, ParamList, Static.Const<'}'>]>,
58+
Static.Tuple<[Static.Const<'{'>, Static.Const<'/'>, ParamList, Static.Const<'}'>]>,
59+
Static.Tuple<[Static.Const<'{'>, Static.Const<';'>, ParamList, Static.Const<'}'>]>,
60+
Static.Tuple<[Static.Const<'{'>, Static.Const<'?'>, ParamList, Static.Const<'}'>]>,
61+
Static.Tuple<[Static.Const<'{'>, Static.Const<'&'>, ParamList, Static.Const<'}'>]>,
62+
Static.Tuple<[Static.Const<'{'>, ParamList, Static.Const<'}'>]>,
63+
Static.Tuple<[Literal]>
64+
], ExpressionMapping>
65+
10866
// -------------------------------------------------------------------
109-
// Span
67+
// Literal
11068
// -------------------------------------------------------------------
111-
interface RestMapping extends Static.IMapping {
112-
output: this['input'] extends infer Text extends string ? TSpan<Text> : never
69+
interface LiteralMapping extends Static.IMapping {
70+
output: this['input'] extends infer Text extends string ? TLiteral<Text> : never
11371
}
114-
type Rest = Static.Rest<RestMapping>
72+
type Literal = Static.Union<[
73+
Static.Until_1<['{']>,
74+
Static.Rest
75+
], LiteralMapping>
11576
// -------------------------------------------------------------------
116-
// Element
77+
// Template
11778
// -------------------------------------------------------------------
118-
type Element = Static.Union<[Expr, Span, Rest]>
119-
12079
interface TemplateMapping extends Static.IMapping {
121-
output: this['input'] extends [infer Left extends TKind, infer Right extends TKind[]]
80+
output: this['input'] extends [infer Left extends TExpression, infer Right extends TExpression[]]
12281
? [Left, ...Right]
12382
: []
12483
}
12584
type Template = Static.Union<[
126-
Static.Tuple<[Element, Template]>,
85+
Static.Tuple<[Expression, Template]>,
12786
Static.Tuple<[]>
12887
], TemplateMapping>
12988

130-
type R = Static.Parse<Template, '/foo/{bar:i32=10}/{baz}asd{?a,b,c}'>[0]
13189

132-
// ------------------------------------------------------------------
133-
// Kind
134-
// ------------------------------------------------------------------
135-
export interface TKind {
136-
kind: string
137-
}
138-
export function IsKind(value: unknown, kind: string): value is TRest {
139-
return typeof value === 'object' && value !== null && 'kind' in value && value.kind === kind
140-
}
141-
// ------------------------------------------------------------------
142-
// Query
143-
// ------------------------------------------------------------------
144-
export interface TQuery<Params extends TParam[] = TParam[]> extends TKind {
145-
kind: 'Query'
146-
params: Params
147-
}
148-
export function Query<Params extends TParam[] = TParam[]>(params: Params): TQuery<Params> {
149-
return { kind: 'Query', params }
150-
}
151-
export function IsQuery(value: unknown): value is TQuery {
152-
return IsKind(value, 'Query')
153-
}
154-
// ------------------------------------------------------------------
155-
// Span
156-
// ------------------------------------------------------------------
157-
export interface TSpan<Text extends string = string> extends TKind {
158-
kind: 'Span'
159-
text: Text
160-
}
161-
export function Span<Text extends string>(text: Text): TSpan<Text> {
162-
return { kind: 'Span', text }
163-
}
164-
export function IsSpan(value: unknown): value is TSpan {
165-
return IsKind(value, 'Span')
166-
}
167-
// ------------------------------------------------------------------
168-
// Param
169-
// ------------------------------------------------------------------
170-
export interface TParam<Name extends string = string, Type extends string = string, Required extends boolean = boolean> extends TKind {
171-
kind: 'Param'
172-
name: Name
173-
type: Type
174-
required: Required
175-
}
176-
export function Param<Name extends string, Type extends string, Required extends boolean>(name: Name, type: Type, required: Required): TParam<Name, Type, Required> {
177-
return { kind: 'Param', name, type, required }
178-
}
179-
export function IsParam(value: unknown): value is TParam {
180-
return IsKind(value, 'Param')
181-
}
182-
// ------------------------------------------------------------------
183-
// Rest
184-
// ------------------------------------------------------------------
185-
export interface TRest<Name extends string = string> extends TKind {
186-
kind: 'Rest'
187-
name: Name
188-
}
189-
export function Rest<Name extends string>(name: Name): TRest<Name> {
190-
return { kind: 'Rest', name }
191-
}
192-
export function IsRest(value: unknown): value is TRest {
193-
return IsKind(value, 'Rest')
194-
}

0 commit comments

Comments
 (0)