A small collection of boolean check utilities to improve readability and prevent common mistakes.
For example, using if (!foo) return; as a type guard against undefined will also return when foo is 0 -- even if the code author chose that behavior intentionally, their intentions remain unclear to readers. Instead, if (isUndefined(foo)) return; disambiguates the author's intentions & no longer returns when foo is 0 '', or null.
import { isUndefined, } from '@antman/bool';
export const fooRepo = {
async tryGet(id: bigint): Foo | undefined {
const { rows: [ foo ] } = await pool.query('SELECT * FROM foo WHERE id = $1', [id]);
return foo;
},
async get(id: bigint): Foo {
const foo = await fooRepo.tryGet(id);
if (isUndefined(foo)) throw new MissingEntityError('foo', id);
return foo
}
}View the documentation