1.0.3 • Published 3 years ago
@awkewainze/checkverify v1.0.3
CheckVerify
A simple library for checking preconditions on functions. Heavily inspired by Guava's Precondition.
Methods starting with verify
throw on failure, while methods starting with is
return boolean
.
Sample usages
import { Check } from "@awkewainze/checkverify";
function doThing(param1: number, param2: string): void {
Check.verifyPositive(param1); // If `param1 <= 0` then throws generic error.
Check.verifyNotNullUndefinedOrEmpty(param2); // If param is `null`, `undefined`, or an empty string then throws generic error.
Check.verify(param1 < 100);
// Do things...
}
import { Check } from "@awkewainze/checkverify";
function doThing(param1: number): void {
if (!Check.isPositive(param1)) {
param1 = 1;
}
// Do things...
}
For more examples, check the check.test.ts
file!
Custom errors
To throw your own error or message, just pass as the second parameter.
import { Check } from "@awkewainze/checkverify";
function doThing(param1: number, param2: string): void {
Check.verifyPositive(param1, "param1 was not positive"); // Throws error with message provided.
Check.verifyNotNullUndefinedOrEmpty(param2, new Error("param2 was null or empty"));
Check.verify(param1 < 100, "param1 must be less than 100");
// Do things...
}
Notes
I will slowly add more useful checks over time as I need them, I just hate having to repeat code in multiple projects, and the verify
methods make error handling super easy.