@ptolemy2002/ts-utils 3.0.0 → 3.2.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
@@ -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,21 @@ 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
+
93
+ ### ValueConditionType
94
+ This type is a union of string literals representing the possible types a `ValueCondition<T>` can be. It may also be a list of those same string literals plus `"false"`, representing the types of each element in an array condition. This array may be nested to any depth. To be precise, it is defined as follows:
95
+
96
+ ```typescript
97
+ type ValueConditionType = "advanced" | "function" | "value" | (ValueConditionType | "false")[];
98
+ ```
99
+
100
+ ### SerializableValueConditionType
101
+ This type is the same as `ValueConditionType` except that the `"function"` type is omitted, since functions are not allowed in `SerializableValueCondition<T>`.
102
+
78
103
  ### ValuesIntersection<T>
79
104
  This type returns an intersection of all the possible values in an object of type `T`.
80
105
 
@@ -107,6 +132,15 @@ This function creates an instance of `AdvancedCondition` with the specified argu
107
132
  #### Returns
108
133
  `AdvancedCondition<T>` - The advanced condition instance.
109
134
 
135
+ ### createSerializableAdvancedCondition<T>
136
+ #### Description
137
+ 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.
138
+
139
+ #### Parameters
140
+ - `condition` (`WithoutBrand<Omit<SerializableAdvancedCondition<T>, "__isAdvancedCondition">>`) - The arguments to use in constructing the condition.
141
+ - `include` - The value or values that must be included in the condition. If this is `false`, it will be ignored.
142
+ - `exclude` - The value or values that must be excluded from the condition. If this is `false`, it will be ignored.
143
+
110
144
  ### valueConditionMatches<T>
111
145
  #### Description
112
146
  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.
@@ -118,6 +152,30 @@ This function takes a value of type `T` and a condition of type `OptionalValueCo
118
152
  #### Returns
119
153
  `boolean` - Whether the value meets the condition.
120
154
 
155
+ ### valueConditionType<T>
156
+ #### Description
157
+ This function takes a condition of type `ValueCondition<T>` and returns a string indicating the type of condition it is. The possible return values are:
158
+ - `"advanced"` - The condition is an instance of `AdvancedCondition<T>`.
159
+ - `"function"` - The condition is a function that takes a value of type `T` and (presumably) returns a boolean. The return type is not checked, due to potential side effects of calling the function.
160
+ - `"value"` - The condition is a value of type `T`.
161
+ - `(ValueConditionType | "false")[]` - The condition is an array of conditions, and the return value is an array of the types of each condition in the array.
162
+
163
+ #### Parameters
164
+ - `condition` (`ValueCondition<T>`) - The condition to check.
165
+
166
+ #### Returns
167
+ `ValueConditionType` - The type of the condition.
168
+
169
+ ### serializeValueConditionType<T>
170
+ #### Description
171
+ 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
+
173
+ #### Parameters
174
+ - `condition` (`SerializableValueCondition<T>`) - The condition to check.
175
+
176
+ #### Returns
177
+ `SerializableValueConditionType` - The type of the condition.
178
+
121
179
  ### omit<T, K extends keyof T>
122
180
  #### Description
123
181
  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.
package/dist/index.d.ts CHANGED
@@ -32,11 +32,22 @@ 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;
47
+ export type ValueConditionType = "advanced" | "function" | "value" | (ValueConditionType | "false")[];
48
+ export type SerializableValueConditionType = "advanced" | "value" | (SerializableValueConditionType | "false")[];
49
+ export declare function valueConditionType<T>(condition: ValueCondition<T>): ValueConditionType;
50
+ export declare function serializableValueConditionType<T>(condition: SerializableValueCondition<T>): SerializableValueConditionType;
40
51
  export type Rename<T, K extends keyof T, N extends string> = Pick<T, Exclude<keyof T, K>> & {
41
52
  [P in N]: T[K];
42
53
  };
package/dist/index.js CHANGED
@@ -2,7 +2,10 @@
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;
7
+ exports.valueConditionType = valueConditionType;
8
+ exports.serializableValueConditionType = serializableValueConditionType;
6
9
  exports.omit = omit;
7
10
  const isCallable = require("is-callable");
8
11
  const ts_brand_utils_1 = require("@ptolemy2002/ts-brand-utils");
@@ -20,6 +23,14 @@ function createAdvancedCondition(condition) {
20
23
  ...(0, ts_brand_utils_1.brand)(condition)
21
24
  };
22
25
  }
26
+ function createSerializableAdvancedCondition(condition) {
27
+ return {
28
+ __isAdvancedCondition: true,
29
+ include: [],
30
+ exclude: [],
31
+ ...(0, ts_brand_utils_1.brand)(condition)
32
+ };
33
+ }
23
34
  function valueConditionMatches(value, condition) {
24
35
  if (condition === null)
25
36
  return true;
@@ -46,6 +57,24 @@ function valueConditionMatches(value, condition) {
46
57
  return !excluded(value);
47
58
  return included(value) && !excluded(value);
48
59
  }
60
+ function valueConditionType(condition) {
61
+ // Type assertion here because TS cannot infer that the filter will remove all false values
62
+ if (Array.isArray(condition))
63
+ return condition.map(c => c === false ? "false" : valueConditionType(c));
64
+ if (isAdvancedCondition(condition))
65
+ return "advanced";
66
+ if (isCallable(condition))
67
+ return "function";
68
+ return "value";
69
+ }
70
+ function serializableValueConditionType(condition) {
71
+ // Type assertion here because TS cannot infer that the filter will remove all false values
72
+ if (Array.isArray(condition))
73
+ return condition.map(c => c === false ? "false" : serializableValueConditionType(c));
74
+ if (isAdvancedCondition(condition))
75
+ return "advanced";
76
+ return "value";
77
+ }
49
78
  function omit(obj, ...keys) {
50
79
  const _ = { ...obj };
51
80
  keys.forEach((key) => delete _[key]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptolemy2002/ts-utils",
3
- "version": "3.0.0",
3
+ "version": "3.2.0",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",