package/LICENSE000644 0000002061 3560116604 010264 0ustar00000000 000000 MIT License Copyright (c) 2019 Alexander Reardon Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.package/dist/tiny-invariant.cjs.js000644 0000000716 3560116604 014317 0ustar00000000 000000 'use strict'; 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); } module.exports = invariant; package/dist/tiny-invariant.esm.js000644 0000000704 3560116604 014321 0ustar00000000 000000 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); } export { invariant as default }; package/src/tiny-invariant.flow.js000644 0000000462 3560116604 014331 0ustar00000000 000000 // @flow // This file is not actually executed // It is just used by flow for typing const prefix: string = 'Invariant failed'; export default function invariant(condition: mixed, message?: string | (() => string)) { if (condition) { return; } throw new Error(`${prefix}: ${message || ''}`); } package/dist/esm/tiny-invariant.js000644 0000000704 3560116604 014322 0ustar00000000 000000 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); } export { invariant as default }; package/dist/tiny-invariant.js000644 0000001511 3560116604 013533 0ustar00000000 000000 (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'; 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; })); package/dist/tiny-invariant.min.js000644 0000000456 3560116604 014324 0ustar00000000 000000 !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")}})); package/dist/esm/package.json000644 0000000025 3560116604 013272 0ustar00000000 000000 { "type": "module" } package/package.json000644 0000005031 3560116604 011545 0ustar00000000 000000 { "name": "tiny-invariant", "version": "1.3.3", "description": "A tiny invariant function", "author": "Alex Reardon ", "license": "MIT", "keywords": [ "invariant", "error", "assert", "asserts" ], "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, "files": [ "/dist", "/src" ], "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", "test:size": "yarn build && yarn size-limit", "prettier:write": "yarn prettier --debug-check src/** test/**", "prettier:check": "yarn prettier --write src/** test/**", "typescript:check": "tsc --noEmit", "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.mjs", "build": "yarn build:clean && yarn build:dist && yarn build:typescript && yarn build:typescript:esm", "prepublishOnly": "yarn build" }, "devDependencies": { "@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.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": "^11.0.2", "ts-jest": "^29.1.2", "tslib": "^2.6.2", "typescript": "^5.3.3" } } package/README.md000644 0000010443 3560116604 010541 0ustar00000000 000000 # 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) ![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) A tiny [`invariant`](https://www.npmjs.com/package/invariant) alternative. ## What is `invariant`? An `invariant` function takes a value, and if the value is [falsy](https://github.com/getify/You-Dont-Know-JS/blob/bdbe570600d4e1107d0b131787903ca1c9ec8140/up%20%26%20going/ch2.md#truthy--falsy) then the `invariant` function will throw. If the value is [truthy](https://github.com/getify/You-Dont-Know-JS/blob/bdbe570600d4e1107d0b131787903ca1c9ec8140/up%20%26%20going/ch2.md#truthy--falsy), then the function will not throw. ```js import invariant from 'tiny-invariant'; invariant(truthyValue, 'This should not throw!'); invariant(falsyValue, 'This will throw!'); // Error('Invariant violation: This will throw!'); ``` 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`? 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 `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 | (() => string)) => void` - `condition` is required and can be anything - `message` optional `string` or a function that returns a `string` (`() => string`) ## Installation ```bash # yarn yarn add tiny-invariant # npm npm install tiny-invariant --save ``` ## Dropping your `message` for kb savings! Big idea: you will want your compiler to convert this code: ```js invariant(condition, 'My cool message that takes up a lot of kbs'); ``` Into this: ```js if (!condition) { if ('production' !== process.env.NODE_ENV) { invariant(false, 'My cool message that takes up a lot of kbs'); } else { invariant(false); } } ``` - **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) Your bundler can then drop the code in the `"production" !== process.env.NODE_ENV` block for your production builds to end up with this: ```js if (!condition) { invariant(false); } ``` - 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) ## Builds - We have a `es` (EcmaScript module) build - We have a `cjs` (CommonJS) build - We have a `umd` (Universal module definition) build in case you needed it We expect `process.env.NODE_ENV` to be available at module compilation. We cache this value ## That's it! 🤘 package/dist/esm/tiny-invariant.d.ts000644 0000002023 3560116604 014552 0ustar00000000 000000 /** * `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; package/dist/tiny-invariant.d.ts000644 0000002023 3560116604 013766 0ustar00000000 000000 /** * `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; package/src/tiny-invariant.ts000644 0000003462 3560116604 013400 0ustar00000000 000000 const isProduction: boolean = process.env.NODE_ENV === 'production'; const prefix: string = 'Invariant failed'; /** * `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, // 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), ): asserts condition { if (condition) { return; } // Condition not passed // In production we strip the message but still throw if (isProduction) { throw new Error(prefix); } // When not in production we allow the message to pass through // *This block will be removed in production builds* 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); }