-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modify types for special expressions, correct types for eqContains an…
…d update tests
- Loading branch information
1 parent
de81228
commit 17f7b3a
Showing
2 changed files
with
33 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,81 @@ | ||
export type EqualityExpression<T, Op extends string> = { | ||
export type EqualityExpression<Op extends string> = { | ||
// type-script | ||
// .operation causes issues if op of e.g. 'contains' is assigned to 'in' because 'contains' contains 'in' | ||
__opName: Op; | ||
operation: `${string}${Op}${string}`; | ||
|
||
values: any[]; | ||
}; | ||
|
||
// clean this up at some point | ||
export type AllExpressions = | ||
export type NumberExpressions = | ||
| ReturnType<typeof eqLessThan> | ||
| ReturnType<typeof eqLessThanOrEqual> | ||
| ReturnType<typeof eqGreaterThan> | ||
| ReturnType<typeof eqGreaterThanOrEqual> | ||
| ReturnType<typeof eqIn>; | ||
|
||
export type ExpressionByValue<Value> = Value extends number | bigint | ||
? AllExpressions | ||
// TODO: support for more values for collection-like expressions | ||
export type ExpressionByValue<Value> = Value extends ( | ||
| string | ||
| number | ||
| bigint | ||
)[] | ||
? ReturnType<typeof eqContains> | ||
: Value extends number | bigint | ||
? NumberExpressions | ||
: ReturnType<typeof eqIn>; | ||
|
||
export const isEqualityExpression = ( | ||
object: any | ||
): object is EqualityExpression<any, any> => | ||
): object is EqualityExpression<any> => | ||
typeof object === 'object' && object !== null && 'operation' in object; | ||
|
||
// TODO: investigate how these things are compared and maybe add more types here | ||
export const eqIn = <T extends (string | number | bigint)[]>( | ||
...values: T | ||
): EqualityExpression<T, 'in'> => ({ | ||
): EqualityExpression<'in'> => ({ | ||
__opName: 'in', | ||
operation: ` in (${values.map((_) => '?').join(',')})`, | ||
values, | ||
}); | ||
|
||
export const eqContains = <T extends string | number | bigint>( | ||
value: T | ||
): EqualityExpression<T, 'contains'> => ({ | ||
operation: ` contains ${value}`, | ||
): EqualityExpression<'contains'> => ({ | ||
__opName: 'contains', | ||
operation: ' contains ?', | ||
values: [value], | ||
}); | ||
|
||
export const eqLessThan = <T extends number | bigint>( | ||
value: T | ||
): EqualityExpression<T, '<'> => ({ | ||
): EqualityExpression<'<'> => ({ | ||
__opName: '<', | ||
operation: '<?', | ||
values: [value], | ||
}); | ||
|
||
export const eqLessThanOrEqual = <T extends number | bigint>( | ||
value: T | ||
): EqualityExpression<T, '<='> => ({ | ||
): EqualityExpression<'<='> => ({ | ||
__opName: '<=', | ||
operation: '<=?', | ||
values: [value], | ||
}); | ||
|
||
export const eqGreaterThan = <T extends number | bigint>( | ||
value: T | ||
): EqualityExpression<T, '>'> => ({ | ||
): EqualityExpression<'>'> => ({ | ||
__opName: '>', | ||
operation: '>?', | ||
values: [value], | ||
}); | ||
|
||
export const eqGreaterThanOrEqual = <T extends number | bigint>( | ||
value: T | ||
): EqualityExpression<T, '>='> => ({ | ||
): EqualityExpression<'>='> => ({ | ||
__opName: '>=', | ||
operation: '>=?', | ||
values: [value], | ||
}); |