import { isDefined, isEmpty, isNotEmpty, isPresent, isTrue } from '../types/Is'; import { Optional } from '../types/Types'; import { Construct, ofConstruct, use } from '../types/Constructor'; import { Get, ofGet } from '../types/Get'; import { OneOrMore } from '../types/Array'; import { toList } from '../types/List'; import { isEqual } from '../types/IsEqual'; export function ifTrue(o: unknown, f: Construct>, alt: Construct): Out; export function ifTrue(o: unknown, f: Construct>, alt?: Construct): Optional; export function ifTrue(o: unknown, f: Construct>, alt?: Construct): Optional { return isTrue(o) ? ofConstruct(f, o) : ofConstruct(alt, o); } export function ifFalse(o: unknown, f: Construct>, alt: Construct): Out; export function ifFalse(o: unknown, f: Construct>, alt?: Construct): Optional; export function ifFalse(o: unknown, f: Construct>, alt?: Construct): Optional { return !isTrue(o) ? ofConstruct(f, o) : ofConstruct(alt, o); } export function ifDefined(o: Optional, f: Construct>, alt: Construct): Out; export function ifDefined(o: Optional, f: Construct>, alt?: Construct): Optional; export function ifDefined(o: Optional, f: Construct>, alt?: Construct): Optional { return isDefined(o) ? ofConstruct(f, o) : ofConstruct(alt); } export function ifNotEmpty(o: In, f: Construct>, alt: Construct): Out; export function ifNotEmpty(o: In, f?: Construct>, alt?: Construct): Optional; export function ifNotEmpty(o: In, f: Construct>, alt?: Construct): Optional; export function ifNotEmpty(o: In, f: Construct> = o => o as Out, alt?: Construct): Optional { return isNotEmpty(o) ? ofConstruct(f, o) : ofConstruct(alt, o); } export function ifEmpty(o: In, f: Construct>, alt: Construct): Out; export function ifEmpty(o: In, f?: Construct>, alt?: Construct): Optional; export function ifEmpty(o: In, f: Construct>, alt?: Construct): Optional; export function ifEmpty(o: In, f: Construct> = o => o as Out, alt?: Construct): Optional { return isEmpty(o) ? ofConstruct(f, o) : ofConstruct(alt, o); } export function given(o: T, key: K, alt: Get): T[K]; export function given(o: T, key: K, alt?: Get): Optional; export function given(o: T, key: K, alt?: Get): Optional { return isDefined(o) && key in o ? o[key] : ofGet(alt); } export function ifEqual(one: unknown, another: unknown, f: Construct, alt: Construct): Out; export function ifEqual(one: unknown, another: unknown, f: Construct, alt?: Construct): Optional; export function ifEqual(one: unknown, another: unknown, f: Construct, alt?: Construct): Optional { return isEqual(one, another) ? ofConstruct(f) : ofConstruct(alt); } export function ifEither(os: OneOrMore, f: Construct>, alt: Construct): Out; export function ifEither(os: OneOrMore, f?: Construct>, alt?: Construct): Optional; export function ifEither(os: OneOrMore, f: Construct>, alt?: Construct): Optional; export function ifEither(os: OneOrMore, f: Construct> = o => o as Out, alt?: Construct): Optional { return use( toList(os).first(o => isPresent(o)), o => (isDefined(o) ? ofConstruct(f, o) : ofConstruct(alt, o)) ); }