Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/mod operator #36

Merged
merged 2 commits into from
Jul 4, 2023
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
11 changes: 11 additions & 0 deletions src/types/filter.assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ ta.assert<
// Test $in operator
ta.assert<ta.Extends<{ a: { $in: number[] } }, TsFilter<{ a: number }>>>()

// test $mod operator
ta.assert<
ta.Extends<{ a: { $mod: [number, number] } }, TsFilter<{ a: number }>>
>()
ta.assert<
ta.Not<ta.Extends<{ a: { $mod: [3, 5] } }, TsFilter<{ a: string }>>>
>()

// Test FlattenPaths / FlattenType
type Example = {
a: number
Expand Down Expand Up @@ -179,6 +187,9 @@ ta.assert<ta.Extends<{ $in: number[] }, FilterType<Example, 'a'>>>()
ta.assert<
ta.Extends<{ $and: [{ $lt: 2 }, { $gt: 0 }] }, FilterType<Example, 'a'>>
>()
ta.assert<ta.Extends<{ $mod: [2, 0] }, FilterType<Example, 'a'>>>()
ta.assert<ta.Not<ta.Extends<{ $mod: [2, 0] }, FilterType<Example, 'b.c'>>>>()
ta.assert<ta.Not<ta.Extends<{ $mod: [2, 0] }, FilterType<Example, 'b.d'>>>>()

// These use to be wrong, keeping as regression testing
ta.assert<ta.Not<ta.Extends<{ a: 2 }, WithOperator<{ a: number }[]>>>>()
Expand Down
9 changes: 8 additions & 1 deletion src/types/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ export type WithRecordOperator<
}
: {}

export type WithModulusOperator<Field> = Field extends number
? {
$mod?: [number, number]
}
: {}

export type WithOperator<Field, IndexType extends number = 0> =
| RecurPartial<Field>
| WithNegatableOperator<
Expand All @@ -102,7 +108,8 @@ export type WithOperator<Field, IndexType extends number = 0> =
WithEqualityOperator<Field> &
WithArrayOperator<Field> &
WithGeoSpatialQueryOperator<Field> &
WithBitwiseOperator<Field>
WithBitwiseOperator<Field> &
WithModulusOperator<Field>
>

/**
Expand Down
Loading