tiny-invariant
Advanced tools
Comparing version 1.1.0 to 1.3.1
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var isProduction = process.env.NODE_ENV === 'production'; | ||
@@ -13,4 +12,7 @@ var prefix = 'Invariant failed'; | ||
} | ||
throw new Error(prefix + ": " + (message || '')); | ||
var provided = typeof message === 'function' ? message() : message; | ||
var value = provided ? "".concat(prefix, ": ").concat(provided) : prefix; | ||
throw new Error(value); | ||
} | ||
exports.default = invariant; | ||
module.exports = invariant; |
@@ -1,1 +0,1 @@ | ||
export default function invariant(condition: any, message?: string): asserts condition; | ||
export default function invariant(condition: any, message?: string | (() => string)): asserts condition; |
@@ -10,5 +10,7 @@ var isProduction = process.env.NODE_ENV === 'production'; | ||
} | ||
throw new Error(prefix + ": " + (message || '')); | ||
var provided = typeof message === 'function' ? message() : message; | ||
var value = provided ? "".concat(prefix, ": ").concat(provided) : prefix; | ||
throw new Error(value); | ||
} | ||
export default invariant; | ||
export { invariant as default }; |
@@ -1,20 +0,23 @@ | ||
(function (factory) { | ||
typeof define === 'function' && define.amd ? define(factory) : | ||
factory(); | ||
}((function () { 'use strict'; | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
typeof define === 'function' && define.amd ? define(factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.invariant = factory()); | ||
})(this, (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 (isProduction) { | ||
throw new Error(prefix); | ||
} | ||
throw new Error(prefix + ": " + (message || '')); | ||
} | ||
exports.default = invariant; | ||
var isProduction = process.env.NODE_ENV === 'production'; | ||
var prefix = 'Invariant failed'; | ||
function invariant(condition, message) { | ||
if (condition) { | ||
return; | ||
} | ||
if (isProduction) { | ||
throw new Error(prefix); | ||
} | ||
var provided = typeof message === 'function' ? message() : message; | ||
var value = provided ? "".concat(prefix, ": ").concat(provided) : prefix; | ||
throw new Error(value); | ||
} | ||
}))); | ||
return invariant; | ||
})); |
@@ -1,1 +0,1 @@ | ||
!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")}})); | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):(e="undefined"!=typeof globalThis?globalThis:e||self).invariant=n()}(this,(function(){"use strict";return function(e,n){if(!e)throw new Error("Invariant failed")}})); |
{ | ||
"name": "tiny-invariant", | ||
"version": "1.1.0", | ||
"version": "1.3.1", | ||
"description": "A tiny invariant function", | ||
"author": "Alex Reardon <alexreardon@gmail.com>", | ||
"license": "MIT", | ||
"keywords": [ | ||
"invariant", | ||
"error" | ||
"error", | ||
"assert", | ||
"asserts" | ||
], | ||
"description": "A tiny invariant function", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/alexreardon/tiny-invariant.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/alexreardon/tiny-invariant/issues" | ||
}, | ||
"main": "dist/tiny-invariant.cjs.js", | ||
"module": "dist/tiny-invariant.esm.js", | ||
"types": "dist/tiny-invariant.d.ts", | ||
"exports": { | ||
".": { | ||
"import": "./dist/esm/tiny-invariant.js", | ||
"default": { | ||
"types": "./dist/tiny-invariant.d.ts", | ||
"default": "./dist/tiny-invariant.cjs.js" | ||
} | ||
} | ||
}, | ||
"sideEffects": false, | ||
@@ -17,39 +37,53 @@ "files": [ | ||
], | ||
"author": "Alex Reardon <alexreardon@gmail.com>", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/alexreardon/tiny-invariant.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/alexreardon/tiny-invariant/issues" | ||
}, | ||
"license": "MIT", | ||
"size-limit": [ | ||
{ | ||
"path": "dist/tiny-invariant.min.js", | ||
"limit": "217B" | ||
}, | ||
{ | ||
"path": "dist/tiny-invariant.js", | ||
"limit": "267B" | ||
}, | ||
{ | ||
"path": "dist/tiny-invariant.cjs.js", | ||
"limit": "171B" | ||
}, | ||
{ | ||
"path": "dist/tiny-invariant.esm.js", | ||
"import": "foo", | ||
"limit": "112B" | ||
} | ||
], | ||
"scripts": { | ||
"test": "yarn jest", | ||
"lint": "yarn prettier:check", | ||
"test:size": "yarn build && yarn size-limit", | ||
"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", | ||
"typescript:check": "yarn tsc --noEmit src/*.ts test/*.ts", | ||
"validate": "yarn prettier:check && yarn typescript:check", | ||
"build:clean": "rimraf dist", | ||
"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:typescript:esm": "tsc ./src/tiny-invariant.ts --emitDeclarationOnly --declaration --outDir ./dist/esm", | ||
"build:dist": "yarn rollup --config rollup.config.js", | ||
"build": "yarn build:clean && yarn build:dist && yarn build:typescript", | ||
"build": "yarn build:clean && yarn build:dist && yarn build:typescript && yarn build:typescript:esm", | ||
"prepublishOnly": "yarn build" | ||
}, | ||
"devDependencies": { | ||
"@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" | ||
"@rollup/plugin-replace": "^4.0.0", | ||
"@rollup/plugin-typescript": "^8.5.0", | ||
"@size-limit/preset-small-lib": "^8.1.0", | ||
"@types/jest": "^29.0.3", | ||
"@types/rollup": "^0.54.0", | ||
"expect-type": "^0.14.2", | ||
"jest": "^29.0.3", | ||
"prettier": "^2.7.1", | ||
"rimraf": "^3.0.2", | ||
"rollup": "^2.79.1", | ||
"rollup-plugin-terser": "^7.0.2", | ||
"size-limit": "^8.1.0", | ||
"ts-jest": "^29.0.2", | ||
"tslib": "^2.4.0", | ||
"typescript": "^4.8.4" | ||
} | ||
} |
@@ -24,2 +24,10 @@ # tiny-invariant 🔬💥 | ||
You can also provide a function to generate your message, for when your message is expensive to create | ||
```js | ||
import invariant from 'tiny-invariant'; | ||
invariant(value, () => getExpensiveMessage()); | ||
``` | ||
## Why `tiny-invariant`? | ||
@@ -39,6 +47,6 @@ | ||
## API: `(condition: any, message?: string) => void` | ||
## API: `(condition: any, message?: string | (() => string)) => void` | ||
- `condition` is required and can be anything | ||
- `message` is an optional string | ||
- `message` optional `string` or a function that returns a `string` (`() => string`) | ||
@@ -52,3 +60,3 @@ ## Installation | ||
# npm | ||
npm add tiny-invariant --save | ||
npm install tiny-invariant --save | ||
``` | ||
@@ -92,3 +100,3 @@ | ||
- We have a `es` (EcmaScript module) build (because you _know_ you want to deduplicate this super heavy library) | ||
- We have a `es` (EcmaScript module) build | ||
- We have a `cjs` (CommonJS) build | ||
@@ -95,0 +103,0 @@ - We have a `umd` (Universal module definition) build in case you needed it |
@@ -7,3 +7,3 @@ // @flow | ||
export default function invariant(condition: mixed, message?: string) { | ||
export default function invariant(condition: mixed, message?: string | (() => string)) { | ||
if (condition) { | ||
@@ -10,0 +10,0 @@ return; |
@@ -1,2 +0,1 @@ | ||
// @flow | ||
const isProduction: boolean = process.env.NODE_ENV === 'production'; | ||
@@ -10,3 +9,5 @@ const prefix: string = 'Invariant failed'; | ||
condition: any, | ||
message?: string, | ||
// Can provide a string, or a function that returns a string for cases where | ||
// the message takes a fair amount of effort to compute | ||
message?: string | (() => string), | ||
): asserts condition { | ||
@@ -25,3 +26,10 @@ if (condition) { | ||
// *This block will be removed in production builds* | ||
throw new Error(`${prefix}: ${message || ''}`); | ||
const provided: string | undefined = typeof message === 'function' ? message() : message; | ||
// Options: | ||
// 1. message provided: `${prefix}: ${provided}` | ||
// 2. message not provided: prefix | ||
const value: string = provided ? `${prefix}: ${provided}` : prefix; | ||
throw new Error(value); | ||
} |
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
12186
13
105
106
15