# assertions-simplified

> A collection of simple runtime assertions inspired by Facebook's invariant and warning.

Latest version **1.0.1** (published 2017-07-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install assertions-simplified
pnpm add assertions-simplified
yarn add assertions-simplified
bun add assertions-simplified
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2017-07-03 |
| First published | 2017-07-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Ivan Saskor |
| Maintainers | ivan-saskor |
| Keywords | simple, assertions, runtime, runtime assertions, assert, invariant, warning, error, safe, safe assert, safe invariant, safe warning, safe error, production safe, production safe assert, production safe invariant, production safe warning, production safe error |

## Links

- npm: https://www.npmjs.com/package/assertions-simplified
- Repository: https://github.com/ivan-saskor/assertions-simplified
- Homepage: https://github.com/ivan-saskor/assertions-simplified#readme
- Issues: https://github.com/ivan-saskor/assertions-simplified/issues
- npm.io page: https://npm.io/package/assertions-simplified

## Dependencies (2)

- [lodash](https://npm.io/package/lodash.md) ^4.17.4
- [in-production](https://npm.io/package/in-production.md) ^1.0.1

## Alternatives

- [@sentry/react-native](https://npm.io/package/@sentry/react-native.md) — 2.6M weekly downloads
- [@ardatan/aggregate-error](https://npm.io/package/@ardatan/aggregate-error.md) — 708.1K weekly downloads
- [custom-error-generator](https://npm.io/package/custom-error-generator.md) — 2.0K weekly downloads
- [@technik-sde/prosemirror-recreate-transform](https://npm.io/package/@technik-sde/prosemirror-recreate-transform.md) — 1.5K weekly downloads
- [@suchipi/error-utils](https://npm.io/package/@suchipi/error-utils.md) — 78 weekly downloads

## Recent versions

- 1.0.1 (latest) — 2017-07-03

## README

# assertions-simplified
A collection of simple runtime assertions inspired by
[invariant](https://github.com/facebook/fbjs/blob/master/packages/fbjs/src/__forks__/invariant.js) and
[warning](https://github.com/facebook/fbjs/blob/master/packages/fbjs/src/__forks__/warning.js) from
[Facebook JS](https://github.com/facebook/fbjs).

## Installation

```
npm install assertions-simplified --save
```

## API

#### `warning(message?: string)`

Writes an error with stack trace in the console log. If 'break on all exceptions' is enabled execution will be paused
on specified breakpoint.

#### `safeWarning(message?: string)`

Does nothing in production. Otherwise same as warning.

#### `error(message?: string)`

Throws an error unconditionally.

#### `safeError(message?: string)`

Does nothing in production. Otherwise same as error.

#### `invariant(condition: boolean, message?: string)`

Throws an error if invariant condition is not met.

#### `safeInvariant(condition: boolean, message?: string)`

Does nothing in production. Otherwise same as invariant.

## Usage

```js

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';
  }
}
 
```

## License

MIT

---
_Source: https://npm.io/package/assertions-simplified · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
