Skip to content

Commit ba33e46

Browse files
committed
chore: format code
1 parent 7afbddb commit ba33e46

File tree

3 files changed

+156
-155
lines changed

3 files changed

+156
-155
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ flatten_route_config(route_config);
306306
are removed from the stored path before link generation.
307307
- Constraint sections in paths are also removed before parameter replacement.
308308
- Path params are URL-encoded by the default `replace_params` function.
309-
- Query params are stringified by the default `format_qs` function.
309+
- Query params are stringified by the default `format_qs` function.
310310

311311
## License
312312

src/link_generator.ts

Lines changed: 117 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import type {
2-
DefaultParamValue,
3-
FlatRouteConfig,
4-
FlatRoutes,
5-
FormatFn,
6-
LinkGenerator,
7-
LinkGeneratorOptions,
8-
Param,
9-
ParamArgs,
10-
QueryArg,
11-
RouteConfig,
12-
RouteContextInit,
2+
DefaultParamValue,
3+
FlatRouteConfig,
4+
FlatRoutes,
5+
FormatFn,
6+
LinkGenerator,
7+
LinkGeneratorOptions,
8+
Param,
9+
ParamArgs,
10+
QueryArg,
11+
RouteConfig,
12+
RouteContextInit,
1313
} from "./types.ts";
1414
import { Symbols } from "./symbols.ts";
1515

@@ -24,9 +24,9 @@ import { Symbols } from "./symbols.ts";
2424
* console.log(params.get('category')); // 'books'
2525
*/
2626
export class ParamsContext extends Map<string, DefaultParamValue> {
27-
constructor(params: Param = {}) {
28-
super(Object.entries(params));
29-
}
27+
constructor(params: Param = {}) {
28+
super(Object.entries(params));
29+
}
3030
}
3131

3232
/**
@@ -42,19 +42,19 @@ export class ParamsContext extends Map<string, DefaultParamValue> {
4242
* console.log(query.toString()); // 'lang=en&page=2'
4343
*/
4444
export class QueryContext extends URLSearchParams {
45-
constructor(...query_params: QueryArg[]) {
46-
const initial_params: [string, string][] = [];
47-
48-
for (const q of query_params) {
49-
for (const [key, value] of Object.entries(q)) {
50-
if (value !== undefined) {
51-
initial_params.push([key, String(value)]);
52-
}
53-
}
54-
}
55-
56-
super(initial_params);
57-
}
45+
constructor(...query_params: QueryArg[]) {
46+
const initial_params: [string, string][] = [];
47+
48+
for (const q of query_params) {
49+
for (const [key, value] of Object.entries(q)) {
50+
if (value !== undefined) {
51+
initial_params.push([key, String(value)]);
52+
}
53+
}
54+
}
55+
56+
super(initial_params);
57+
}
5858
}
5959

6060
/**
@@ -72,17 +72,17 @@ export class QueryContext extends URLSearchParams {
7272
* @property query - A normalized query object that combines all query fragments.
7373
*/
7474
export class RouteContext<Config extends RouteConfig = RouteConfig> {
75-
readonly id: keyof FlatRoutes<Config>;
76-
path: string;
77-
params: ParamsContext;
78-
query: QueryContext;
79-
80-
constructor(id: keyof FlatRoutes<Config>, init?: RouteContextInit) {
81-
this.id = id;
82-
this.path = init?.path ?? "";
83-
this.params = init?.params ?? new ParamsContext();
84-
this.query = init?.query ?? new QueryContext();
85-
}
75+
readonly id: keyof FlatRoutes<Config>;
76+
path: string;
77+
params: ParamsContext;
78+
query: QueryContext;
79+
80+
constructor(id: keyof FlatRoutes<Config>, init?: RouteContextInit) {
81+
this.id = id;
82+
this.path = init?.path ?? "";
83+
this.params = init?.params ?? new ParamsContext();
84+
this.query = init?.query ?? new QueryContext();
85+
}
8686
}
8787

8888
/**
@@ -131,40 +131,40 @@ export class RouteContext<Config extends RouteConfig = RouteConfig> {
131131
* link('posts', undefined, { page: 2 }); // => /posts?page=2
132132
*/
133133
export function link_generator<Config extends RouteConfig>(
134-
route_config: Config,
135-
options?: LinkGeneratorOptions<Config>,
134+
route_config: Config,
135+
options?: LinkGeneratorOptions<Config>,
136136
): LinkGenerator<FlatRoutes<Config>> {
137-
const routes = create_routes_map(flatten_route_config(route_config));
138-
const transforms = options?.transforms ?? [];
139-
const format_qs_fn: FormatFn<Config> = options?.format_qs_fn ?? format_qs;
140-
const replace_params_fn: FormatFn<Config> =
141-
options?.replace_params_fn ?? replace_params;
142-
143-
return <RouteId extends keyof FlatRoutes<Config>>(
144-
route_id: RouteId,
145-
...params: ParamArgs<FlatRoutes<Config>, RouteId>
146-
): string => {
147-
const path = routes.get(route_id) as string;
148-
const [path_params, ...query_params] = params;
149-
const ctx = new RouteContext<Config>(route_id, {
150-
path,
151-
params: new ParamsContext(path_params ?? {}),
152-
query: new QueryContext(...(query_params as QueryArg[])),
153-
});
154-
155-
for (const transform of transforms) {
156-
transform(ctx);
157-
}
158-
159-
let replaced = replace_params_fn(ctx);
160-
const qs = format_qs_fn(ctx);
161-
162-
if (qs !== "") {
163-
replaced += "?";
164-
}
165-
166-
return replaced + qs;
167-
};
137+
const routes = create_routes_map(flatten_route_config(route_config));
138+
const transforms = options?.transforms ?? [];
139+
const format_qs_fn: FormatFn<Config> = options?.format_qs_fn ?? format_qs;
140+
const replace_params_fn: FormatFn<Config> = options?.replace_params_fn ??
141+
replace_params;
142+
143+
return <RouteId extends keyof FlatRoutes<Config>>(
144+
route_id: RouteId,
145+
...params: ParamArgs<FlatRoutes<Config>, RouteId>
146+
): string => {
147+
const path = routes.get(route_id) as string;
148+
const [path_params, ...query_params] = params;
149+
const ctx = new RouteContext<Config>(route_id, {
150+
path,
151+
params: new ParamsContext(path_params ?? {}),
152+
query: new QueryContext(...(query_params as QueryArg[])),
153+
});
154+
155+
for (const transform of transforms) {
156+
transform(ctx);
157+
}
158+
159+
let replaced = replace_params_fn(ctx);
160+
const qs = format_qs_fn(ctx);
161+
162+
if (qs !== "") {
163+
replaced += "?";
164+
}
165+
166+
return replaced + qs;
167+
};
168168
}
169169

170170
/**
@@ -202,84 +202,85 @@ export function link_generator<Config extends RouteConfig>(
202202
* // }
203203
*/
204204
export function flatten_route_config(
205-
route_config: RouteConfig,
206-
parent_path = "",
207-
result: FlatRouteConfig = {},
205+
route_config: RouteConfig,
206+
parent_path = "",
207+
result: FlatRouteConfig = {},
208208
): FlatRouteConfig {
209-
for (const parent_route_name in route_config) {
210-
const route = route_config[parent_route_name];
211-
const current_path = remove_query_area(route.path);
212-
const path_with_parent = `${parent_path}${current_path}`;
209+
for (const parent_route_name in route_config) {
210+
const route = route_config[parent_route_name];
211+
const current_path = remove_query_area(route.path);
212+
const path_with_parent = `${parent_path}${current_path}`;
213213

214-
result[parent_route_name] = path_with_parent;
214+
result[parent_route_name] = path_with_parent;
215215

216-
if (route.children) {
217-
const children = flatten_route_config(route.children, path_with_parent);
216+
if (route.children) {
217+
const children = flatten_route_config(route.children, path_with_parent);
218218

219-
for (const child_route_name in children) {
220-
const child_route_id = `${parent_route_name}${Symbols.PathSeparater}${child_route_name}`;
219+
for (const child_route_name in children) {
220+
const child_route_id =
221+
`${parent_route_name}${Symbols.PathSeparater}${child_route_name}`;
221222

222-
result[child_route_id] = children[child_route_name];
223-
}
224-
}
225-
}
223+
result[child_route_id] = children[child_route_name];
224+
}
225+
}
226+
}
226227

227-
return result;
228+
return result;
228229
}
229230

230231
function remove_query_area(path: string): string {
231-
const starting_query_index = path.indexOf(Symbols.Query);
232-
const is_include_query = starting_query_index > 0;
232+
const starting_query_index = path.indexOf(Symbols.Query);
233+
const is_include_query = starting_query_index > 0;
233234

234-
return is_include_query ? path.slice(0, starting_query_index) : path;
235+
return is_include_query ? path.slice(0, starting_query_index) : path;
235236
}
236237

237238
function create_routes_map(flat_route: FlatRouteConfig) {
238-
const routes_map = new Map();
239+
const routes_map = new Map();
239240

240-
for (const [route_id, path_template] of Object.entries(flat_route)) {
241-
let path_to_format = path_template;
241+
for (const [route_id, path_template] of Object.entries(flat_route)) {
242+
let path_to_format = path_template;
242243

243-
if (has_constraint_area(path_template)) {
244-
path_to_format = remove_constraint_area(path_template);
245-
}
244+
if (has_constraint_area(path_template)) {
245+
path_to_format = remove_constraint_area(path_template);
246+
}
246247

247-
routes_map.set(route_id, path_to_format);
248-
}
248+
routes_map.set(route_id, path_to_format);
249+
}
249250

250-
return routes_map;
251+
return routes_map;
251252
}
252253

253254
function has_constraint_area(path: string): boolean {
254-
const constraint_open_index = path.indexOf(Symbols.ConstraintOpen);
255+
const constraint_open_index = path.indexOf(Symbols.ConstraintOpen);
255256

256-
return constraint_open_index > 0;
257+
return constraint_open_index > 0;
257258
}
258259

259260
function remove_constraint_area(path: string): string {
260-
const constraint_area = new RegExp(
261-
`${Symbols.ConstraintOpen}.*?${Symbols.ConstraintClose}`,
262-
"g",
263-
);
261+
const constraint_area = new RegExp(
262+
`${Symbols.ConstraintOpen}.*?${Symbols.ConstraintClose}`,
263+
"g",
264+
);
264265

265-
return path.replace(constraint_area, "");
266+
return path.replace(constraint_area, "");
266267
}
267268

268269
// Default parameter replacer that uses a regular expression to find parameter placeholders in the path and replace them with the corresponding values from the context.
269270
export function replace_params(ctx: RouteContext): string {
270-
const param_area_regex = new RegExp(
271-
`(?<=${Symbols.PathSeparater})${Symbols.PathParam}([^\\${Symbols.PathSeparater}?]+)`,
272-
"g",
273-
);
271+
const param_area_regex = new RegExp(
272+
`(?<=${Symbols.PathSeparater})${Symbols.PathParam}([^\\${Symbols.PathSeparater}?]+)`,
273+
"g",
274+
);
274275

275-
return ctx.path.replace(param_area_regex, (_, param_name) => {
276-
const param_value = ctx.params.get(param_name) ?? "";
276+
return ctx.path.replace(param_area_regex, (_, param_name) => {
277+
const param_value = ctx.params.get(param_name) ?? "";
277278

278-
return encodeURIComponent(param_value);
279-
});
279+
return encodeURIComponent(param_value);
280+
});
280281
}
281282

282283
// Default query string formatter that simply converts the query parameters to a string using URLSearchParams.
283284
export function format_qs(ctx: RouteContext): string {
284-
return ctx.query.toString();
285+
return ctx.query.toString();
285286
}

0 commit comments

Comments
 (0)