Last active
July 30, 2023 21:44
-
-
Save bigmistqke/7e0bb87569ee1f10d327ae808d51d3b8 to your computer and use it in GitHub Desktop.
when.ts
This file contains hidden or 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
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