@ptolemy2002/ts-utils 3.2.1 → 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 CHANGED
@@ -106,6 +106,20 @@ This type returns an intersection of all the possible values in an object of typ
106
106
  ### Contains<L extends unknown[], T>
107
107
  This type returns a boolean indicating whether the type `T` is contained in the array `L`.
108
108
 
109
+ ### ArrayWithOptional<AR extends unknown[], AO extends unknown[]>
110
+ This type returns a union of the array `AR` and an array that consists of all the elements of `AR` followed by all the elements of `AO`. You can chain it for multiple optional segments, like so:
111
+ ```typescript
112
+ type MyArray = ArrayWithOptional<[number, string], ArrayWithOptional<[boolean], [Date]>>;
113
+ ```
114
+
115
+ The resulting type `MyArray` would be equivalent to:
116
+ ```typescript
117
+ type MyArray =
118
+ | [number, string]
119
+ | [number, string, boolean]
120
+ | [number, string, boolean, Date];
121
+ ```
122
+
109
123
  ## Functions
110
124
  The following functions are available in the library:
111
125
 
@@ -166,7 +180,7 @@ This function takes a condition of type `ValueCondition<T>` and returns a string
166
180
  #### Returns
167
181
  `ValueConditionType` - The type of the condition.
168
182
 
169
- ### serializeValueConditionType<T>
183
+ ### serializableValueConditionType<T>
170
184
  #### Description
171
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.
172
186
 
@@ -176,6 +190,26 @@ The same as `valueConditionType`, except that it specifically takes a `Serializa
176
190
  #### Returns
177
191
  `SerializableValueConditionType` - The type of the condition.
178
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
+
179
213
  ### omit<T, K extends keyof T>
180
214
  #### Description
181
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.
@@ -190,6 +224,8 @@ This function takes an object of type `T` and a list of keys `K` and returns a n
190
224
  ## Peer Dependencies
191
225
  - `is-callable^1.2.7`
192
226
  - `@ptolemy2002/ts-brand-utils^1.0.0`
227
+ - `@ptolemy2002/regex-utils^4.2.0`
228
+ - `zod^4.3.6`
193
229
 
194
230
  ## Commands
195
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
  };
@@ -57,3 +78,4 @@ export type ValuesIntersection<T> = ValueOf<{
57
78
  }> extends (x: infer I) => void ? I : never;
58
79
  export type Contains<L extends unknown[], T> = L extends [...unknown[], T] ? true : L extends [T, ...unknown[]] ? true : L extends [T] ? true : false;
59
80
  export declare function omit<T extends object, K extends keyof T>(obj: T, ...keys: K[]): Omit<T, K>;
81
+ export type ArrayWithOptional<AR extends unknown[], AO extends unknown[]> = AR | [...AR, ...AO];
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 isCallable = require("is-callable");
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 (isCallable(condition))
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 && (isCallable(i) ? i(v) : match(value, i)));
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 && (isCallable(e) ? e(v) : match(value, e)));
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 (isCallable(condition))
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.2.1",
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
- "@ptolemy2002/ts-brand-utils": "^1.0.0"
33
+ "zod": "^4.3.6"
32
34
  },
33
35
  "devDependencies": {
34
- "@types/is-callable": "~1.1.2",
36
+ "@ptolemy2002/regex-utils": "^4.2.0",
35
37
  "@ptolemy2002/ts-brand-utils": "^1.0.0",
36
- "is-callable": "^1.2.7"
38
+ "@types/is-callable": "~1.1.2",
39
+ "is-callable": "^1.2.7",
40
+ "zod": "^4.3.6"
37
41
  }
38
42
  }