1+ /*--------------------------------------------------------------------------
2+
3+ TypeBox
4+
5+ The MIT License (MIT)
6+
7+ Copyright (c) 2017-2026 Haydn Paterson
8+
9+ Permission is hereby granted, free of charge, to any person obtaining a copy
10+ of this software and associated documentation files (the "Software"), to deal
11+ in the Software without restriction, including without limitation the rights
12+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+ copies of the Software, and to permit persons to whom the Software is
14+ furnished to do so, subject to the following conditions:
15+
16+ The above copyright notice and this permission notice shall be included in
17+ all copies or substantial portions of the Software.
18+
19+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+ THE SOFTWARE.
26+
27+ ---------------------------------------------------------------------------*/
28+
29+ // deno-fmt-ignore-file
30+
31+ import { Memory } from '../../system/memory/index.ts'
32+ import { type StaticType , type StaticDirection } from './static.ts'
33+ import { type TSchema , type TSchemaOptions , IsKind } from './schema.ts'
34+ import { type TProperties } from './properties.ts'
35+
36+ // ------------------------------------------------------------------
37+ // Static
38+ // ------------------------------------------------------------------
39+ export type StaticIf < Stack extends string [ ] , Direction extends StaticDirection , Context extends TProperties , This extends TProperties , If extends TSchema , Then extends TSchema , Else extends TSchema ,
40+ StaticIf extends unknown = StaticType < Stack , Direction , Context , This , If > ,
41+ StaticThen extends unknown = StaticType < Stack , Direction , Context , This , Then > ,
42+ StaticElse extends unknown = StaticType < Stack , Direction , Context , This , Else > ,
43+ Result extends unknown = ( StaticIf & StaticThen ) | Exclude < StaticElse , StaticIf >
44+ > = Result
45+ // ------------------------------------------------------------------
46+ // Type
47+ // ------------------------------------------------------------------
48+ /** Represents a Conditionally Dependent If Type */
49+ export interface TIf < If extends TSchema = TSchema , Then extends TSchema = TSchema , Else extends TSchema = TSchema > extends TSchema {
50+ '~kind' : 'If'
51+ if : If
52+ then : Then
53+ else : Else
54+ }
55+ // ------------------------------------------------------------------
56+ // Factory
57+ // ------------------------------------------------------------------
58+ /** Creates a Conditionally Dependent If Type */
59+ export function If < If extends TSchema , Then extends TSchema , Else extends TSchema > ( if_ : If , then_ : Then , else_ : Else , options : TSchemaOptions = { } ) : TIf < If , Then , Else > {
60+ return Memory . Create ( { '~kind' : 'If' } , { if : if_ , then : then_ , else : else_ } , options ) as never
61+ }
62+ // ------------------------------------------------------------------
63+ // Guard
64+ // ------------------------------------------------------------------
65+ /** Returns true if the given value is TIf. */
66+ export function IsIf ( value : unknown ) : value is TIf {
67+ return IsKind ( value , 'If' )
68+ }
69+
70+ // ------------------------------------------------------------------
71+ // Options
72+ // ------------------------------------------------------------------
73+ /** Extracts options from a TIf. */
74+ export function IfOptions ( type : TIf ) : TSchemaOptions {
75+ return Memory . Discard ( type , [ '~kind' , 'if' , 'then' , 'else' ] )
76+ }
0 commit comments