is-kit 1.0.1 → 1.0.3
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 +42 -19
- package/dist/index.js +13 -10
- package/dist/index.mjs +13 -10
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
Lightweight, zero-dependency toolkit for building `isFoo` style type guards in TypeScript.
|
|
17
17
|
Runtime-safe 🛡️, composable 🧩, and ergonomic ✨.
|
|
18
18
|
|
|
19
|
-
[
|
|
19
|
+
[📚 full documentation](https://is-kit-docs.vercel.app/)
|
|
20
20
|
|
|
21
21
|
## Why?
|
|
22
22
|
|
|
@@ -60,32 +60,55 @@ Build `is` functions from tiny, composable pieces:
|
|
|
60
60
|
```ts
|
|
61
61
|
import {
|
|
62
62
|
define,
|
|
63
|
-
predicateToRefine,
|
|
64
63
|
and,
|
|
65
64
|
or,
|
|
66
65
|
not,
|
|
67
|
-
|
|
66
|
+
struct,
|
|
67
|
+
oneOfValues,
|
|
68
|
+
optional,
|
|
68
69
|
isNumber,
|
|
70
|
+
isString,
|
|
71
|
+
predicateToRefine,
|
|
69
72
|
} from 'is-kit';
|
|
70
73
|
|
|
71
|
-
//
|
|
72
|
-
const isShortString = define<string>(
|
|
73
|
-
|
|
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)
|
|
74
79
|
);
|
|
75
|
-
const isPositive = predicateToRefine<number>((num) => num > 0);
|
|
76
|
-
|
|
77
|
-
// 2. compose them
|
|
78
|
-
const isPositiveNumber = and(isNumber, isPositive);
|
|
79
|
-
const shortOrPositive = or(isShortString, isPositiveNumber);
|
|
80
|
-
const isOther = not(shortOrPositive);
|
|
81
80
|
|
|
82
|
-
//
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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
|
+
}
|
|
86
106
|
```
|
|
87
107
|
|
|
88
|
-
|
|
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`.
|
|
89
112
|
|
|
90
113
|
## Core Ideas
|
|
91
114
|
|
|
@@ -96,9 +119,9 @@ These primitives plug into higher-level combinators like `struct` when you need
|
|
|
96
119
|
|
|
97
120
|
## API Reference
|
|
98
121
|
|
|
99
|
-
|
|
122
|
+
For full API details and runnable examples, visit
|
|
100
123
|
|
|
101
|
-
|
|
124
|
+
[📚 See full document](https://is-kit-docs.vercel.app/)
|
|
102
125
|
|
|
103
126
|
## Development
|
|
104
127
|
|
package/dist/index.js
CHANGED
|
@@ -144,7 +144,7 @@ var isObject = define(
|
|
|
144
144
|
var isPlainObject = define((value) => {
|
|
145
145
|
if (!isObject(value)) return false;
|
|
146
146
|
const proto = Object.getPrototypeOf(value);
|
|
147
|
-
return proto ===
|
|
147
|
+
return proto === null || Object.getPrototypeOf(proto) === null;
|
|
148
148
|
});
|
|
149
149
|
var isArray = define(Array.isArray);
|
|
150
150
|
var isDate = define(
|
|
@@ -159,15 +159,18 @@ var isMap = define(
|
|
|
159
159
|
var isSet = define(
|
|
160
160
|
(value) => getTag(value) === "[object Set]"
|
|
161
161
|
);
|
|
162
|
-
var isPromiseLike = define(
|
|
163
|
-
(value)
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
);
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
)
|
|
162
|
+
var isPromiseLike = define((value) => {
|
|
163
|
+
if (!isObject(value) && !isFunction(value)) return false;
|
|
164
|
+
return typeof value.then === "function";
|
|
165
|
+
});
|
|
166
|
+
var isIterable = define((value) => {
|
|
167
|
+
if (!isObject(value) && !isFunction(value)) return false;
|
|
168
|
+
return typeof value[Symbol.iterator] === "function";
|
|
169
|
+
});
|
|
170
|
+
var isAsyncIterable = define((value) => {
|
|
171
|
+
if (!isObject(value) && !isFunction(value)) return false;
|
|
172
|
+
return typeof value[Symbol.asyncIterator] === "function";
|
|
173
|
+
});
|
|
171
174
|
var isArrayBuffer = define(
|
|
172
175
|
(value) => getTag(value) === "[object ArrayBuffer]"
|
|
173
176
|
);
|
package/dist/index.mjs
CHANGED
|
@@ -69,7 +69,7 @@ var isObject = define(
|
|
|
69
69
|
var isPlainObject = define((value) => {
|
|
70
70
|
if (!isObject(value)) return false;
|
|
71
71
|
const proto = Object.getPrototypeOf(value);
|
|
72
|
-
return proto ===
|
|
72
|
+
return proto === null || Object.getPrototypeOf(proto) === null;
|
|
73
73
|
});
|
|
74
74
|
var isArray = define(Array.isArray);
|
|
75
75
|
var isDate = define(
|
|
@@ -84,15 +84,18 @@ var isMap = define(
|
|
|
84
84
|
var isSet = define(
|
|
85
85
|
(value) => getTag(value) === "[object Set]"
|
|
86
86
|
);
|
|
87
|
-
var isPromiseLike = define(
|
|
88
|
-
(value)
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
);
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
)
|
|
87
|
+
var isPromiseLike = define((value) => {
|
|
88
|
+
if (!isObject(value) && !isFunction(value)) return false;
|
|
89
|
+
return typeof value.then === "function";
|
|
90
|
+
});
|
|
91
|
+
var isIterable = define((value) => {
|
|
92
|
+
if (!isObject(value) && !isFunction(value)) return false;
|
|
93
|
+
return typeof value[Symbol.iterator] === "function";
|
|
94
|
+
});
|
|
95
|
+
var isAsyncIterable = define((value) => {
|
|
96
|
+
if (!isObject(value) && !isFunction(value)) return false;
|
|
97
|
+
return typeof value[Symbol.asyncIterator] === "function";
|
|
98
|
+
});
|
|
96
99
|
var isArrayBuffer = define(
|
|
97
100
|
(value) => getTag(value) === "[object ArrayBuffer]"
|
|
98
101
|
);
|