A collection of simple runtime assertions inspired by invariant and warning from Facebook JS.
npm install assertions-simplified --save
Writes an error with stack trace in the console log. If 'break on all exceptions' is enabled execution will be paused on specified breakpoint.
Does nothing in production. Otherwise same as warning.
Throws an error unconditionally.
Does nothing in production. Otherwise same as error.
Throws an error if invariant condition is not met.
Does nothing in production. Otherwise same as invariant.
import { warning, safeWarning } from 'assertions-simplified';
import { error, safeError } from 'assertions-simplified';
import { invariant, safeInvariant } from 'assertions-simplified';
//----------------------------------------------------------------------------------------
// Basic use-cases
//----------------------------------------------------------------------------------------
// Write an error in console log, and continue.
warning();
// Throw an error.
error();
// Throw an error when invariant condition is not meet.
invariant(name.length < 200);
//----------------------------------------------------------------------------------------
// With custom message
//----------------------------------------------------------------------------------------
warning('Oops! Something went slightly wrong!');
error('Oops! Something went terribly wrong!');
invariant(name.length < 200, 'Name is too long!');
//--------------------------------------------
// Safe variations
//--------------------------------------------
safeWarning(); // Do nothing in production.
safeError(); // Do nothing in production.
safeInvariant(); // Do nothing in production.
//--------------------------------------------
// Example: How to use safe error
//--------------------------------------------
function getFriendlyName(schoolGrade) {
switch (schoolGrade) {
case 9:
return 'Freshman';
case 10:
return 'Sophomore';
case 10:
return 'Junior';
case 10:
return 'Senior';
default:
safeError('Unsupported school grade!'); // stops execution in development only
return 'unknown';
}
}
MIT