Skip to content

Instantly share code, notes, and snippets.

@sagarpanchal
Created October 5, 2023 12:21
Show Gist options
  • Save sagarpanchal/c7390d877b00d141b0121964f836a4df to your computer and use it in GitHub Desktop.
Save sagarpanchal/c7390d877b00d141b0121964f836a4df to your computer and use it in GitHub Desktop.
TS Chain
export function chain<T>(object: T) {
const call =
<V>(previousResult: V) =>
<U>(callback: (object: T, result: V) => U) => {
const result = callback(object, previousResult)
return { call: call(result), result: () => result, object: () => object }
}
return { call: call(undefined), result: () => undefined, object: () => object }
}
chain.debug = <T, V>(object: T, result: V) => {
console.info({ object, result })
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment