generated from sapphiredev/sapphire-template
-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Vlad Frangu <[email protected]>
- Loading branch information
1 parent
80f1522
commit 16af17b
Showing
14 changed files
with
379 additions
and
22 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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { ConstraintError, ConstraintErrorMessageBuilder } from '../lib/errors/ConstraintError'; | ||
import { Result } from '../lib/Result'; | ||
import type { IConstraint } from './base/IConstraint'; | ||
import { Comparator, eq, gt, ge, lt, le, ne } from './util/operators'; | ||
|
||
function bigintComparator( | ||
comparator: Comparator, | ||
name: string, | ||
messageBuilder: ConstraintErrorMessageBuilder<bigint>, | ||
number: bigint | ||
): IConstraint<bigint> { | ||
return { | ||
run(input: bigint) { | ||
return comparator(input, number) // | ||
? Result.ok(input) | ||
: Result.err(new ConstraintError(name, messageBuilder(input, number), input, number)); | ||
} | ||
}; | ||
} | ||
|
||
export const bigintLt = bigintComparator.bind( | ||
null, | ||
lt, | ||
'bigintLt', | ||
(given, expected) => `Expected bigint to be less than ${expected}, but received ${given}` | ||
); | ||
|
||
export const bigintLe = bigintComparator.bind( | ||
null, | ||
le, | ||
'bigintLe', | ||
(given, expected) => `Expected bigint to be less or equals than ${expected}, but received ${given}` | ||
); | ||
|
||
export const bigintGt = bigintComparator.bind( | ||
null, | ||
gt, | ||
'bigintGt', | ||
(given, expected) => `Expected bigint to be greater than ${expected}, but received ${given}` | ||
); | ||
|
||
export const bigintGe = bigintComparator.bind( | ||
null, | ||
ge, | ||
'bigintGe', | ||
(given, expected) => `Expected bigint to be greater or equals than ${expected}, but received ${given}` | ||
); | ||
|
||
export const bigintEq = bigintComparator.bind( | ||
null, | ||
eq, | ||
'bigintEq', | ||
(given, expected) => `Expected bigint to be exactly ${expected}, but received ${given}` | ||
); | ||
|
||
export const bigintNe = bigintComparator.bind(null, ne, 'bigintNe', (_, expected) => `Expected bigint to not be ${expected}`); |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { ConstraintError } from '../lib/errors/ConstraintError'; | ||
import { Result } from '../lib/Result'; | ||
import type { IConstraint } from './base/IConstraint'; | ||
|
||
export const booleanTrue: IConstraint<boolean, true> = { | ||
run(input: boolean) { | ||
return input // | ||
? Result.ok(input) | ||
: Result.err(new ConstraintError('booleanTrue', 'Expected boolean to be true, but received false', input, true)); | ||
} | ||
}; | ||
|
||
export const booleanFalse: IConstraint<boolean, false> = { | ||
run(input: boolean) { | ||
return input // | ||
? Result.err(new ConstraintError('booleanFalse', 'Expected boolean to be false, but received true', input, false)) | ||
: Result.ok(input); | ||
} | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { ConstraintError, ConstraintErrorMessageBuilder } from '../lib/errors/ConstraintError'; | ||
import { Result } from '../lib/Result'; | ||
import type { IConstraint } from './base/IConstraint'; | ||
import { Comparator, eq, ge, gt, le, lt, ne } from './util/operators'; | ||
|
||
function dateComparator( | ||
comparator: Comparator, | ||
name: string, | ||
messageBuilder: ConstraintErrorMessageBuilder<Date>, | ||
date: Date, | ||
number = date.getTime() | ||
): IConstraint<Date> { | ||
return { | ||
run(input: Date) { | ||
return comparator(input.getTime(), number) // | ||
? Result.ok(input) | ||
: Result.err(new ConstraintError(name, messageBuilder(input, date), input, date)); | ||
} | ||
}; | ||
} | ||
|
||
export const dateLt = dateComparator.bind( | ||
null, | ||
lt, | ||
'dateLt', | ||
(given, expected) => `Expected date to be earlier than ${expected}, but received ${given}` | ||
); | ||
|
||
export const dateLe = dateComparator.bind( | ||
null, | ||
le, | ||
'dateLe', | ||
(given, expected) => `Expected date to be earlier or equals than ${expected}, but received ${given}` | ||
); | ||
|
||
export const dateGt = dateComparator.bind( | ||
null, | ||
gt, | ||
'dateGt', | ||
(given, expected) => `Expected date to be later than ${expected}, but received ${given}` | ||
); | ||
|
||
export const dateGe = dateComparator.bind( | ||
null, | ||
ge, | ||
'dateGe', | ||
(given, expected) => `Expected date to be later or equals than ${expected}, but received ${given}` | ||
); | ||
|
||
export const dateEq = dateComparator.bind(null, eq, 'dateEq', (given, expected) => `Expected date to be exactly ${expected}, but received ${given}`); | ||
export const dateNe = dateComparator.bind(null, ne, 'dateNe', (_, expected) => `Expected date to not be ${expected}`); | ||
|
||
export const dateInvalid: IConstraint<Date> = { | ||
run(input: Date) { | ||
return Number.isNaN(input.getTime()) // | ||
? Result.ok(input) | ||
: Result.err(new ConstraintError('dateInvalid', `Expected Date's time to be a NaN, but received ${input}`, input, 'An invalid Date')); | ||
} | ||
}; | ||
|
||
export const dateValid: IConstraint<Date> = { | ||
run(input: Date) { | ||
return Number.isNaN(input.getTime()) // | ||
? Result.err(new ConstraintError('dateValid', `Expected Date's time to not be a NaN, but received ${input}`, input, 'A valid Date')) | ||
: Result.ok(input); | ||
} | ||
}; |
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
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,27 +1,40 @@ | ||
export function lt(a: number, b: number): boolean { | ||
export function lt(a: number, b: number): boolean; | ||
export function lt(a: bigint, b: bigint): boolean; | ||
export function lt(a: number | bigint, b: number | bigint): boolean { | ||
return a < b; | ||
} | ||
|
||
export function le(a: number, b: number): boolean { | ||
export function le(a: number, b: number): boolean; | ||
export function le(a: bigint, b: bigint): boolean; | ||
export function le(a: number | bigint, b: number | bigint): boolean { | ||
return a <= b; | ||
} | ||
|
||
export function gt(a: number, b: number): boolean { | ||
export function gt(a: number, b: number): boolean; | ||
export function gt(a: bigint, b: bigint): boolean; | ||
export function gt(a: number | bigint, b: number | bigint): boolean { | ||
return a > b; | ||
} | ||
|
||
export function ge(a: number, b: number): boolean { | ||
export function ge(a: number, b: number): boolean; | ||
export function ge(a: bigint, b: bigint): boolean; | ||
export function ge(a: number | bigint, b: number | bigint): boolean { | ||
return a > b; | ||
} | ||
|
||
export function eq(a: number, b: number): boolean { | ||
export function eq(a: number, b: number): boolean; | ||
export function eq(a: bigint, b: bigint): boolean; | ||
export function eq(a: number | bigint, b: number | bigint): boolean { | ||
return a === b; | ||
} | ||
|
||
export function ne(a: number, b: number): boolean { | ||
export function ne(a: number, b: number): boolean; | ||
export function ne(a: bigint, b: bigint): boolean; | ||
export function ne(a: number | bigint, b: number | bigint): boolean { | ||
return a !== b; | ||
} | ||
|
||
export interface Comparator { | ||
(a: number, b: number): boolean; | ||
(a: bigint, b: bigint): boolean; | ||
} |
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
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
Oops, something went wrong.