@ptolemy2002/ts-utils 3.1.0 → 3.2.1

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
@@ -90,6 +90,16 @@ This type is the same as `AdvancedCondition<T>` except that the `include` and `e
90
90
 
91
91
  This is assignable to any field that accepts an `AdvancedCondition<T>`.
92
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
+
93
103
  ### ValuesIntersection<T>
94
104
  This type returns an intersection of all the possible values in an object of type `T`.
95
105
 
@@ -142,6 +152,30 @@ This function takes a value of type `T` and a condition of type `OptionalValueCo
142
152
  #### Returns
143
153
  `boolean` - Whether the value meets the condition.
144
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
+
145
179
  ### omit<T, K extends keyof T>
146
180
  #### Description
147
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
@@ -28,14 +28,15 @@ export type Override<T, U> = Omit<T, keyof U> & U;
28
28
  export declare const advancedConditionSymbol: unique symbol;
29
29
  export type AdvancedCondition<T> = Branded<{
30
30
  __isAdvancedCondition: true;
31
- include?: T | false | ((v: T) => boolean) | (T | false | ((v: T) => boolean) | false)[];
32
- exclude?: T | false | ((v: T) => boolean) | (T | false | ((v: T) => boolean | false))[];
31
+ include?: T | false | ((v: T) => boolean) | (T | false | ((v: T) => boolean))[];
32
+ exclude?: T | false | ((v: T) => boolean) | (T | false | ((v: T) => boolean))[];
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
+ export type SerializableAdvancedCondition<T> = Branded<{
36
+ __isAdvancedCondition: true;
37
+ include?: T | false | (T | false)[];
38
+ exclude?: T | false | (T | false)[];
39
+ }, [typeof advancedConditionSymbol]>;
39
40
  export declare function isAdvancedCondition(value: any): value is AdvancedCondition<any>;
40
41
  export declare function createAdvancedCondition<T>(condition: WithoutBrand<Omit<AdvancedCondition<T>, "__isAdvancedCondition">>): AdvancedCondition<T>;
41
42
  export declare function createSerializableAdvancedCondition<T>(condition: WithoutBrand<Omit<SerializableAdvancedCondition<T>, "__isAdvancedCondition">>): SerializableAdvancedCondition<T>;
@@ -44,6 +45,10 @@ export type OptionalValueCondition<T> = ValueCondition<T> | null;
44
45
  export type SerializableValueCondition<T> = SerializableAdvancedCondition<T> | T | (SerializableValueCondition<T> | false)[];
45
46
  export type OptionalSerializableValueCondition<T> = SerializableValueCondition<T> | null;
46
47
  export declare function valueConditionMatches<T>(value: T, condition: OptionalValueCondition<T>): boolean;
48
+ export type ValueConditionType = "advanced" | "function" | "value" | (ValueConditionType | "false")[];
49
+ export type SerializableValueConditionType = "advanced" | "value" | (SerializableValueConditionType | "false")[];
50
+ export declare function valueConditionType<T>(condition: ValueCondition<T>): ValueConditionType;
51
+ export declare function serializableValueConditionType<T>(condition: SerializableValueCondition<T>): SerializableValueConditionType;
47
52
  export type Rename<T, K extends keyof T, N extends string> = Pick<T, Exclude<keyof T, K>> & {
48
53
  [P in N]: T[K];
49
54
  };
package/dist/index.js CHANGED
@@ -4,6 +4,8 @@ exports.isAdvancedCondition = isAdvancedCondition;
4
4
  exports.createAdvancedCondition = createAdvancedCondition;
5
5
  exports.createSerializableAdvancedCondition = createSerializableAdvancedCondition;
6
6
  exports.valueConditionMatches = valueConditionMatches;
7
+ exports.valueConditionType = valueConditionType;
8
+ exports.serializableValueConditionType = serializableValueConditionType;
7
9
  exports.omit = omit;
8
10
  const isCallable = require("is-callable");
9
11
  const ts_brand_utils_1 = require("@ptolemy2002/ts-brand-utils");
@@ -55,6 +57,24 @@ function valueConditionMatches(value, condition) {
55
57
  return !excluded(value);
56
58
  return included(value) && !excluded(value);
57
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
+ }
58
78
  function omit(obj, ...keys) {
59
79
  const _ = { ...obj };
60
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.1.0",
3
+ "version": "3.2.1",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",