tiny-invariant
Advanced tools
Comparing version 1.0.6 to 1.1.0-beta.1
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var isProduction = process.env.NODE_ENV === 'production'; | ||
var prefix = 'Invariant failed'; | ||
function invariant(condition, message) { | ||
if (condition) { | ||
return; | ||
} | ||
if (isProduction) { | ||
throw new Error(prefix); | ||
} else { | ||
if (condition) { | ||
return; | ||
} | ||
if (isProduction) { | ||
throw new Error(prefix); | ||
} | ||
throw new Error(prefix + ": " + (message || '')); | ||
} | ||
} | ||
module.exports = invariant; | ||
exports.default = invariant; |
@@ -0,15 +1,13 @@ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var isProduction = process.env.NODE_ENV === 'production'; | ||
var prefix = 'Invariant failed'; | ||
function invariant(condition, message) { | ||
if (condition) { | ||
return; | ||
} | ||
if (isProduction) { | ||
throw new Error(prefix); | ||
} else { | ||
if (condition) { | ||
return; | ||
} | ||
if (isProduction) { | ||
throw new Error(prefix); | ||
} | ||
throw new Error(prefix + ": " + (message || '')); | ||
} | ||
} | ||
export default invariant; | ||
exports.default = invariant; |
@@ -1,20 +0,20 @@ | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
(function (factory) { | ||
typeof define === 'function' && define.amd ? define(factory) : | ||
(global = global || self, global.invariant = factory()); | ||
}(this, function () { 'use strict'; | ||
factory(); | ||
}((function () { 'use strict'; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var isProduction = process.env.NODE_ENV === 'production'; | ||
var prefix = 'Invariant failed'; | ||
function invariant(condition, message) { | ||
if (condition) { | ||
return; | ||
} | ||
{ | ||
if (condition) { | ||
return; | ||
} | ||
if (isProduction) { | ||
throw new Error(prefix); | ||
} | ||
throw new Error(prefix + ": " + (message || '')); | ||
} | ||
} | ||
exports.default = invariant; | ||
return invariant; | ||
})); | ||
}))); |
@@ -1,1 +0,1 @@ | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):(e=e||self).invariant=n()}(this,function(){"use strict";return function(e,n){if(!e)throw new Error("Invariant failed")}}); | ||
!function(e){"function"==typeof define&&define.amd?define(e):e()}((function(){"use strict";Object.defineProperty(exports,"__esModule",{value:!0});exports.default=function(e,n){if(!e)throw new Error("Invariant failed")}})); |
{ | ||
"name": "tiny-invariant", | ||
"version": "1.0.6", | ||
"version": "1.1.0-beta.1", | ||
"keywords": [ | ||
@@ -11,3 +11,3 @@ "invariant", | ||
"module": "dist/tiny-invariant.esm.js", | ||
"types": "src/index.d.ts", | ||
"types": "dist/tiny-invariant.d.ts", | ||
"sideEffects": false, | ||
@@ -29,27 +29,28 @@ "files": [ | ||
"test": "yarn jest", | ||
"lint": "yarn prettier --debug-check src/** test/**", | ||
"typecheck": "yarn flow", | ||
"validate": "yarn lint && yarn flow", | ||
"lint": "yarn prettier:check", | ||
"prettier:write": "yarn prettier --debug-check src/** test/**", | ||
"prettier:check": "yarn prettier --write src/** test/**", | ||
"typecheck": "yarn tsc --noEmit src/*.ts test/*.ts", | ||
"validate": "yarn lint && yarn typecheck", | ||
"build:clean": "rimraf dist", | ||
"build:flow": "echo \"// @flow\n\nexport * from '../src';\" > dist/tiny-invariant.cjs.js.flow", | ||
"build:flow": "cp src/tiny-invariant.js.flow dist/tiny-invariant.cjs.js.flow", | ||
"build:typescript": "tsc ./src/tiny-invariant.ts --emitDeclarationOnly --declaration --outDir ./dist", | ||
"build:dist": "yarn rollup --config rollup.config.js", | ||
"build": "yarn build:clean && yarn build:dist && yarn build:flow", | ||
"build": "yarn build:clean && yarn build:dist && yarn build:typescript", | ||
"prepublishOnly": "yarn build" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.5.0", | ||
"@babel/preset-env": "^7.5.0", | ||
"@babel/preset-flow": "^7.0.0", | ||
"@babel/runtime-corejs2": "^7.5.1", | ||
"babel-core": "7.0.0-bridge.0", | ||
"babel-jest": "^24.8.0", | ||
"flow-bin": "^0.102.0", | ||
"jest": "^24.8.0", | ||
"prettier": "^1.18.2", | ||
"rimraf": "^2.6.3", | ||
"rollup": "^1.16.6", | ||
"rollup-plugin-babel": "^4.3.3", | ||
"rollup-plugin-replace": "^2.2.0", | ||
"rollup-plugin-uglify": "^6.0.2" | ||
"@rollup/plugin-replace": "^2.3.0", | ||
"@rollup/plugin-typescript": "^3.0.0", | ||
"@types/jest": "^25.1.0", | ||
"jest": "^25.1.0", | ||
"prettier": "^1.19.1", | ||
"rimraf": "^3.0.1", | ||
"rollup": "^1.30.1", | ||
"rollup-plugin-terser": "^5.2.0", | ||
"ts-expect": "^1.1.0", | ||
"ts-jest": "^25.0.0", | ||
"tslib": "^1.10.0", | ||
"typescript": "^3.7.5" | ||
} | ||
} |
@@ -1,7 +0,8 @@ | ||
# `tiny-invariant` 🔬💥 | ||
# tiny-invariant 🔬💥 | ||
[![Build Status](https://travis-ci.org/alexreardon/tiny-invariant.svg?branch=master)](https://travis-ci.org/alexreardon/tiny-invariant) | ||
[![npm](https://img.shields.io/npm/v/tiny-invariant.svg)](https://www.npmjs.com/package/tiny-invariant) [![dependencies](https://david-dm.org/alexreardon/tiny-invariant.svg)](https://david-dm.org/alexreardon/tiny-invariant) | ||
[![min](https://img.shields.io/bundlephobia/min/tiny-invariant.svg)](https://www.npmjs.com/package/tiny-invariant) | ||
![types](https://img.shields.io/badge/types-typescript%20%7C%20flow-blueviolet) | ||
[![minzip](https://img.shields.io/bundlephobia/minzip/tiny-invariant.svg)](https://www.npmjs.com/package/tiny-invariant) | ||
[![Downloads per month](https://img.shields.io/npm/dm/tiny-invariant.svg)](https://www.npmjs.com/package/tiny-invariant) | ||
@@ -27,4 +28,14 @@ A tiny [`invariant`](https://www.npmjs.com/package/invariant) alternative. | ||
## API: `(condition: mixed, message?: string) => void` | ||
## Type narrowing | ||
`tiny-invariant` is useful for correctly narrowing types for `flow` and `typescript` | ||
```ts | ||
const value: Person | null = { name: 'Alex' }; // type of value == 'Person | null' | ||
invariant(value, 'Expected value to be a person'); | ||
// type of value has been narrowed to 'Person' | ||
``` | ||
## API: `(condition: any, message?: string) => void` | ||
- `condition` is required and can be anything | ||
@@ -39,3 +50,3 @@ - `message` is an optional string | ||
# bash | ||
# npm | ||
npm add tiny-invariant --save | ||
@@ -46,6 +57,4 @@ ``` | ||
We recommend using [`babel-plugin-dev-expression`](https://www.npmjs.com/package/babel-plugin-dev-expression) to remove the `message` argument from your `invariant` calls in production builds to save kbs! | ||
Big idea: you will want your compiler to convert this code: | ||
What it does it turn your code that looks like this: | ||
```js | ||
@@ -55,3 +64,3 @@ invariant(condition, 'My cool message that takes up a lot of kbs'); | ||
Into this | ||
Into this: | ||
@@ -68,5 +77,6 @@ ```js | ||
Your bundler can then drop the code in the `"production" !== process.env.NODE_ENV` block for your production builds | ||
- **Babel**: recommend [`babel-plugin-dev-expression`](https://www.npmjs.com/package/babel-plugin-dev-expression) | ||
- **TypeScript**: recommend [`tsdx`](https://github.com/jaredpalmer/tsdx#invariant) (or you can run `babel-plugin-dev-expression` after TypeScript compiling) | ||
Final result: | ||
Your bundler can then drop the code in the `"production" !== process.env.NODE_ENV` block for your production builds to end up with this: | ||
@@ -79,5 +89,4 @@ ```js | ||
> For `rollup` use [rollup-plugin-replace](https://github.com/rollup/rollup-plugin-replace) and set `NODE_ENV` to `production` and then `rollup` will treeshake out the unused code | ||
> | ||
> [`Webpack` instructions](https://webpack.js.org/guides/production/#specify-the-mode) | ||
- rollup: use [rollup-plugin-replace](https://github.com/rollup/rollup-plugin-replace) and set `NODE_ENV` to `production` and then `rollup` will treeshake out the unused code | ||
- Webpack: [instructions](https://webpack.js.org/guides/production/#specify-the-mode) | ||
@@ -84,0 +93,0 @@ ## Builds |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
9608
12
79
98
2