@guanghechen/invariant 7.0.0 → 7.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/CHANGELOG.md +32 -0
  2. package/LICENSE +21 -0
  3. package/README.md +55 -17
  4. package/package.json +13 -17
package/CHANGELOG.md ADDED
@@ -0,0 +1,32 @@
1
+ # Change Log
2
+
3
+ ## 7.0.2
4
+
5
+ ### Patch Changes
6
+
7
+ - chore: vitest config auto-load aliases and coverage thresholds; style/doc formatting updates
8
+
9
+ All notable changes to this project will be documented in this file. See
10
+ [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
11
+
12
+ ## 7.0.1 (2025-02-07)
13
+
14
+ ### Improvements
15
+
16
+ - Clean up build configs and standardize package exports
17
+
18
+ ### Documentation
19
+
20
+ - Update README.md
21
+
22
+ ### Miscellaneous
23
+
24
+ - Add LICENSE file
25
+ - Remove node engine restrictions
26
+ - Migrate from lerna to changesets
27
+
28
+ ## 7.0.0 (2025-01-15)
29
+
30
+ ### Improvements
31
+
32
+ - Upgrade to stable release
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023-present guanghechen (https://github.com/guanghechen)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -43,8 +43,8 @@
43
43
  </header>
44
44
  <br/>
45
45
 
46
- An invariant function, which takes a `condition` and an optional `message` value,
47
- and throws an error when the given condition fails.
46
+ A simple invariant function that throws an error when the given condition fails. Error messages are
47
+ stripped in production builds (`NODE_ENV === 'production'`) for smaller bundle size.
48
48
 
49
49
  ## Install
50
50
 
@@ -60,29 +60,67 @@ and throws an error when the given condition fails.
60
60
  yarn add @guanghechen/invariant
61
61
  ```
62
62
 
63
-
64
63
  ## Usage
65
64
 
66
- - Syntax
65
+ ### Basic Usage
67
66
 
68
- ```typescript
69
- function invariant(
70
- condition: boolean,
71
- message?: string | (() => string),
72
- ): asserts condition
73
- ```
67
+ ```typescript
68
+ import invariant from '@guanghechen/invariant'
74
69
 
75
- - Demo
70
+ function divide(a: number, b: number): number {
71
+ invariant(b !== 0, 'Division by zero is not allowed')
72
+ return a / b
73
+ }
76
74
 
77
- ```typescript
78
- import invariant from '@guanghechen/invariant'
75
+ divide(10, 2) // 5
76
+ divide(10, 0) // throws Error: "Invariant failed: Division by zero is not allowed"
77
+ ```
79
78
 
80
- invariant(typeof window !== 'undefined', '`window` is not defined.')
81
- invariant(typeof window !== 'undefined', () => '`window` is not defined:' + window)
82
- ```
79
+ ### With Lazy Message
80
+
81
+ Use a function for expensive message computation:
82
+
83
+ ```typescript
84
+ import { invariant } from '@guanghechen/invariant'
85
+
86
+ function processUser(user: User | null) {
87
+ invariant(user !== null, () => `User not found: ${JSON.stringify(context)}`)
88
+ // TypeScript now knows `user` is not null
89
+ return user.name
90
+ }
91
+ ```
92
+
93
+ ### TypeScript Type Narrowing
94
+
95
+ The function uses `asserts condition` for proper type narrowing:
96
+
97
+ ```typescript
98
+ import invariant from '@guanghechen/invariant'
99
+
100
+ function getValue(map: Map<string, number>, key: string): number {
101
+ const value = map.get(key)
102
+ invariant(value !== undefined, `Key "${key}" not found in map`)
103
+ // TypeScript knows `value` is `number` here, not `number | undefined`
104
+ return value
105
+ }
106
+ ```
107
+
108
+ ### API
109
+
110
+ ```typescript
111
+ function invariant(
112
+ condition: boolean,
113
+ message?: string | (() => string) | null,
114
+ ): asserts condition
115
+ ```
116
+
117
+ - `condition`: The condition to assert. If falsy, an error is thrown.
118
+ - `message`: Optional error message (string or function returning string).
83
119
 
120
+ ## Reference
84
121
 
85
- ## Related
122
+ - [homepage][homepage]
123
+ - Inspired by [tiny-invariant][tiny-invariant]
86
124
 
87
125
  [homepage]: https://github.com/guanghechen/sora/tree/@guanghechen/invariant@7.0.0/packages/invariant#readme
88
126
  [tiny-invariant]: https://github.com/alexreardon/tiny-invariant
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@guanghechen/invariant",
3
- "version": "7.0.0",
3
+ "version": "7.0.2",
4
4
  "description": "A simple invariant function",
5
5
  "author": {
6
6
  "name": "guanghechen",
@@ -8,17 +8,17 @@
8
8
  },
9
9
  "repository": {
10
10
  "type": "git",
11
- "url": "https://github.com/guanghechen/sora/tree/@guanghechen/invariant@6.0.5",
11
+ "url": "https://github.com/guanghechen/sora/tree/@guanghechen/invariant@7.0.0",
12
12
  "directory": "packages/invariant"
13
13
  },
14
- "homepage": "https://github.com/guanghechen/sora/tree/@guanghechen/invariant@6.0.5/packages/invariant#readme",
14
+ "homepage": "https://github.com/guanghechen/sora/tree/@guanghechen/invariant@7.0.0/packages/invariant#readme",
15
15
  "type": "module",
16
16
  "exports": {
17
17
  ".": {
18
+ "types": "./lib/types/index.d.ts",
18
19
  "source": "./src/index.ts",
19
20
  "import": "./lib/esm/index.mjs",
20
- "require": "./lib/cjs/index.cjs",
21
- "types": "./lib/types/index.d.ts"
21
+ "require": "./lib/cjs/index.cjs"
22
22
  }
23
23
  },
24
24
  "source": "./src/index.ts",
@@ -26,16 +26,6 @@
26
26
  "module": "./lib/esm/index.mjs",
27
27
  "types": "./lib/types/index.d.ts",
28
28
  "license": "MIT",
29
- "scripts": {
30
- "build": "rollup -c ../../rollup.config.mjs",
31
- "clean": "rimraf lib",
32
- "test": "vitest run --config ../../vitest.config.ts",
33
- "test:coverage": "vitest run --config ../../vitest.config.ts --coverage",
34
- "test:update": "vitest run --config ../../vitest.config.ts -u"
35
- },
36
- "engines": {
37
- "node": ">= 18.0.0"
38
- },
39
29
  "files": [
40
30
  "lib/",
41
31
  "!lib/**/*.map",
@@ -44,5 +34,11 @@
44
34
  "LICENSE",
45
35
  "README.md"
46
36
  ],
47
- "gitHead": "12990a720b31d50d217e2e17a6191256dc94eda6"
48
- }
37
+ "scripts": {
38
+ "build": "rollup -c ../../rollup.config.mjs",
39
+ "clean": "rimraf lib",
40
+ "test": "vitest run --config ../../vitest.config.ts",
41
+ "test:coverage": "vitest run --config ../../vitest.config.ts --coverage",
42
+ "test:update": "vitest run --config ../../vitest.config.ts -u"
43
+ }
44
+ }