Skip to content

Commit

Permalink
Feat/mod operator (#36)
Browse files Browse the repository at this point in the history
* Feat: Add $mod operator

* Add more test

---------

Co-authored-by: Nicolás San Martín <nicolassanmar@gmail.com>
  • Loading branch information
tianhuil and nicolassanmar committed Jul 4, 2023
1 parent 6fde512 commit ec746ee
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
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

0 comments on commit ec746ee

Please sign in to comment.