@ptolemy2002/ts-utils 3.0.0 → 3.1.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 +24 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +9 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -60,9 +60,19 @@ This type allows the user to specify a condition for matching a subset of type `
|
|
|
60
60
|
|
|
61
61
|
The condition can then be passed to the `valueConditionMatches` function along with a value of type `T` to determine if the condition is met for that value.
|
|
62
62
|
|
|
63
|
+
### SerializableValueCondition<T>
|
|
64
|
+
This type is the same as `ValueCondition<T>` except that any function values are disallowed, making it safe for JSON serialization, assuming that `T` is also JSON-serializable.
|
|
65
|
+
|
|
66
|
+
This is assignable to any field that accepts a `ValueCondition<T>`.
|
|
67
|
+
|
|
63
68
|
### OptionalValueCondition<T>
|
|
64
69
|
This type is the same as `ValueCondition<T>` except that the value can also be `null`, indicating any value is acceptable.
|
|
65
70
|
|
|
71
|
+
### OptionalSerializableValueCondition<T>
|
|
72
|
+
This type is the same as `SerializableValueCondition<T>` except that the value can also be `null`, indicating any value is acceptable.
|
|
73
|
+
|
|
74
|
+
This is assignable to any field that accepts an `OptionalValueCondition<T>`.
|
|
75
|
+
|
|
66
76
|
### AdvancedCondition<T>
|
|
67
77
|
```typescript
|
|
68
78
|
declare const advancedConditionSymbol: unique symbol;
|
|
@@ -75,6 +85,11 @@ type AdvancedCondition<T> = Branded<{
|
|
|
75
85
|
}, [typeof advancedConditionSymbol]>;
|
|
76
86
|
```
|
|
77
87
|
|
|
88
|
+
### SerializableAdvancedCondition<T>
|
|
89
|
+
This type is the same as `AdvancedCondition<T>` except that the `include` and `exclude` fields cannot be functions and the `match` field is omit, making it safe for JSON serialization, assuming that `T` is also JSON-serializable. Thus, you lose the ability to use custom matching logic when using this type.
|
|
90
|
+
|
|
91
|
+
This is assignable to any field that accepts an `AdvancedCondition<T>`.
|
|
92
|
+
|
|
78
93
|
### ValuesIntersection<T>
|
|
79
94
|
This type returns an intersection of all the possible values in an object of type `T`.
|
|
80
95
|
|
|
@@ -107,6 +122,15 @@ This function creates an instance of `AdvancedCondition` with the specified argu
|
|
|
107
122
|
#### Returns
|
|
108
123
|
`AdvancedCondition<T>` - The advanced condition instance.
|
|
109
124
|
|
|
125
|
+
### createSerializableAdvancedCondition<T>
|
|
126
|
+
#### Description
|
|
127
|
+
This function is the same as `createAdvancedCondition` except that it takes a `SerializableAdvancedCondition<T>` as its argument. The underlying object created is the same, except that the `match` function is not included.
|
|
128
|
+
|
|
129
|
+
#### Parameters
|
|
130
|
+
- `condition` (`WithoutBrand<Omit<SerializableAdvancedCondition<T>, "__isAdvancedCondition">>`) - The arguments to use in constructing the condition.
|
|
131
|
+
- `include` - The value or values that must be included in the condition. If this is `false`, it will be ignored.
|
|
132
|
+
- `exclude` - The value or values that must be excluded from the condition. If this is `false`, it will be ignored.
|
|
133
|
+
|
|
110
134
|
### valueConditionMatches<T>
|
|
111
135
|
#### Description
|
|
112
136
|
This function takes a value of type `T` and a condition of type `OptionalValueCondition<T>` and returns a boolean indicating whether the value meets the condition. If the condition is `null`, the function will always return `true`. Any instance of the value `false` will be filtered out of lists. If `match` is not specified, it will default to `Object.is`. If `include` is not specified or empty, it will be ignored and `exclude` will act as the only constraint. If `exclude` is not specified, it will be assumed to be empty.
|
package/dist/index.d.ts
CHANGED
|
@@ -32,10 +32,17 @@ export type AdvancedCondition<T> = Branded<{
|
|
|
32
32
|
exclude?: T | false | ((v: T) => boolean) | (T | false | ((v: T) => boolean | false))[];
|
|
33
33
|
match?: (a: T, b: T) => boolean;
|
|
34
34
|
}, [typeof advancedConditionSymbol]>;
|
|
35
|
+
export type SerializableAdvancedCondition<T> = Override<Omit<AdvancedCondition<T>, "match">, {
|
|
36
|
+
include?: Exclude<AdvancedCondition<T>["include"], () => any>;
|
|
37
|
+
exclude?: Exclude<AdvancedCondition<T>["exclude"], () => any>;
|
|
38
|
+
}>;
|
|
35
39
|
export declare function isAdvancedCondition(value: any): value is AdvancedCondition<any>;
|
|
36
40
|
export declare function createAdvancedCondition<T>(condition: WithoutBrand<Omit<AdvancedCondition<T>, "__isAdvancedCondition">>): AdvancedCondition<T>;
|
|
41
|
+
export declare function createSerializableAdvancedCondition<T>(condition: WithoutBrand<Omit<SerializableAdvancedCondition<T>, "__isAdvancedCondition">>): SerializableAdvancedCondition<T>;
|
|
37
42
|
export type ValueCondition<T> = AdvancedCondition<T> | T | ((v: T) => boolean) | (ValueCondition<T> | false)[];
|
|
38
43
|
export type OptionalValueCondition<T> = ValueCondition<T> | null;
|
|
44
|
+
export type SerializableValueCondition<T> = SerializableAdvancedCondition<T> | T | (SerializableValueCondition<T> | false)[];
|
|
45
|
+
export type OptionalSerializableValueCondition<T> = SerializableValueCondition<T> | null;
|
|
39
46
|
export declare function valueConditionMatches<T>(value: T, condition: OptionalValueCondition<T>): boolean;
|
|
40
47
|
export type Rename<T, K extends keyof T, N extends string> = Pick<T, Exclude<keyof T, K>> & {
|
|
41
48
|
[P in N]: T[K];
|
package/dist/index.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.isAdvancedCondition = isAdvancedCondition;
|
|
4
4
|
exports.createAdvancedCondition = createAdvancedCondition;
|
|
5
|
+
exports.createSerializableAdvancedCondition = createSerializableAdvancedCondition;
|
|
5
6
|
exports.valueConditionMatches = valueConditionMatches;
|
|
6
7
|
exports.omit = omit;
|
|
7
8
|
const isCallable = require("is-callable");
|
|
@@ -20,6 +21,14 @@ function createAdvancedCondition(condition) {
|
|
|
20
21
|
...(0, ts_brand_utils_1.brand)(condition)
|
|
21
22
|
};
|
|
22
23
|
}
|
|
24
|
+
function createSerializableAdvancedCondition(condition) {
|
|
25
|
+
return {
|
|
26
|
+
__isAdvancedCondition: true,
|
|
27
|
+
include: [],
|
|
28
|
+
exclude: [],
|
|
29
|
+
...(0, ts_brand_utils_1.brand)(condition)
|
|
30
|
+
};
|
|
31
|
+
}
|
|
23
32
|
function valueConditionMatches(value, condition) {
|
|
24
33
|
if (condition === null)
|
|
25
34
|
return true;
|