Created
July 30, 2017 22:46
-
-
Save chriseppstein/9c8d4052bc635d09079d88591faaa0c9 to your computer and use it in GitHub Desktop.
This file contains 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 { assert } from "chai"; | |
export function isDefined<X>(value: X | undefined): {and: (cb: (defValue: X) => any) => void } { | |
if (value) { | |
return { | |
and: function(cb: (v: X) => void) { | |
cb(value); | |
} | |
}; | |
} else { | |
assert.isDefined(value); | |
throw new Error("this is unreachable"); | |
} | |
} | |
export function isNotNull<X>(value: X | null): {and: (cb: (defValue: X) => any) => void } { | |
if (value) { | |
return { | |
and: function(cb: (v: X) => void) { | |
cb(value); | |
} | |
}; | |
} else { | |
assert.isNotNull(value); | |
throw new Error("this is unreachable"); | |
} | |
} | |
export function isExisting<X>(value: X | null | undefined): {and: (cb: (defValue: X) => any) => void } { | |
let exists: X | undefined = undefined; | |
isDefined(value).and((value) => { | |
isNotNull(value).and((value) => { | |
exists = value; | |
}); | |
}); | |
if (exists) { | |
return exists; | |
} else { | |
throw new Error("this is unreachable"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment