is-kit 1.0.0 → 1.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.
- package/README.md +44 -22
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# is-kit
|
|
2
2
|
|
|
3
3
|
<p align="center">
|
|
4
|
-
<img src="https://raw.githubusercontent.com/nyaomaru/is-kit/main/docs/public/
|
|
4
|
+
<img src="https://raw.githubusercontent.com/nyaomaru/is-kit/main/docs/public/iskit-logo_700_500.png" width="700px" align="center" alt="is-kit logo" />
|
|
5
5
|
</p>
|
|
6
6
|
|
|
7
7
|
<p align="center">
|
|
@@ -11,15 +11,12 @@
|
|
|
11
11
|
<a href="https://jsr.io/@nyaomaru/is-kit">
|
|
12
12
|
<img src="https://img.shields.io/jsr/v/@nyaomaru/is-kit" alt="JSR">
|
|
13
13
|
</a>
|
|
14
|
-
<a href="https://github.com/nyaomaru/is-kit/blob/main/LICENSE">
|
|
15
|
-
<img src="https://img.shields.io/npm/l/@nyaomaru/is-kit.svg?sanitize=true" alt="License">
|
|
16
|
-
</a>
|
|
17
14
|
</p>
|
|
18
15
|
|
|
19
16
|
Lightweight, zero-dependency toolkit for building `isFoo` style type guards in TypeScript.
|
|
20
17
|
Runtime-safe 🛡️, composable 🧩, and ergonomic ✨.
|
|
21
18
|
|
|
22
|
-
[
|
|
19
|
+
[📚 full documentation](https://is-kit-docs.vercel.app/)
|
|
23
20
|
|
|
24
21
|
## Why?
|
|
25
22
|
|
|
@@ -63,32 +60,55 @@ Build `is` functions from tiny, composable pieces:
|
|
|
63
60
|
```ts
|
|
64
61
|
import {
|
|
65
62
|
define,
|
|
66
|
-
predicateToRefine,
|
|
67
63
|
and,
|
|
68
64
|
or,
|
|
69
65
|
not,
|
|
70
|
-
|
|
66
|
+
struct,
|
|
67
|
+
oneOfValues,
|
|
68
|
+
optional,
|
|
71
69
|
isNumber,
|
|
70
|
+
isString,
|
|
71
|
+
predicateToRefine,
|
|
72
72
|
} from 'is-kit';
|
|
73
73
|
|
|
74
|
-
//
|
|
75
|
-
const isShortString = define<string>(
|
|
76
|
-
|
|
74
|
+
// Define small guards
|
|
75
|
+
const isShortString = define<string>((v) => isString(v) && v.length <= 3);
|
|
76
|
+
const isPositive = and(
|
|
77
|
+
isNumber,
|
|
78
|
+
predicateToRefine<number>((v) => v > 0)
|
|
77
79
|
);
|
|
78
|
-
const isPositive = predicateToRefine<number>((num) => num > 0);
|
|
79
|
-
|
|
80
|
-
// 2. compose them
|
|
81
|
-
const isPositiveNumber = and(isNumber, isPositive);
|
|
82
|
-
const shortOrPositive = or(isShortString, isPositiveNumber);
|
|
83
|
-
const isOther = not(shortOrPositive);
|
|
84
80
|
|
|
85
|
-
//
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
81
|
+
// Combine them (or / not)
|
|
82
|
+
const isShortOrPositive = or(isShortString, isPositive);
|
|
83
|
+
const isOther = not(isShortOrPositive);
|
|
84
|
+
|
|
85
|
+
// Define object shapes
|
|
86
|
+
const isRole = oneOfValues(['admin', 'member'] as const);
|
|
87
|
+
const isUser = struct({
|
|
88
|
+
id: isPositive, // number > 0
|
|
89
|
+
name: isString, // string
|
|
90
|
+
role: isRole, // 'admin' | 'member'
|
|
91
|
+
nickname: optional(isShortString), // string <= 3 | undefined
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
// Use them
|
|
95
|
+
isPositive(3); // true
|
|
96
|
+
isShortOrPositive('foo'); // true
|
|
97
|
+
isShortOrPositive(false); // false
|
|
98
|
+
isOther('x'); // true
|
|
99
|
+
|
|
100
|
+
const maybeUser: unknown = { id: 7, name: 'Rin', role: 'admin' };
|
|
101
|
+
if (isUser(maybeUser)) {
|
|
102
|
+
// The type is narrowed inside this block
|
|
103
|
+
maybeUser.role; // 'admin' | 'member'
|
|
104
|
+
maybeUser.nickname?.toUpperCase();
|
|
105
|
+
}
|
|
89
106
|
```
|
|
90
107
|
|
|
91
|
-
|
|
108
|
+
Composed guards stay reusable:
|
|
109
|
+
`isPositive` can be used standalone or as part of a `struct` definition.
|
|
110
|
+
|
|
111
|
+
When validating complex shapes, reach for `struct` — and friends like `arrayOf`, `recordOf`, or `oneOf`.
|
|
92
112
|
|
|
93
113
|
## Core Ideas
|
|
94
114
|
|
|
@@ -99,7 +119,9 @@ These primitives plug into higher-level combinators like `struct` when you need
|
|
|
99
119
|
|
|
100
120
|
## API Reference
|
|
101
121
|
|
|
102
|
-
|
|
122
|
+
For full API details and runnable examples, visit
|
|
123
|
+
|
|
124
|
+
[📚 See full document](https://is-kit-docs.vercel.app/)
|
|
103
125
|
|
|
104
126
|
## Development
|
|
105
127
|
|