Skip to content
/ bool Public

A small collection of boolean check utilities to improve readability while preventing common mistakes.

License

Notifications You must be signed in to change notification settings

Antman261/bool

Repository files navigation

@antman/bool

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.

Usage

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

About

A small collection of boolean check utilities to improve readability while preventing common mistakes.

Topics

Resources

License

Stars

Watchers

Forks