@ptolemy2002/ts-utils 3.3.0 → 3.4.0
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 +23 -1
- package/dist/index.d.ts +21 -0
- package/dist/index.js +36 -5
- package/package.json +8 -4
package/README.md
CHANGED
|
@@ -180,7 +180,7 @@ This function takes a condition of type `ValueCondition<T>` and returns a string
|
|
|
180
180
|
#### Returns
|
|
181
181
|
`ValueConditionType` - The type of the condition.
|
|
182
182
|
|
|
183
|
-
###
|
|
183
|
+
### serializableValueConditionType<T>
|
|
184
184
|
#### Description
|
|
185
185
|
The same as `valueConditionType`, except that it specifically takes a `SerializableValueCondition<T>` and returns a `SerializableValueConditionType`. The difference is that function conditions are not serializable, so they are not included in the return type and cannot be specified in the argument.
|
|
186
186
|
|
|
@@ -190,6 +190,26 @@ The same as `valueConditionType`, except that it specifically takes a `Serializa
|
|
|
190
190
|
#### Returns
|
|
191
191
|
`SerializableValueConditionType` - The type of the condition.
|
|
192
192
|
|
|
193
|
+
### zodSerializableAdvancedConditionSchemaTemplate<ZT extends ZodType>
|
|
194
|
+
#### Description
|
|
195
|
+
A function that allows you to pass in a Zod schema and wrap it such that it can be used to validate a `SerializableAdvancedCondition<T>` where `T` is the type represented by the Zod schema. Note: not the full extent of recursion in `SerializableAdvancedCondition<T>` is supported, nor are functions.
|
|
196
|
+
|
|
197
|
+
#### Parameters
|
|
198
|
+
- `zt` (`ZT`) - The Zod schema representing the type `T`.
|
|
199
|
+
|
|
200
|
+
#### Returns
|
|
201
|
+
`ZodObject<...>` - A Zod schema that can be used to validate a `SerializableAdvancedCondition<T>`.
|
|
202
|
+
|
|
203
|
+
### zodSerializableValueConditionSchemaTemplate<ZT extends ZodType>
|
|
204
|
+
#### Description
|
|
205
|
+
A function that allows you to pass in a Zod schema and wrap it such that it can be used to validate a `SerializableValueCondition<T>` where `T` is the type represented by the Zod schema. Note: not the full extent of recursion in `SerializableValueCondition<T>` is supported, nor are functions.
|
|
206
|
+
|
|
207
|
+
#### Parameters
|
|
208
|
+
- `zt` (`ZT`) - The Zod schema representing the type `T`.
|
|
209
|
+
|
|
210
|
+
#### Returns
|
|
211
|
+
`ZodUnion<...>` - A Zod schema that can be used to validate a `SerializableValueCondition<T>`.
|
|
212
|
+
|
|
193
213
|
### omit<T, K extends keyof T>
|
|
194
214
|
#### Description
|
|
195
215
|
This function takes an object of type `T` and a list of keys `K` and returns a new object with all the keys in `K` removed.
|
|
@@ -204,6 +224,8 @@ This function takes an object of type `T` and a list of keys `K` and returns a n
|
|
|
204
224
|
## Peer Dependencies
|
|
205
225
|
- `is-callable^1.2.7`
|
|
206
226
|
- `@ptolemy2002/ts-brand-utils^1.0.0`
|
|
227
|
+
- `@ptolemy2002/regex-utils^4.2.0`
|
|
228
|
+
- `zod^4.3.6`
|
|
207
229
|
|
|
208
230
|
## Commands
|
|
209
231
|
The following commands exist in the project:
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Branded, WithoutBrand } from "@ptolemy2002/ts-brand-utils";
|
|
2
|
+
import z, { ZodType } from "zod";
|
|
2
3
|
export type ValueOf<T> = T[keyof T];
|
|
3
4
|
export type MaybeTransformer<T, Args extends any[] = []> = T | ((...args: Args) => T);
|
|
4
5
|
export type MaybeTransformerRecord<T, Args extends any[] = []> = {
|
|
@@ -49,6 +50,26 @@ export type ValueConditionType = "advanced" | "function" | "value" | (ValueCondi
|
|
|
49
50
|
export type SerializableValueConditionType = "advanced" | "value" | (SerializableValueConditionType | "false")[];
|
|
50
51
|
export declare function valueConditionType<T>(condition: ValueCondition<T>): ValueConditionType;
|
|
51
52
|
export declare function serializableValueConditionType<T>(condition: SerializableValueCondition<T>): SerializableValueConditionType;
|
|
53
|
+
export declare function zodSerializableAdvancedConditionSchemaTemplate<ZT extends ZodType>(zt: ZT): z.ZodPipe<z.ZodObject<{
|
|
54
|
+
include: z.ZodOptional<z.ZodUnion<readonly [ZT, z.ZodLiteral<false>, z.ZodArray<z.ZodUnion<readonly [ZT, z.ZodLiteral<false>]>>]>>;
|
|
55
|
+
exclude: z.ZodOptional<z.ZodUnion<readonly [ZT, z.ZodLiteral<false>, z.ZodArray<z.ZodUnion<readonly [ZT, z.ZodLiteral<false>]>>]>>;
|
|
56
|
+
}, z.core.$strip>, z.ZodTransform<SerializableAdvancedCondition<z.core.$InferUnionOutput<ZT>>, {
|
|
57
|
+
include?: false | z.core.$InferUnionOutput<ZT> | (false | z.core.$InferUnionOutput<ZT>)[];
|
|
58
|
+
exclude?: false | z.core.$InferUnionOutput<ZT> | (false | z.core.$InferUnionOutput<ZT>)[];
|
|
59
|
+
}>>;
|
|
60
|
+
export declare function zodSerializableValueConditionSchemaTemplate<ZT extends ZodType>(zt: ZT): z.ZodUnion<readonly [ZT, z.ZodArray<z.ZodUnion<readonly [ZT, z.ZodLiteral<false>, z.ZodPipe<z.ZodObject<{
|
|
61
|
+
include: z.ZodOptional<z.ZodUnion<readonly [ZT, z.ZodLiteral<false>, z.ZodArray<z.ZodUnion<readonly [ZT, z.ZodLiteral<false>]>>]>>;
|
|
62
|
+
exclude: z.ZodOptional<z.ZodUnion<readonly [ZT, z.ZodLiteral<false>, z.ZodArray<z.ZodUnion<readonly [ZT, z.ZodLiteral<false>]>>]>>;
|
|
63
|
+
}, z.core.$strip>, z.ZodTransform<SerializableAdvancedCondition<z.core.$InferUnionOutput<ZT>>, {
|
|
64
|
+
include?: false | z.core.$InferUnionOutput<ZT> | (false | z.core.$InferUnionOutput<ZT>)[];
|
|
65
|
+
exclude?: false | z.core.$InferUnionOutput<ZT> | (false | z.core.$InferUnionOutput<ZT>)[];
|
|
66
|
+
}>>]>>, z.ZodPipe<z.ZodObject<{
|
|
67
|
+
include: z.ZodOptional<z.ZodUnion<readonly [ZT, z.ZodLiteral<false>, z.ZodArray<z.ZodUnion<readonly [ZT, z.ZodLiteral<false>]>>]>>;
|
|
68
|
+
exclude: z.ZodOptional<z.ZodUnion<readonly [ZT, z.ZodLiteral<false>, z.ZodArray<z.ZodUnion<readonly [ZT, z.ZodLiteral<false>]>>]>>;
|
|
69
|
+
}, z.core.$strip>, z.ZodTransform<SerializableAdvancedCondition<z.core.$InferUnionOutput<ZT>>, {
|
|
70
|
+
include?: false | z.core.$InferUnionOutput<ZT> | (false | z.core.$InferUnionOutput<ZT>)[];
|
|
71
|
+
exclude?: false | z.core.$InferUnionOutput<ZT> | (false | z.core.$InferUnionOutput<ZT>)[];
|
|
72
|
+
}>>]>;
|
|
52
73
|
export type Rename<T, K extends keyof T, N extends string> = Pick<T, Exclude<keyof T, K>> & {
|
|
53
74
|
[P in N]: T[K];
|
|
54
75
|
};
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.isAdvancedCondition = isAdvancedCondition;
|
|
4
7
|
exports.createAdvancedCondition = createAdvancedCondition;
|
|
@@ -6,9 +9,13 @@ exports.createSerializableAdvancedCondition = createSerializableAdvancedConditio
|
|
|
6
9
|
exports.valueConditionMatches = valueConditionMatches;
|
|
7
10
|
exports.valueConditionType = valueConditionType;
|
|
8
11
|
exports.serializableValueConditionType = serializableValueConditionType;
|
|
12
|
+
exports.zodSerializableAdvancedConditionSchemaTemplate = zodSerializableAdvancedConditionSchemaTemplate;
|
|
13
|
+
exports.zodSerializableValueConditionSchemaTemplate = zodSerializableValueConditionSchemaTemplate;
|
|
9
14
|
exports.omit = omit;
|
|
10
|
-
const
|
|
15
|
+
const is_callable_1 = __importDefault(require("is-callable"));
|
|
11
16
|
const ts_brand_utils_1 = require("@ptolemy2002/ts-brand-utils");
|
|
17
|
+
const regex_utils_1 = require("@ptolemy2002/regex-utils");
|
|
18
|
+
const zod_1 = __importDefault(require("zod"));
|
|
12
19
|
function isAdvancedCondition(value) {
|
|
13
20
|
return (typeof value === "object" &&
|
|
14
21
|
value !== null &&
|
|
@@ -36,7 +43,7 @@ function valueConditionMatches(value, condition) {
|
|
|
36
43
|
return true;
|
|
37
44
|
if (Array.isArray(condition))
|
|
38
45
|
return condition.some(c => c !== false && valueConditionMatches(value, c));
|
|
39
|
-
if (
|
|
46
|
+
if ((0, is_callable_1.default)(condition))
|
|
40
47
|
return condition(value);
|
|
41
48
|
// If the condition value here is not a condition object, it must be of type T, so we can directly compare it
|
|
42
49
|
if (!isAdvancedCondition(condition))
|
|
@@ -47,10 +54,10 @@ function valueConditionMatches(value, condition) {
|
|
|
47
54
|
if (!Array.isArray(exclude))
|
|
48
55
|
exclude = [exclude];
|
|
49
56
|
const included = (v) => {
|
|
50
|
-
return include.some(i => i !== false && (
|
|
57
|
+
return include.some(i => i !== false && ((0, is_callable_1.default)(i) ? i(v) : match(value, i)));
|
|
51
58
|
};
|
|
52
59
|
const excluded = (v) => {
|
|
53
|
-
return exclude.some(e => e !== false && (
|
|
60
|
+
return exclude.some(e => e !== false && ((0, is_callable_1.default)(e) ? e(v) : match(value, e)));
|
|
54
61
|
};
|
|
55
62
|
// If there are no includes, the last condition will unexpectedly fail, so we add a separate case for this
|
|
56
63
|
if (include.length === 0)
|
|
@@ -63,7 +70,7 @@ function valueConditionType(condition) {
|
|
|
63
70
|
return condition.map(c => c === false ? "false" : valueConditionType(c));
|
|
64
71
|
if (isAdvancedCondition(condition))
|
|
65
72
|
return "advanced";
|
|
66
|
-
if (
|
|
73
|
+
if ((0, is_callable_1.default)(condition))
|
|
67
74
|
return "function";
|
|
68
75
|
return "value";
|
|
69
76
|
}
|
|
@@ -75,6 +82,30 @@ function serializableValueConditionType(condition) {
|
|
|
75
82
|
return "advanced";
|
|
76
83
|
return "value";
|
|
77
84
|
}
|
|
85
|
+
const zodValueConditionGenericFactory = (0, regex_utils_1.zodGenericFactory)();
|
|
86
|
+
function zodSerializableAdvancedConditionSchemaTemplate(zt) {
|
|
87
|
+
return zod_1.default.object({
|
|
88
|
+
include: zod_1.default.union([
|
|
89
|
+
zt,
|
|
90
|
+
zod_1.default.literal(false),
|
|
91
|
+
zod_1.default.array(zod_1.default.union([zt, zod_1.default.literal(false)]))
|
|
92
|
+
]),
|
|
93
|
+
exclude: zod_1.default.union([
|
|
94
|
+
zt,
|
|
95
|
+
zod_1.default.literal(false),
|
|
96
|
+
zod_1.default.array(zod_1.default.union([zt, zod_1.default.literal(false)]))
|
|
97
|
+
])
|
|
98
|
+
}).partial().transform((data) => createSerializableAdvancedCondition(data));
|
|
99
|
+
}
|
|
100
|
+
function zodSerializableValueConditionSchemaTemplate(zt) {
|
|
101
|
+
return zodValueConditionGenericFactory(zt)((s) => {
|
|
102
|
+
return zod_1.default.union([
|
|
103
|
+
s,
|
|
104
|
+
zod_1.default.array(zod_1.default.union([s, zod_1.default.literal(false), zodSerializableAdvancedConditionSchemaTemplate(s)])),
|
|
105
|
+
zodSerializableAdvancedConditionSchemaTemplate(s)
|
|
106
|
+
]);
|
|
107
|
+
});
|
|
108
|
+
}
|
|
78
109
|
function omit(obj, ...keys) {
|
|
79
110
|
const _ = { ...obj };
|
|
80
111
|
keys.forEach((key) => delete _[key]);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ptolemy2002/ts-utils",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.4.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -27,12 +27,16 @@
|
|
|
27
27
|
"release-major": "bash ./scripts/release.sh major"
|
|
28
28
|
},
|
|
29
29
|
"peerDependencies": {
|
|
30
|
+
"@ptolemy2002/regex-utils": "^4.2.0",
|
|
31
|
+
"@ptolemy2002/ts-brand-utils": "^1.0.0",
|
|
30
32
|
"is-callable": "^1.2.7",
|
|
31
|
-
"
|
|
33
|
+
"zod": "^4.3.6"
|
|
32
34
|
},
|
|
33
35
|
"devDependencies": {
|
|
34
|
-
"@
|
|
36
|
+
"@ptolemy2002/regex-utils": "^4.2.0",
|
|
35
37
|
"@ptolemy2002/ts-brand-utils": "^1.0.0",
|
|
36
|
-
"is-callable": "
|
|
38
|
+
"@types/is-callable": "~1.1.2",
|
|
39
|
+
"is-callable": "^1.2.7",
|
|
40
|
+
"zod": "^4.3.6"
|
|
37
41
|
}
|
|
38
42
|
}
|