Skip to content

Instantly share code, notes, and snippets.

@wmakeev
Last active May 21, 2021 05:29
Show Gist options
  • Save wmakeev/4de1af82f395699375cb2a2f546b3229 to your computer and use it in GitHub Desktop.
Save wmakeev/4de1af82f395699375cb2a2f546b3229 to your computer and use it in GitHub Desktop.
[Tools] #tools #maybe #wait #if #case
export function CASE<T>(...args: [boolean, T][]) {
  return args.find(a => a[0])?.[1]
}

Example:

CASE(
  [
    status.type === StatusType.ERROR,
    h(AppError, { message: status.message })
  ],
  [status.type === StatusType.INIT, h(AppInit)],
  [
    true,
    div('.branding-below', [
      div('.prop.header', [
        div('.name', span('Наименование')),
        div('.value', span('Значение'))
      ]),
      div(
        properties.filter(p => p.value !== null).map(p => h(Property, p))
      ),
      div(h(Draft, draft))
    ])
  ]
)
export function IF<T, F>(condition: unknown, trueValue: T, falseValue?: F) {
return condition ? trueValue : falseValue
}
export function MAYBE<T>(value: T): [NonNullable<T>] {
if (value != null) {
return [value as NonNullable<T>]
} else {
return [] as any
}
}
export function wait(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment