outvariant
Advanced tools
Comparing version 1.2.0 to 1.2.1
@@ -37,7 +37,5 @@ "use strict"; | ||
if (_this.stack) { | ||
var prevStack = _this.stack; | ||
_this.stack = prevStack | ||
.split('\n') | ||
.slice(STACK_FRAMES_TO_IGNORE) | ||
.join('\n'); | ||
var nextStack = _this.stack.split('\n'); | ||
nextStack.splice(1, STACK_FRAMES_TO_IGNORE); | ||
_this.stack = nextStack.join('\n'); | ||
} | ||
@@ -44,0 +42,0 @@ return _this; |
{ | ||
"name": "outvariant", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "Type-safe implementation of invariant with positionals.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
# `outvariant` | ||
## Why? | ||
Type-safe implementation of invariant with positionals. | ||
- Type-safe implementation of invariant. Asserts the given predicate expression so it's treated as non-nullable after the `invariant` call: | ||
## Motivation | ||
### Type-safely | ||
This implementation asserts the given predicate expression so it's treated as non-nullable after the `invariant` call: | ||
```ts | ||
// Regular "invariant": | ||
// Regular invariant: | ||
invariant(user, 'Failed to fetch') | ||
user?.firstName // "user" is possibly undefined | ||
// The glorious "outvariant": | ||
// Outvariant: | ||
invariant(user, 'Failed to fetch') | ||
user.firstName // OK, "user" exists at this point | ||
user.firstName // OK, "invariant" ensured the "user" exists | ||
``` | ||
- Positionals support. | ||
### Positionals support | ||
This implementation uses [rest parameters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters) to support dynamic number of positionals: | ||
```js | ||
@@ -23,4 +29,26 @@ invariant(predicate, 'Expected %s but got %s', 'one', false) | ||
## Getting started | ||
## What is this for? | ||
Invariant is a shorthand function that asserts a given predicate and throws an error if that predicate is false. | ||
Compare these two pieces of code identical in behavior: | ||
```js | ||
if (!token) { | ||
throw new Error(`Expected a token to be set but got ${typeof token}`) | ||
} | ||
``` | ||
```js | ||
import { invariant } from 'outvariant' | ||
invariant(token, 'Expected a token to be set but got %s', typeof token) | ||
``` | ||
Using `invariant` reduces the visual nesting of the code and leads to cleaner error messages thanks to formatted positionals (i.e. the `%s` (string) positional above). | ||
## Usage | ||
### Install | ||
```sh | ||
@@ -32,2 +60,6 @@ npm install outvariant | ||
> You may want to install this library as a dev dependency (`-D`) based on your usage. | ||
### Write an assertion | ||
```js | ||
@@ -38,1 +70,32 @@ import { invariant } from 'outvariant' | ||
``` | ||
## Positionals | ||
The following positional tokens are supported: | ||
| Token | Expected value type | | ||
| --------- | ------------------------------------------------------- | | ||
| `%s` | String | | ||
| `%d`/`%i` | Number | | ||
| `%j` | JSON (non-stringified) | | ||
| `%o` | Arbitrary object or object-like (i.e. a class instance) | | ||
Whenever present in the error message, a positional token will look up the value to insert in its place from the arguments given to `invariant`. | ||
```js | ||
invariant( | ||
false, | ||
'Expected the "%s" property but got %j', | ||
// Note that positionals are sensitive to order: | ||
// - "firstName" replaces "%s" because it's first. | ||
// - {"id":1} replaces "%j" because it's second. | ||
'firstName', | ||
{ | ||
id: 1, | ||
} | ||
) | ||
``` | ||
## Contributing | ||
Please [open an issue](https://github.com/open-draft/outvariant/issues) or [submit a pull request](https://github.com/open-draft/outvariant/pulls) if you wish to contribute. Thank you. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8681
99
133