tiny-invariant
Advanced tools
Comparing version 1.3.1 to 1.3.2
@@ -1,1 +0,21 @@ | ||
export default function invariant(condition: any, message?: string | (() => string)): asserts condition; | ||
/** | ||
* `invariant` is used to [assert](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions) that the `condition` is [truthy](https://github.com/getify/You-Dont-Know-JS/blob/bdbe570600d4e1107d0b131787903ca1c9ec8140/up%20%26%20going/ch2.md#truthy--falsy). | ||
* | ||
* 💥 `invariant` will `throw` an `Error` if the `condition` is [falsey](https://github.com/getify/You-Dont-Know-JS/blob/bdbe570600d4e1107d0b131787903ca1c9ec8140/up%20%26%20going/ch2.md#truthy--falsy) | ||
* | ||
* 🤏 `message`s are not displayed in production environments to help keep bundles small | ||
* | ||
* @example | ||
* | ||
* ```ts | ||
* const value: Person | null = { name: 'Alex' }; | ||
* invariant(value, 'Expected value to be a person'); | ||
* // type of `value`` has been narrowed to `Person` | ||
* ``` | ||
*/ | ||
export default function invariant(condition: any, | ||
/** | ||
* 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; |
@@ -1,1 +0,21 @@ | ||
export default function invariant(condition: any, message?: string | (() => string)): asserts condition; | ||
/** | ||
* `invariant` is used to [assert](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions) that the `condition` is [truthy](https://github.com/getify/You-Dont-Know-JS/blob/bdbe570600d4e1107d0b131787903ca1c9ec8140/up%20%26%20going/ch2.md#truthy--falsy). | ||
* | ||
* 💥 `invariant` will `throw` an `Error` if the `condition` is [falsey](https://github.com/getify/You-Dont-Know-JS/blob/bdbe570600d4e1107d0b131787903ca1c9ec8140/up%20%26%20going/ch2.md#truthy--falsy) | ||
* | ||
* 🤏 `message`s are not displayed in production environments to help keep bundles small | ||
* | ||
* @example | ||
* | ||
* ```ts | ||
* const value: Person | null = { name: 'Alex' }; | ||
* invariant(value, 'Expected value to be a person'); | ||
* // type of `value`` has been narrowed to `Person` | ||
* ``` | ||
*/ | ||
export default function invariant(condition: any, | ||
/** | ||
* 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; |
{ | ||
"name": "tiny-invariant", | ||
"version": "1.3.1", | ||
"version": "1.3.2", | ||
"description": "A tiny invariant function", | ||
@@ -61,3 +61,3 @@ "author": "Alex Reardon <alexreardon@gmail.com>", | ||
"prettier:check": "yarn prettier --write src/** test/**", | ||
"typescript:check": "yarn tsc --noEmit src/*.ts test/*.ts", | ||
"typescript:check": "tsc --noEmit", | ||
"validate": "yarn prettier:check && yarn typescript:check", | ||
@@ -68,3 +68,3 @@ "build:clean": "rimraf dist", | ||
"build:typescript:esm": "tsc ./src/tiny-invariant.ts --emitDeclarationOnly --declaration --outDir ./dist/esm", | ||
"build:dist": "yarn rollup --config rollup.config.js", | ||
"build:dist": "yarn rollup --config rollup.config.mjs", | ||
"build": "yarn build:clean && yarn build:dist && yarn build:typescript && yarn build:typescript:esm", | ||
@@ -74,18 +74,19 @@ "prepublishOnly": "yarn build" | ||
"devDependencies": { | ||
"@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", | ||
"@rollup/plugin-replace": "^5.0.5", | ||
"@rollup/plugin-typescript": "^11.1.6", | ||
"@size-limit/preset-small-lib": "^11.0.2", | ||
"@types/jest": "^29.5.12", | ||
"@types/node": "^20.11.20", | ||
"@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", | ||
"expect-type": "^0.17.3", | ||
"jest": "^29.7.0", | ||
"prettier": "^3.2.5", | ||
"rimraf": "^5.0.5", | ||
"rollup": "^4.12.0", | ||
"rollup-plugin-terser": "^7.0.2", | ||
"size-limit": "^8.1.0", | ||
"ts-jest": "^29.0.2", | ||
"tslib": "^2.4.0", | ||
"typescript": "^4.8.4" | ||
"size-limit": "^11.0.2", | ||
"ts-jest": "^29.1.2", | ||
"tslib": "^2.6.2", | ||
"typescript": "^5.3.3" | ||
} | ||
} |
@@ -34,4 +34,8 @@ # tiny-invariant 🔬💥 | ||
The [`library: invariant`](https://www.npmjs.com/package/invariant) supports passing in arguments to the `invariant` function in a sprintf style `(condition, format, a, b, c, d, e, f)`. It has internal logic to execute the sprintf substitutions. The sprintf logic is not removed in production builds. `tiny-invariant` has dropped all of the sprintf logic. `tiny-invariant` allows you to pass a single string message. With [template literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) there is really no need for a custom message formatter to be built into the library. If you need a multi part message you can just do this: `invariant(condition, 'Hello, ${name} - how are you today?')` | ||
The [`library: invariant`](https://www.npmjs.com/package/invariant) supports passing in arguments to the `invariant` function in a sprintf style `(condition, format, a, b, c, d, e, f)`. It has internal logic to execute the sprintf substitutions. The sprintf logic is not removed in production builds. `tiny-invariant` has dropped all of the sprintf logic. `tiny-invariant` allows you to pass a single string message. With [template literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) there is really no need for a custom message formatter to be built into the library. If you need a multi part message you can just do this: | ||
```js | ||
invariant(condition, `Hello, ${name} - how are you today?`); | ||
``` | ||
## Type narrowing | ||
@@ -38,0 +42,0 @@ |
const isProduction: boolean = process.env.NODE_ENV === 'production'; | ||
const prefix: string = 'Invariant failed'; | ||
// Throw an error if the condition fails | ||
// Strip out error messages for production | ||
// > Not providing an inline default argument for message as the result is smaller | ||
/** | ||
* `invariant` is used to [assert](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions) that the `condition` is [truthy](https://github.com/getify/You-Dont-Know-JS/blob/bdbe570600d4e1107d0b131787903ca1c9ec8140/up%20%26%20going/ch2.md#truthy--falsy). | ||
* | ||
* 💥 `invariant` will `throw` an `Error` if the `condition` is [falsey](https://github.com/getify/You-Dont-Know-JS/blob/bdbe570600d4e1107d0b131787903ca1c9ec8140/up%20%26%20going/ch2.md#truthy--falsy) | ||
* | ||
* 🤏 `message`s are not displayed in production environments to help keep bundles small | ||
* | ||
* @example | ||
* | ||
* ```ts | ||
* const value: Person | null = { name: 'Alex' }; | ||
* invariant(value, 'Expected value to be a person'); | ||
* // type of `value`` has been narrowed to `Person` | ||
* ``` | ||
*/ | ||
export default function invariant( | ||
condition: any, | ||
// Can provide a string, or a function that returns a string for cases where | ||
// the message takes a fair amount of effort to compute | ||
// Not providing an inline default argument for message as the result is smaller | ||
/** | ||
* 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), | ||
@@ -12,0 +27,0 @@ ): asserts condition { |
14809
163
110
16