Skip to content

Commit

Permalink
Fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
etienne-dldc committed Jan 14, 2025
1 parent 16862f1 commit ff45a25
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 42 deletions.
20 changes: 11 additions & 9 deletions src/chemin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const chemin: TCreateChemin = cheminFactory();
* @returns
*/
export function cheminFactory(
defaultSerializeOptions: ISlashOptions = {}
defaultSerializeOptions: ISlashOptions = {},
): TCreateChemin {
function createChemin<Args extends readonly TIn[]>(
...fragments: Args
Expand Down Expand Up @@ -70,10 +70,11 @@ export function cheminFactory(
*/
export function match<Params>(
chemin: IChemin<Params>,
pathname: string | Array<string>
pathname: string | Array<string>,
): TCheminMatchMaybe<Params> {
const pathParts =
typeof pathname === "string" ? splitPathname(pathname) : pathname;
const pathParts = typeof pathname === "string"
? splitPathname(pathname)
: pathname;
return matchPart(chemin, pathParts);
}

Expand All @@ -82,7 +83,7 @@ export function match<Params>(
*/
export function matchExact<Params>(
chemin: IChemin<Params>,
pathname: string | Array<string>
pathname: string | Array<string>,
): null | Params {
const matchResult = match(chemin, pathname);
if (matchResult && matchResult.rest.length === 0) {
Expand All @@ -98,11 +99,12 @@ export function serialize<Params>(
chemin: IChemin<Params>,
// deno-lint-ignore ban-types
params: {} extends Params ? null | undefined : Params,
options: ISlashOptions = {}
options: ISlashOptions = {},
): string {
const { leadingSlash = true, trailingSlash = false } = options;
const paramsResolved: any =
params === null || params === undefined ? {} : params;
const paramsResolved: any = params === null || params === undefined
? {}
: params;

const result = chemin.parts
.map((part) => {
Expand Down Expand Up @@ -132,7 +134,7 @@ export function serialize<Params>(
*/
export function stringify(
chemin: IChemin<any>,
options: ISlashOptions
options: ISlashOptions,
): string {
const { leadingSlash = true, trailingSlash = false } = options;
const result = chemin.parts
Expand Down
4 changes: 2 additions & 2 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function isChemin(maybe: any): maybe is IChemin<any> {
*/
export function matchPart(
part: TPart,
pathname: readonly string[]
pathname: readonly string[],
): ICheminMatch<any> | null {
if (isChemin(part)) {
const match = matchParts(part.parts, pathname);
Expand Down Expand Up @@ -60,7 +60,7 @@ export function matchPart(
*/
export function matchParts(
parts: ReadonlyArray<TPart>,
pathname: readonly string[]
pathname: readonly string[],
): ICheminMatch<any> | null {
if (parts.length === 0) {
return { params: {}, rest: pathname, exact: pathname.length === 0 };
Expand Down
16 changes: 8 additions & 8 deletions src/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export function pInteger<N extends string>(
name: N,
options: {
strict?: boolean;
} = {}
} = {},
): TCheminParam<N, number, { strict: boolean }> {
const { strict = true } = options;
return {
Expand All @@ -91,12 +91,12 @@ export function pInteger<N extends string>(
serialize: (value) => {
if (typeof value !== "number") {
throw new Error(
`CheminParam.interger expect an interger when serializing`
`CheminParam.interger expect an interger when serializing`,
);
}
if (Math.round(value) !== value || !Number.isFinite(value)) {
throw new Error(
`CheminParam.interger expect an interger when serializing`
`CheminParam.interger expect an interger when serializing`,
);
}
return value.toString();
Expand Down Expand Up @@ -154,7 +154,7 @@ export type OptionalValue<T> = { present: false } | { present: true; value: T };
* @returns A chemin param
*/
export function pOptional<N extends string, T>(
sub: TCheminParam<N, T, any>
sub: TCheminParam<N, T, any>,
): TCheminParam<N, OptionalValue<T>, { sub: TCheminParam<N, T, any> }> {
return {
name: sub.name,
Expand Down Expand Up @@ -196,7 +196,7 @@ export function pOptional<N extends string, T>(
*/
export function pOptionalConst<N extends string>(
name: N,
constant: string = name
constant: string = name,
): TCheminParam<N, boolean, { constant: string }> {
return {
name,
Expand Down Expand Up @@ -228,7 +228,7 @@ export function pOptionalConst<N extends string>(
* @returns
*/
export function pOptionalString<N extends string>(
name: N
name: N,
): TCheminParam<N, string | false> {
return {
name,
Expand Down Expand Up @@ -267,7 +267,7 @@ export function pOptionalString<N extends string>(
*/
export function pMultiple<N extends string, T, Meta>(
sub: TCheminParam<N, T, Meta>,
atLeastOne: boolean = false
atLeastOne: boolean = false,
): TCheminParam<
N,
Array<T>,
Expand Down Expand Up @@ -311,7 +311,7 @@ export function pMultiple<N extends string, T, Meta>(
*/
export function cheminParamsEqual(
left: ICheminParamBase<any, any, any>,
right: ICheminParamBase<any, any, any>
right: ICheminParamBase<any, any, any>,
): boolean {
if (left.factory !== right.factory) {
return false;
Expand Down
42 changes: 19 additions & 23 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ export type TSimplify<T> = { [KeyType in keyof T]: T[KeyType] } & {};
*/
export type TUnionParams<R extends readonly TIn[]> = R extends readonly [
infer H,
...infer S
]
? TParams<H> & (S extends readonly TIn[] ? TUnionParams<S> : TEmptyObject)
...infer S,
] ? TParams<H> & (S extends readonly TIn[] ? TUnionParams<S> : TEmptyObject)
: TEmptyObject;

/**
Expand All @@ -59,14 +58,10 @@ export type TEmptyObject = Record<never, never>;
/**
* Extract the params type of a chemin
*/
export type TParams<T> = T extends string
? TEmptyObject
: T extends IChemin<infer P>
? P
: T extends TCheminParam<any, void, any>
? TEmptyObject
: T extends TCheminParam<infer N, infer P, any>
? { [K in N]: P }
export type TParams<T> = T extends string ? TEmptyObject
: T extends IChemin<infer P> ? P
: T extends TCheminParam<any, void, any> ? TEmptyObject
: T extends TCheminParam<infer N, infer P, any> ? { [K in N]: P }
: TEmptyObject;

/**
Expand All @@ -88,10 +83,10 @@ export type TIn = string | TCheminParamAny | IChemin<any>;
export type TPartMatchResult<T> =
| { readonly match: false }
| {
readonly match: true;
readonly value: T extends void ? null : T;
readonly next: ReadonlyArray<string>;
};
readonly match: true;
readonly value: T extends void ? null : T;
readonly next: ReadonlyArray<string>;
};

/**
* Part match function
Expand All @@ -107,7 +102,7 @@ export type TPartSerialize<T> = (value: T) => string | null;
* Part isEqual function
*/
export type TPartIsEqual<N extends string, T, Meta> = (
other: TCheminParam<N, T, Meta>
other: TCheminParam<N, T, Meta>,
) => boolean;

/**
Expand All @@ -131,12 +126,13 @@ export interface ICheminParamBase<N extends string, T, Meta> {
/**
* Chemin param type
*/
export type TCheminParam<N extends string, T, Meta = null> = ICheminParamBase<
N,
T,
Meta
> &
(T extends void ? { noValue: true } : TEmptyObject);
export type TCheminParam<N extends string, T, Meta = null> =
& ICheminParamBase<
N,
T,
Meta
>
& (T extends void ? { noValue: true } : TEmptyObject);

/**
* Chemin param type with any value
Expand All @@ -158,7 +154,7 @@ export interface IChemin<Params = any> {
readonly parts: ReadonlyArray<TPart>;
readonly serialize: TSerialize<Params>;
readonly match: (
pathname: string | Array<string>
pathname: string | Array<string>,
) => TCheminMatchMaybe<Params>;
readonly matchExact: (pathname: string | Array<string>) => Params | null;
readonly stringify: (options?: ISlashOptions) => string;
Expand Down

0 comments on commit ff45a25

Please sign in to comment.