Skip to content

Instantly share code, notes, and snippets.

@bigmistqke
Last active July 30, 2023 21:44
Show Gist options
  • Save bigmistqke/7e0bb87569ee1f10d327ae808d51d3b8 to your computer and use it in GitHub Desktop.
Save bigmistqke/7e0bb87569ee1f10d327ae808d51d3b8 to your computer and use it in GitHub Desktop.
when.ts
import { Accessor } from 'solid-js'
export function when<
TAccessors extends Array<Accessor<any>>,
const TValues extends { [TKey in keyof TAccessors]: Exclude<ReturnType<TAccessors[TKey]>, null | undefined | false> }
>(...accessors: TAccessors) {
function callback<const TResult>(): TValues | undefined
function callback<const TResult>(callback: (...values: TValues) => TResult): TResult | undefined
function callback<const TResult>(callback?: (...values: TValues) => TResult) {
const values = new Array(accessors.length)
for (let i = 0; i < accessors.length; i++) {
const _value = accessors[i]()
if (_value === undefined || _value === null || _value === false) return undefined
values[i] = _value
}
return callback ? callback(...(values as any)) : values
}
return callback
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment