Skip to content

Instantly share code, notes, and snippets.

@bramblex
Created July 28, 2020 06:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bramblex/8b80d41f1dd94f0b3a63cdef829c4fd9 to your computer and use it in GitHub Desktop.
Save bramblex/8b80d41f1dd94f0b3a63cdef829c4fd9 to your computer and use it in GitHub Desktop.
function guard<T0, T1>(
v: T0,
...conditions: [(v: T0) => boolean, (V: T0) => T1][]
) {
for (const [condition, func] of conditions) {
if (condition(v)) {
return func(v);
}
}
throw new Error('Condition not match');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment