Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/lexer/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,9 @@ type FilterTokenizer<
>
: Head extends NumericValue ?
FilterTokenizer<ReadNum<T>[1], Schema, [...Acc, NumToken<ReadNum<T>[0]>]>
: Head extends `${number}` | `${bigint}` ?
// Handle an interpolated number, e.g. a runtime value in `field:=${n}`
FilterTokenizer<Tail, Schema, [...Acc, NumToken<Head>]>
: `Unknown token: ${Head}`
: T extends keyof TokenMap ?
// Handle case when the last token is a symbol (e.g. "&&")
Expand Down
90 changes: 90 additions & 0 deletions tests/filter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,96 @@ describe("FilterTokenizer tests", () => {
FilterTokenizer<"age != 20", typeof _usersSchema>
>().toEqualTypeOf<"Unknown token: !">();
});

it("should tokenize a `${number}` interpolation as a num token", () => {
expectTypeOf<
FilterTokenizer<`age:=${number}`, typeof _usersSchema>
>().toEqualTypeOf<[Ident<"age", "int32">, ":=", NumToken<`${number}`>]>();
});

it("should tokenize a `${number}` interpolation followed by a clause", () => {
expectTypeOf<
FilterTokenizer<`age:=${number} && name:John`, typeof _usersSchema>
>().toEqualTypeOf<
[
Ident<"age", "int32">,
":=",
NumToken<`${number}`>,
"&&",
Ident<"name", "string">,
":",
LiteralToken<"John">,
]
>();
});

it("should tokenize `${number}` interpolations inside a range", () => {
expectTypeOf<
FilterTokenizer<`age:[${number}..${number}]`, typeof _usersSchema>
>().toEqualTypeOf<
[
Ident<"age", "int32">,
":[",
NumToken<`${number}`>,
"..",
NumToken<`${number}`>,
"]",
]
>();
});

it("should tokenize a `${bigint}` interpolation as a num token", () => {
expectTypeOf<
FilterTokenizer<`age:=${bigint}`, typeof _usersSchema>
>().toEqualTypeOf<[Ident<"age", "int32">, ":=", NumToken<`${bigint}`>]>();
});

it("should still tokenize literal numbers with their exact value", () => {
expectTypeOf<
FilterTokenizer<"age:=20", typeof _usersSchema>
>().toEqualTypeOf<[Ident<"age", "int32">, ":=", NumToken<"20">]>();
});
});

describe("ParseFilter with runtime `${number}` values", () => {
it("should accept a runtime numeric value with `:=`", () => {
expectTypeOf<
ParseFilter<`age:=${number}`, typeof _usersSchema>
>().toEqualTypeOf<true>();
});

it("should accept a runtime numeric value with comparison operators", () => {
expectTypeOf<
ParseFilter<`age:>${number}`, typeof _usersSchema>
>().toEqualTypeOf<true>();
expectTypeOf<
ParseFilter<`age:<${number}`, typeof _usersSchema>
>().toEqualTypeOf<true>();
});

it("should accept a runtime numeric value inside an array", () => {
expectTypeOf<
ParseFilter<`age:[${number}]`, typeof _usersSchema>
>().toEqualTypeOf<true>();
});

it("should accept a runtime numeric range", () => {
expectTypeOf<
ParseFilter<`age:[${number}..${number}]`, typeof _usersSchema>
>().toEqualTypeOf<true>();
});

it("should accept a runtime numeric value followed by another clause", () => {
expectTypeOf<
ParseFilter<`age:=${number} && name:John`, typeof _usersSchema>
>().toEqualTypeOf<true>();
});

it("should accept a runtime bigint value", () => {
expectTypeOf<
ParseFilter<`age:=${bigint}`, typeof _usersSchema>
>().toEqualTypeOf<true>();
});
});

describe("IsNextTokenValid type tests", () => {
Expand Down
Loading