Skip to content

Instantly share code, notes, and snippets.

@chriseppstein
Created July 30, 2017 22:46
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 chriseppstein/9c8d4052bc635d09079d88591faaa0c9 to your computer and use it in GitHub Desktop.
Save chriseppstein/9c8d4052bc635d09079d88591faaa0c9 to your computer and use it in GitHub Desktop.
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