@ptolemy2002/ts-utils 3.3.0 → 3.5.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 +29 -1
- package/dist/index.d.ts +54 -0
- package/dist/index.js +40 -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.
|
|
@@ -201,9 +221,17 @@ This function takes an object of type `T` and a list of keys `K` and returns a n
|
|
|
201
221
|
#### Returns
|
|
202
222
|
`Omit<T, K>` - The object with the keys omitted.
|
|
203
223
|
|
|
224
|
+
## Classes
|
|
225
|
+
This library exports the following classes:
|
|
226
|
+
|
|
227
|
+
### Collection<T>
|
|
228
|
+
An abstract class representing an object that can be treated as an array, but with some methods overwritten to use a collection of this type rather than a standard array. No methods are implemented here, so it is up to the extending class to do it all. This is just a template for the extending class to ensure that it has all the necessary methods and that they return the correct types.
|
|
229
|
+
|
|
204
230
|
## Peer Dependencies
|
|
205
231
|
- `is-callable^1.2.7`
|
|
206
232
|
- `@ptolemy2002/ts-brand-utils^1.0.0`
|
|
233
|
+
- `@ptolemy2002/regex-utils^4.2.0`
|
|
234
|
+
- `zod^4.3.6`
|
|
207
235
|
|
|
208
236
|
## Commands
|
|
209
237
|
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
|
};
|
|
@@ -58,3 +79,36 @@ export type ValuesIntersection<T> = ValueOf<{
|
|
|
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>;
|
|
60
81
|
export type ArrayWithOptional<AR extends unknown[], AO extends unknown[]> = AR | [...AR, ...AO];
|
|
82
|
+
export declare abstract class Collection<T> {
|
|
83
|
+
abstract length(): number;
|
|
84
|
+
abstract at(index: number): T | undefined;
|
|
85
|
+
abstract find(predicate: (item: T, index: number, array: Collection<T>) => boolean): T | undefined;
|
|
86
|
+
abstract findIndex(predicate: (item: T, index: number, array: Collection<T>) => boolean): number;
|
|
87
|
+
abstract indexOf(item: T, fromIndex?: number): number;
|
|
88
|
+
abstract includes(item: T, fromIndex?: number): boolean;
|
|
89
|
+
abstract some(predicate: (item: T, index: number, array: Collection<T>) => boolean): boolean;
|
|
90
|
+
abstract every(predicate: (item: T, index: number, array: Collection<T>) => boolean): boolean;
|
|
91
|
+
abstract forEach(callback: (item: T, index: number, array: Collection<T>) => void): void;
|
|
92
|
+
abstract map<U>(callback: (item: T, index: number, array: Collection<T>) => U): U[];
|
|
93
|
+
abstract filter(predicate: (item: T, index: number, array: Collection<T>) => boolean): Collection<T>;
|
|
94
|
+
abstract flat(depth?: number): Collection<T>;
|
|
95
|
+
abstract flatMap<U>(callback: (item: T, index: number, array: Collection<T>) => U | U[], depth?: number): U[];
|
|
96
|
+
abstract slice(start?: number, end?: number): Collection<T>;
|
|
97
|
+
abstract concat(...others: (T | T[] | Collection<T>)[]): Collection<T>;
|
|
98
|
+
abstract push(...items: T[]): void;
|
|
99
|
+
abstract pop(): T | undefined;
|
|
100
|
+
abstract shift(): T | undefined;
|
|
101
|
+
abstract unshift(...items: T[]): number;
|
|
102
|
+
abstract splice(start: number, deleteCount?: number, ...items: T[]): Collection<T>;
|
|
103
|
+
abstract reverse(): Collection<T>;
|
|
104
|
+
abstract sort(compareFn?: (a: T, b: T) => number): Collection<T>;
|
|
105
|
+
abstract fill(value: T, start?: number, end?: number): Collection<T>;
|
|
106
|
+
abstract [Symbol.iterator](): Iterator<T>;
|
|
107
|
+
abstract entries(): IterableIterator<[number, T]>;
|
|
108
|
+
abstract keys(): IterableIterator<number>;
|
|
109
|
+
abstract values(): IterableIterator<T>;
|
|
110
|
+
abstract reduce<U>(accumulator: U, callback: (accumulator: U, item: T, index: number, array: Collection<T>) => U): U;
|
|
111
|
+
abstract reduce(callback: (accumulator: T, item: T, index: number, array: Collection<T>) => T): T;
|
|
112
|
+
abstract reduce<U>(callback: (accumulator: U | T, item: T, index: number, array: Collection<T>) => U | T, initialValue?: U): U | T;
|
|
113
|
+
abstract toArray(): T[];
|
|
114
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -1,14 +1,22 @@
|
|
|
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 });
|
|
6
|
+
exports.Collection = void 0;
|
|
3
7
|
exports.isAdvancedCondition = isAdvancedCondition;
|
|
4
8
|
exports.createAdvancedCondition = createAdvancedCondition;
|
|
5
9
|
exports.createSerializableAdvancedCondition = createSerializableAdvancedCondition;
|
|
6
10
|
exports.valueConditionMatches = valueConditionMatches;
|
|
7
11
|
exports.valueConditionType = valueConditionType;
|
|
8
12
|
exports.serializableValueConditionType = serializableValueConditionType;
|
|
13
|
+
exports.zodSerializableAdvancedConditionSchemaTemplate = zodSerializableAdvancedConditionSchemaTemplate;
|
|
14
|
+
exports.zodSerializableValueConditionSchemaTemplate = zodSerializableValueConditionSchemaTemplate;
|
|
9
15
|
exports.omit = omit;
|
|
10
|
-
const
|
|
16
|
+
const is_callable_1 = __importDefault(require("is-callable"));
|
|
11
17
|
const ts_brand_utils_1 = require("@ptolemy2002/ts-brand-utils");
|
|
18
|
+
const regex_utils_1 = require("@ptolemy2002/regex-utils");
|
|
19
|
+
const zod_1 = __importDefault(require("zod"));
|
|
12
20
|
function isAdvancedCondition(value) {
|
|
13
21
|
return (typeof value === "object" &&
|
|
14
22
|
value !== null &&
|
|
@@ -36,7 +44,7 @@ function valueConditionMatches(value, condition) {
|
|
|
36
44
|
return true;
|
|
37
45
|
if (Array.isArray(condition))
|
|
38
46
|
return condition.some(c => c !== false && valueConditionMatches(value, c));
|
|
39
|
-
if (
|
|
47
|
+
if ((0, is_callable_1.default)(condition))
|
|
40
48
|
return condition(value);
|
|
41
49
|
// If the condition value here is not a condition object, it must be of type T, so we can directly compare it
|
|
42
50
|
if (!isAdvancedCondition(condition))
|
|
@@ -47,10 +55,10 @@ function valueConditionMatches(value, condition) {
|
|
|
47
55
|
if (!Array.isArray(exclude))
|
|
48
56
|
exclude = [exclude];
|
|
49
57
|
const included = (v) => {
|
|
50
|
-
return include.some(i => i !== false && (
|
|
58
|
+
return include.some(i => i !== false && ((0, is_callable_1.default)(i) ? i(v) : match(value, i)));
|
|
51
59
|
};
|
|
52
60
|
const excluded = (v) => {
|
|
53
|
-
return exclude.some(e => e !== false && (
|
|
61
|
+
return exclude.some(e => e !== false && ((0, is_callable_1.default)(e) ? e(v) : match(value, e)));
|
|
54
62
|
};
|
|
55
63
|
// If there are no includes, the last condition will unexpectedly fail, so we add a separate case for this
|
|
56
64
|
if (include.length === 0)
|
|
@@ -63,7 +71,7 @@ function valueConditionType(condition) {
|
|
|
63
71
|
return condition.map(c => c === false ? "false" : valueConditionType(c));
|
|
64
72
|
if (isAdvancedCondition(condition))
|
|
65
73
|
return "advanced";
|
|
66
|
-
if (
|
|
74
|
+
if ((0, is_callable_1.default)(condition))
|
|
67
75
|
return "function";
|
|
68
76
|
return "value";
|
|
69
77
|
}
|
|
@@ -75,8 +83,35 @@ function serializableValueConditionType(condition) {
|
|
|
75
83
|
return "advanced";
|
|
76
84
|
return "value";
|
|
77
85
|
}
|
|
86
|
+
const zodValueConditionGenericFactory = (0, regex_utils_1.zodGenericFactory)();
|
|
87
|
+
function zodSerializableAdvancedConditionSchemaTemplate(zt) {
|
|
88
|
+
return zod_1.default.object({
|
|
89
|
+
include: zod_1.default.union([
|
|
90
|
+
zt,
|
|
91
|
+
zod_1.default.literal(false),
|
|
92
|
+
zod_1.default.array(zod_1.default.union([zt, zod_1.default.literal(false)]))
|
|
93
|
+
]),
|
|
94
|
+
exclude: zod_1.default.union([
|
|
95
|
+
zt,
|
|
96
|
+
zod_1.default.literal(false),
|
|
97
|
+
zod_1.default.array(zod_1.default.union([zt, zod_1.default.literal(false)]))
|
|
98
|
+
])
|
|
99
|
+
}).partial().transform((data) => createSerializableAdvancedCondition(data));
|
|
100
|
+
}
|
|
101
|
+
function zodSerializableValueConditionSchemaTemplate(zt) {
|
|
102
|
+
return zodValueConditionGenericFactory(zt)((s) => {
|
|
103
|
+
return zod_1.default.union([
|
|
104
|
+
s,
|
|
105
|
+
zod_1.default.array(zod_1.default.union([s, zod_1.default.literal(false), zodSerializableAdvancedConditionSchemaTemplate(s)])),
|
|
106
|
+
zodSerializableAdvancedConditionSchemaTemplate(s)
|
|
107
|
+
]);
|
|
108
|
+
});
|
|
109
|
+
}
|
|
78
110
|
function omit(obj, ...keys) {
|
|
79
111
|
const _ = { ...obj };
|
|
80
112
|
keys.forEach((key) => delete _[key]);
|
|
81
113
|
return _;
|
|
82
114
|
}
|
|
115
|
+
class Collection {
|
|
116
|
+
}
|
|
117
|
+
exports.Collection = Collection;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ptolemy2002/ts-utils",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.5.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
|
}
|