Skip to content

Commit

Permalink
merge upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
lednhatkhanh committed Jan 16, 2025
1 parent 4208682 commit 1f41837
Show file tree
Hide file tree
Showing 16 changed files with 1,691 additions and 2,846 deletions.
12 changes: 6 additions & 6 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
version: 2
updates:
# Enable version updates for npm
- package-ecosystem: "npm"
- package-ecosystem: 'npm'
# Look for `package.json` and `lock` files in the `root` directory
directory: "/"
directory: '/'
# Always increase the version requirement to match the new version.
versioning-strategy: increase
# Check the npm registry for updates at the start of every week
schedule:
interval: "weekly"
day: "monday"
time: "08:00"
timezone: "Australia/Sydney"
interval: 'monthly'
day: 'monday'
time: '08:00'
timezone: 'Australia/Sydney'
17 changes: 11 additions & 6 deletions .github/workflows/bundle-size-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ jobs:
env:
CI_JOB_NUMBER: 1
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- name: Checkout code
uses: actions/checkout@v3

- name: Setup node
uses: actions/setup-node@v3
with:
node-version: '16'
node-version-file: '.nvmrc'
# Using node action to manage caching node_modules
cache: 'yarn'

# The size limit github action
- uses: andresz1/size-limit-action@v1
- name: Run size limit github action
uses: andresz1/size-limit-action@v1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
github_token: ${{ secrets.GITHUB_TOKEN }}
19 changes: 8 additions & 11 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,18 @@ jobs:

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: '16'
- name: Checkout code
uses: actions/checkout@v3

- name: Restore dependency cache
uses: actions/cache@v2
- name: Setup node
uses: actions/setup-node@v3
with:
path: '**/node_modules'
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}
node-version-file: '.nvmrc'
# Using node action to manage caching node_modules
cache: 'yarn'

- name: Install dependencies
run: yarn install

# Run tests
- name: Tests
- name: Run tests
run: yarn test
16 changes: 7 additions & 9 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@ jobs:

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: '16'
- name: Checkout code
uses: actions/checkout@v3

- name: Restore dependency cache
uses: actions/cache@v2
- name: Setup node
uses: actions/setup-node@v3
with:
path: '**/node_modules'
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}
node-version-file: '.nvmrc'
# Using node action to manage caching node_modules
cache: 'yarn'

- name: Install dependencies
run: yarn install
Expand Down
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
16.11.1
18.4.0
40 changes: 26 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
# 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)
![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/alexreardon/tiny-invariant/test.yml)
![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)
![npm bundle size](https://img.shields.io/bundlephobia/minzip/tiny-invariant)
![NPM Downloads](https://img.shields.io/npm/dm/tiny-invariant)

A tiny [`invariant`](https://www.npmjs.com/package/invariant) alternative.
`tiny-invariant` is a _tiny_, widely-supported, zero-dependency alternative to [`invariant`](https://www.npmjs.com/package/invariant).

`tiny-invariant` - when every byte counts!

## 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
```ts
import invariant from 'tiny-invariant';

invariant(truthyValue, 'This should not throw!');
Expand All @@ -21,17 +22,28 @@ 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
## 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 code for `sprintf` logic and instead encourages consumers to leverage [template literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) for message formatting.

```ts
invariant(condition, `Hello, ${name} - how are you today?`);
```

## Error Messages

```js
`tiny-invariant` allows you to pass a `string` message, or a function that returns a `string` message. Using a function that returns a message is helpful when your message is expensive to create.

```ts
import invariant from 'tiny-invariant';

invariant(condition, `Hello, ${name} - how are you today?`);

// Using a function is helpful when your message is expensive
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: `invariant(condition, 'Hello, ${name} - how are you today?')`
When `process.env.NODE_ENV` is set to `production`, the message will be replaced with the generic message `Invariant failed`.

## Type narrowing

Expand Down Expand Up @@ -62,13 +74,13 @@ npm install tiny-invariant --save

Big idea: you will want your compiler to convert this code:

```js
```ts
invariant(condition, 'My cool message that takes up a lot of kbs');
```

Into this:

```js
```ts
if (!condition) {
if ('production' !== process.env.NODE_ENV) {
invariant(false, 'My cool message that takes up a lot of kbs');
Expand All @@ -83,7 +95,7 @@ if (!condition) {

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
```ts
if (!condition) {
invariant(false);
}
Expand Down
11 changes: 4 additions & 7 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',

globals: {
'ts-jest': {
diagnostics: false,
},
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json'],
testEnvironment: 'node',
};
55 changes: 33 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@minswap/tiny-invariant",
"version": "1.2.0",
"name": "tiny-invariant",
"version": "1.3.3",
"description": "A tiny invariant function",
"author": "Alex Reardon <[email protected]>",
"license": "MIT",
Expand All @@ -20,6 +20,15 @@
"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",
Expand All @@ -28,51 +37,53 @@
"size-limit": [
{
"path": "dist/tiny-invariant.min.js",
"limit": "175B"
"limit": "217B"
},
{
"path": "dist/tiny-invariant.js",
"limit": "176B"
"limit": "267B"
},
{
"path": "dist/tiny-invariant.cjs.js",
"limit": "164B"
"limit": "171B"
},
{
"path": "dist/tiny-invariant.esm.js",
"import": "foo",
"limit": "49B"
"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": "yarn tsc --noEmit src/*.ts test/*.ts",
"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:dist": "yarn rollup --config rollup.config.js",
"build": "yarn build:clean && yarn build:dist && yarn build:typescript",
"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": "^3.0.0",
"@rollup/plugin-typescript": "^8.3.0",
"@size-limit/preset-small-lib": "^6.0.4",
"@types/jest": "^27.0.2",
"@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.24",
"@types/rollup": "^0.54.0",
"expect-type": "^0.13.0",
"jest": "^27.3.1",
"prettier": "^2.4.1",
"rimraf": "^3.0.2",
"rollup": "^2.58.3",
"expect-type": "^0.18.0",
"jest": "^29.7.0",
"prettier": "^3.2.5",
"rimraf": "^5.0.5",
"rollup": "^4.12.0",
"rollup-plugin-terser": "^7.0.2",
"size-limit": "^6.0.4",
"ts-jest": "^27.0.7",
"tslib": "^2.3.1",
"typescript": "^4.4.4"
"size-limit": "^11.0.2",
"ts-jest": "^29.1.2",
"tslib": "^2.6.2",
"typescript": "^5.3.3"
}
}
22 changes: 22 additions & 0 deletions rollup.config.js → rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,28 @@ export default [
},
plugins: [typescript({ module: 'ESNext' })],
},
// ESM build for "module": "node16" TypeScript projects (https://github.com/alexreardon/tiny-invariant/issues/144)
{
input,
output: {
file: 'dist/esm/tiny-invariant.js',
format: 'esm',
},
plugins: [
typescript({ module: 'ESNext' }),
// https://github.com/rollup/rollup/blob/69ff4181e701a0fe0026d0ba147f31bc86beffa8/build-plugins/emit-module-package-file.ts
{
generateBundle() {
this.emitFile({
fileName: 'package.json',
source: `{ "type": "module" }\n`,
type: 'asset',
});
},
name: 'emit-module-package-file',
},
],
},
// CommonJS build
{
input,
Expand Down
5 changes: 1 addition & 4 deletions src/tiny-invariant.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

const prefix: string = 'Invariant failed';

export default function invariant(
condition: mixed,
message?: string | (() => string),
) {
export default function invariant(condition: mixed, message?: string | (() => string)) {
if (condition) {
return;
}
Expand Down
28 changes: 21 additions & 7 deletions src/tiny-invariant.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
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),
): asserts condition {
if (condition) {
Expand All @@ -17,8 +32,7 @@ export default function invariant(
// 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;
const provided: string | undefined = typeof message === 'function' ? message() : message;

// Options:
// 1. message provided: `${prefix}: ${provided}`
Expand Down
Loading

0 comments on commit 1f41837

Please sign in to comment.