Skip to content

Instantly share code, notes, and snippets.

@typoerr
Created February 4, 2017 04:33
Show Gist options
  • Save typoerr/4078ddf4dcd5cd04edcab4941d8e7843 to your computer and use it in GitHub Desktop.
Save typoerr/4078ddf4dcd5cd04edcab4941d8e7843 to your computer and use it in GitHub Desktop.
/**
* testがundefined or nullでなければthenを実行
* そうでなければfallbackを実行する
*
* @template R1
* @template R2
* @template R3
* @param {((() => R1) | R1)} test
* @param {(a: R1) => R2} then
* @param {() => R3} [fallback]
* @returns {(R2 | R3)}
*/
function whenExists<R1, R2, R3>(test: (() => R1) | R1, then: (a: R1) => R2, fallback?: () => R3): R2 | R3;
function whenExists<A, R1, R2, R3>(test: ((a: A) => R1) | R1, then: (a: R1) => R2, fallback?: () => R3): R2 | R3;
function whenExists<A1, A2, R1, R2, R3>(test: ((a1: A1, a2: A2) => R1) | R1,
then: (a: R1) => R2, fallback?: () => R3): R2 | R3;
function whenExists(test: Function | any, then: Function, fallback?: Function) {
let result = test;
if (typeof test === 'function') {
result = test();
}
if (existy(result)) return then(result);
return fallback && fallback();
}
/**
* null or undefinedでないことのvalidation
*
* @param {*} v
* @returns
*/
function existy(v: any) {
return !(v === null || v === undefined);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment