-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: greatly rework how assertions and brands work
- Loading branch information
Showing
76 changed files
with
604 additions
and
302 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,9 +1,4 @@ | ||
import type { | ||
AndSchema, | ||
InferAndOutput, | ||
InferAndSchema, | ||
} from "@assertions/and/types"; | ||
import type { Assertion } from "@the-minimal/types"; | ||
import type { AndSchema, Validate } from "@assertions/and/types"; | ||
|
||
/** | ||
* Checks if all the assertions pass. | ||
|
@@ -23,12 +18,9 @@ import type { Assertion } from "@the-minimal/types"; | |
* userEmail("[email protected]"); // passes | ||
* ``` | ||
*/ | ||
export const and = | ||
<const $Schema extends AndSchema>( | ||
assertions: InferAndSchema<$Schema>, | ||
): Assertion<InferAndOutput<$Schema>> => | ||
(v) => { | ||
export const and = <const $Schema extends AndSchema>(assertions: $Schema) => | ||
((v: unknown) => { | ||
for (let i = 0; i < assertions.length; ++i) { | ||
((assertions as any)[i] as any)(v); | ||
} | ||
}; | ||
}) as unknown as Validate.And<$Schema>; |
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,16 +1,47 @@ | ||
import type { Assertion } from "@the-minimal/types"; | ||
import { and } from "@assertions/and/index"; | ||
import type { AnyBrand, Brand, None } from "@the-minimal/types"; | ||
|
||
export type AndSchema = Array<unknown>; | ||
export namespace Validate { | ||
export type And<$Types extends AndSchema> = Brand< | ||
"and", | ||
$Schema, | ||
{ | ||
input: InferAndInputs<FilterAndInputs<$Types>>; | ||
output: InferAndOutputs<$Types>; | ||
} | ||
>; | ||
} | ||
|
||
export type InferAndSchema<$Schema extends AndSchema> = { | ||
[$Key in keyof $Schema]: Assertion<$Schema[$Key]>; | ||
}; | ||
export type AndSchema = AnyBrand[]; | ||
|
||
export type InferAndOutput<$Schema extends AndSchema> = $Schema extends [ | ||
export type InferAndInputs<$Types extends unknown[]> = $Types extends [ | ||
infer $Head, | ||
...infer $Tail, | ||
] | ||
? $Tail extends [infer $1, ...infer $2] | ||
? $Head & InferAndOutput<$Tail> | ||
? $Tail extends [infer _1, ...infer _2] | ||
? $Head & InferAndOutputs<$Tail> | ||
: $Head | ||
: never; | ||
|
||
type FilterAndInputs<$Schema extends AndSchema> = $Schema extends readonly [ | ||
infer $Head, | ||
...infer $Tail, | ||
] | ||
? $Tail extends readonly [infer _1, ...infer _2] | ||
? $Head extends Brand<any, any, { input: infer $Input; output: any }> | ||
? [$Input, ...FilterAndInputs<$Tail>] | ||
: [] | ||
: $Head extends Brand<any, any, { input: infer $Input; output: any }> | ||
? [$Input] | ||
: [] | ||
: []; | ||
|
||
export type InferAndOutputs<$Schema extends AndSchema> = | ||
$Schema extends readonly [infer $Head, ...infer $Tail] | ||
? $Tail extends readonly [infer _1, ...infer _2] | ||
? ($Head extends Brand<any, any, { input: infer $Input; output: any }> | ||
? $Input | ||
: $Head) & | ||
InferAndOutputs<$Tail> | ||
: $Head | ||
: never; |
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,4 +1,5 @@ | ||
import type { Assertion } from "@the-minimal/types"; | ||
import type { Validate } from "@assertions/and/types"; | ||
import type { AnyBrand } from "@the-minimal/types"; | ||
|
||
/** | ||
* Checks that both assertions pass. | ||
|
@@ -15,12 +16,11 @@ import type { Assertion } from "@the-minimal/types"; | |
* userEmail("[email protected]"); // passes | ||
* ``` | ||
*/ | ||
export const and2 = | ||
<$Input1, $Input2>( | ||
assertion1: Assertion<$Input1>, | ||
assertion2: Assertion<$Input2>, | ||
): Assertion<$Input1 & $Input2> => | ||
(v) => { | ||
assertion1(v); | ||
assertion2(v); | ||
}; | ||
export const and2 = <$Brand1 extends AnyBrand, $Brand2 extends AnyBrand>( | ||
brand1: $Brand1, | ||
brand2: $Brand2, | ||
) => | ||
((v: unknown) => { | ||
(brand1 as any)(v); | ||
(brand2 as any)(v); | ||
}) as unknown as Validate.And<[$Brand1, $Brand2]>; |
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,4 +1,5 @@ | ||
import type { Assertion } from "@the-minimal/types"; | ||
import type { Validate } from "@assertions/and/types"; | ||
import type { AnyBrand } from "@the-minimal/types"; | ||
|
||
/** | ||
* Checks that all three assertions pass. | ||
|
@@ -17,14 +18,17 @@ import type { Assertion } from "@the-minimal/types"; | |
* userEmail("[email protected]"); // passes | ||
* ``` | ||
*/ | ||
export const and3 = | ||
<$Input1, $Input2, $Input3>( | ||
assertion1: Assertion<$Input1>, | ||
assertion2: Assertion<$Input2>, | ||
assertion3: Assertion<$Input3>, | ||
): Assertion<$Input1 & $Input2 & $Input3> => | ||
(v) => { | ||
assertion1(v); | ||
assertion2(v); | ||
assertion3(v); | ||
}; | ||
export const and3 = < | ||
$Brand1 extends AnyBrand, | ||
$Brand2 extends AnyBrand, | ||
$Brand3 extends AnyBrand, | ||
>( | ||
brand1: $Brand1, | ||
brand2: $Brand2, | ||
brand3: $Brand3, | ||
) => | ||
((v: unknown) => { | ||
(brand1 as any)(v); | ||
(brand2 as any)(v); | ||
(brand3 as any)(v); | ||
}) as unknown as Validate.And<[$Brand1, $Brand2, $Brand3]>; |
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,13 @@ | ||
import type { AnyBrand, Brand, None } from "@the-minimal/types"; | ||
import type { InferInput } from "@types"; | ||
|
||
export namespace Validate { | ||
export type Array<$Brand extends AnyBrand> = Brand< | ||
"array", | ||
$Brand, | ||
{ | ||
input: InferInput<$Brand>[]; | ||
output: None; | ||
} | ||
>; | ||
} |
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,17 +1,18 @@ | ||
import { boolean } from "@assertions/boolean"; | ||
import { fc, test } from "@fast-check/vitest"; | ||
import { assert } from "@utils"; | ||
import { expect } from "vitest"; | ||
|
||
test.prop([fc.boolean()])( | ||
"should not throw if value is of type boolean", | ||
(value) => { | ||
expect(() => boolean(value)).not.toThrow(); | ||
expect(() => assert(boolean, value)).not.toThrow(); | ||
}, | ||
); | ||
|
||
test.prop([fc.string()])( | ||
"should throw if value is not of type boolean", | ||
(value) => { | ||
expect(() => boolean(value)).toThrow(); | ||
expect(() => assert(boolean, value)).toThrow(); | ||
}, | ||
); |
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,14 @@ | ||
import type { Brand } from "@the-minimal/types"; | ||
|
||
export namespace Validate { | ||
export namespace Type { | ||
export type Boolean = Brand< | ||
"type", | ||
"boolean", | ||
{ | ||
input: boolean; | ||
output: 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
import type { Email } from "@assertions/email/types"; | ||
import { regex } from "@assertions/regex"; | ||
import type { Assertion } from "@the-minimal/types"; | ||
import type { Validate } from "./types"; | ||
|
||
/** | ||
* Checks if value matches email RegExp. | ||
|
@@ -11,4 +10,4 @@ import type { Assertion } from "@the-minimal/types"; | |
* email("[email protected]"); // passes | ||
* ``` | ||
*/ | ||
export const email: Assertion<Email> = regex(/^\w+@.+\..+$/); | ||
export const email: Validate.Regex.Email = regex(/^\w+@.+\..+$/); |
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,3 +1,7 @@ | ||
import type { Regex } from "@assertions/regex/types"; | ||
import type { Brand } from "@the-minimal/types"; | ||
|
||
export type Email = Regex<"email">; | ||
export namespace Validate { | ||
export namespace Regex { | ||
export type Email = Brand<"regex", "email">; | ||
} | ||
} |
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,3 +1,10 @@ | ||
import type { Brand } from "@the-minimal/types"; | ||
|
||
export type EndsWith<$Input> = Brand<"EndsWith", $Input>; | ||
export namespace Validate { | ||
export namespace String { | ||
export type EndsWith<$Type extends string> = Brand< | ||
"string-endswith", | ||
$Type | ||
>; | ||
} | ||
} |
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,3 +1,7 @@ | ||
import type { Brand } from "@the-minimal/types"; | ||
|
||
export type Includes<$Input> = Brand<"Includes", $Input>; | ||
export namespace Validate { | ||
export namespace List { | ||
export type Includes<$Input> = Brand<"list-includes", $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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
import type { Brand } from "@the-minimal/types"; | ||
|
||
export type Integer = Brand<"Integer">; | ||
export namespace Validate { | ||
export namespace Number { | ||
export type Integer = Brand<"number-integer">; | ||
} | ||
} |
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.