Skip to content

trabe/simple-guards

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple Guards

Build Status

Description

Simple utilities to add guard clauses to your JS code.

Guards raises an Error if a given condition is falsy.

Features

  • Guards not executed in production environment (process.env.NODE_ENV !== "production")
  • Guards can be checked lazily

Install

npm install simple-guards

If you use yarn, just:

yarn add simple-guards

Usage

The utility exports two methods: guard and guards.

To set a simple guard:

import { guard } from "simple-guards";

function division(numerator, denominator) {
  guard(denominator !== 0, "denominator cannot be zero");

  // more awful code
}

You can also pass a function to lazy evaluate an expensive guard:

function division(numerator, denominator) {
  guard(() => !expensiveDetectionOfZero(denominator), "denominator cannot be zero");

  // more awful code
}

If you want to lazy evaluate many guards you can use the guards method to avoid excessive wrapper functions:

import { guards } from "simple-guards";

function division(numerator, denominator) {
  guards(guard => {
    guard(!expensiveDetectionOfZero(numerator), "we don't want numerator to be zero");
    guard(!expensiveDetectionOfZero(denominator), "denominator cannot be zero");
  });

  // more awful code
}

Changelog

next

  • Updated all dependencies
  • No longer use Mocha. Just jest
  • Added ESLint + Prettier to make our code nicer
  • Use Travis CI

1.0.0

Initial release

About

Utils to add guard clauses to JS code

Resources

License

Stars

Watchers

Forks

Packages

No packages published