@ptolemy2002/ts-utils 2.5.0 → 2.6.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
@@ -58,13 +58,16 @@ The condition can then be passed to the `valueConditionMatches` function along w
58
58
  ### OptionalValueCondition<T>
59
59
  This type is the same as `ValueCondition<T>` except that the value can also be `null`, indicating any value is acceptable.
60
60
 
61
- ### AdvancedConditionConstructorArgs<T>
61
+ ### AdvancedCondition<T>
62
62
  ```typescript
63
- type AdvancedConditionConstructorArgs<T> = {
64
- include?: T | ((v: T) => boolean) | (T | ((v: T) => boolean) | false)[],
65
- exclude?: T | ((v: T) => boolean) | (T | ((v: T) => boolean | false))[],
63
+ declare const advancedConditionSymbol: unique symbol;
64
+
65
+ type AdvancedCondition<T> = Branded<{
66
+ __isAdvancedCondition: true,
67
+ include?: T | false | ((v: T) => boolean) | (T | false | ((v: T) => boolean) | false)[],
68
+ exclude?: T | false | ((v: T) => boolean) | (T | false | ((v: T) => boolean | false))[],
66
69
  match?: (a: T, b: T) => boolean
67
- };
70
+ }, [typeof advancedConditionSymbol]>;
68
71
  ```
69
72
 
70
73
  ### ValuesIntersection<T>
@@ -102,25 +105,38 @@ This type returns a type that is the same as `T` except that it is branded with
102
105
  ### WithBrand<T, B>
103
106
  Using the `Contains` type, this type returns `T` if it is branded with `B` and `never` otherwise.
104
107
 
108
+ ### WithoutBrand<T extends BrandTag<unknown[]>>
109
+ This type returns the inner type without the brand applied to it.
110
+
105
111
  ## Values
106
112
  ### declare const __brand
107
113
  A unique symbol used to define type brands.
108
114
 
109
- ## Classes
110
- The following classes are available in the library:
115
+ ## Functions
116
+ The following functions are available in the library:
111
117
 
112
- ### AdvancedCondition<T>
118
+ ### isAdvancedCondition
113
119
  #### Description
114
- This class is used to create an advanced condition for matching a subset of type `T`. Note that a normal object with the same properties as `AdvancedConditionConstructorArgs<T>` cannot be used in place of this class instance, contrary to how Typescript normally handles classes. A hack has been used to disable this behavior.
120
+ This function takes a value of any type and returns a boolean indicating whether the value is an instance of `AdvancedCondition` by checking for the `__isAdvancedCondition` property and ensuring it is `true`.
115
121
 
116
- #### Properties
117
- - `args: AdvancedConditionConstructorArgs<T>` - The arguments used to construct the condition.
118
- - `include?` (`AdvancedConditionConstructorArgs["include"]`) - The value or values that must be included in the condition. If this is a function, it will be used to determine if a value should be included. If this is an instance of `Condition`, it will be used as a subcondition. If this is `false`, it will be ignored.
119
- - `exclude?` (`AdvancedConditionConstructorArgs["exclude"]`) - The value or values that must be excluded from the condition. If this is a function, it will be used to determine if a value should be excluded. If this is an instance of `Condition`, it will be used as a subcondition. If this is `false`, it will be ignored.
120
- - `match?` (`AdvancedConditionConstructorArgs["match"]`) - The function used to determine if two values are equal. If this is not specified, it will default to `Object.is`.
122
+ #### Parameters
123
+ - `value` (`any`) - The value to check.
121
124
 
122
- ## Functions
123
- The following functions are available in the library:
125
+ #### Returns
126
+ `value is AdvancedCondition<any>` - Boolean determining whether the value is an instance of `AdvancedCondition`. It functions as a type guard in Typescript.
127
+
128
+ ### createAdvancedCondition<T>
129
+ #### Description
130
+ This function creates an instance of `AdvancedCondition` with the specified arguments and sensible defaults applied.
131
+
132
+ #### Parameters
133
+ - `condition` (`WithoutBrand<Omit<AdvancedCondition<T>, "__isAdvancedCondition">>`) - The arguments to use in constructing the condition.
134
+ - `include` - The value or values that must be included in the condition. If this is a function, it will be used to determine if a value should be included. If this is `false`, it will be ignored.
135
+ - `exclude` - The value or values that must be excluded from the condition. If this is a function, it will be used to determine if a value should be excluded. If this is `false`, it will be ignored.
136
+ - `match` - The function used to determine if two values are equal. If this is not specified, it will default to `Object.is`.
137
+
138
+ #### Returns
139
+ `AdvancedCondition<T>` - The advanced condition instance.
124
140
 
125
141
  ### valueConditionMatches<T>
126
142
  #### Description
@@ -131,7 +147,7 @@ This function takes a value of type `T` and a condition of type `OptionalValueCo
131
147
  - `condition` (`OptionalValueCondition<T>`) - The condition to check against the value.
132
148
 
133
149
  #### Returns
134
- - `boolean` - Whether the value meets the condition.
150
+ `boolean` - Whether the value meets the condition.
135
151
 
136
152
  ### omit<T, K extends keyof T>
137
153
  #### Description
@@ -142,7 +158,7 @@ This function takes an object of type `T` and a list of keys `K` and returns a n
142
158
  - `keys` (`K[]`) - The keys to omit from the object.
143
159
 
144
160
  #### Returns
145
- - `Omit<T, K>` - The object with the keys omitted.
161
+ `Omit<T, K>` - The object with the keys omitted.
146
162
 
147
163
  ## Peer Dependencies
148
164
  - `is-callable^1.2.7`
package/dist/index.d.ts CHANGED
@@ -24,19 +24,15 @@ export type AtLeastOne<T, U = {
24
24
  [K in keyof T]: Pick<T, K>;
25
25
  }> = Partial<T> & U[keyof U];
26
26
  export type Override<T, U> = Omit<T, keyof U> & U;
27
- export type AdvancedConditionConstructorArgs<T> = {
27
+ export declare const advancedConditionSymbol: unique symbol;
28
+ export type AdvancedCondition<T> = Branded<{
29
+ __isAdvancedCondition: true;
28
30
  include?: T | false | ((v: T) => boolean) | (T | false | ((v: T) => boolean) | false)[];
29
31
  exclude?: T | false | ((v: T) => boolean) | (T | false | ((v: T) => boolean | false))[];
30
32
  match?: (a: T, b: T) => boolean;
31
- };
32
- export declare class AdvancedCondition<T> {
33
- private kind;
34
- include: AdvancedConditionConstructorArgs<T>["include"];
35
- exclude: AdvancedConditionConstructorArgs<T>["exclude"];
36
- match?: AdvancedConditionConstructorArgs<T>["match"];
37
- constructor({ include, exclude, match }?: AdvancedConditionConstructorArgs<T>);
38
- static isCondition<T>(value: any): value is AdvancedCondition<T>;
39
- }
33
+ }, [typeof advancedConditionSymbol]>;
34
+ export declare function isAdvancedCondition(value: any): value is AdvancedCondition<any>;
35
+ export declare function createAdvancedCondition<T>(condition: WithoutBrand<Omit<AdvancedCondition<T>, "__isAdvancedCondition">>): AdvancedCondition<T>;
40
36
  export type ValueCondition<T> = AdvancedCondition<T> | T | ((v: T) => boolean) | (ValueCondition<T> | false)[];
41
37
  export type OptionalValueCondition<T> = ValueCondition<T> | null;
42
38
  export declare function valueConditionMatches<T>(value: T, condition: OptionalValueCondition<T>): boolean;
@@ -53,5 +49,6 @@ export type BrandTag<B extends unknown[]> = {
53
49
  };
54
50
  export type Branded<T, B extends unknown[]> = T & BrandTag<B>;
55
51
  export type WithBrand<T, B> = T extends BrandTag<unknown[]> ? (Contains<T[typeof __brand], B> extends true ? T : never) : never;
52
+ export type WithoutBrand<T extends BrandTag<unknown[]>> = Omit<T, typeof __brand>;
56
53
  export declare function brandValue<B extends unknown[], T>(value: T): Branded<T, B>;
57
54
  export declare function omit<T extends object, K extends keyof T>(obj: T, ...keys: K[]): Omit<T, K>;
package/dist/index.js CHANGED
@@ -1,21 +1,23 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AdvancedCondition = void 0;
3
+ exports.isAdvancedCondition = isAdvancedCondition;
4
+ exports.createAdvancedCondition = createAdvancedCondition;
4
5
  exports.valueConditionMatches = valueConditionMatches;
5
6
  exports.brandValue = brandValue;
6
7
  exports.omit = omit;
7
8
  const isCallable = require("is-callable");
8
- class AdvancedCondition {
9
- constructor({ include = [], exclude = [], match = Object.is } = {}) {
10
- this.include = include;
11
- this.exclude = exclude;
12
- this.match = match;
13
- }
14
- static isCondition(value) {
15
- return value instanceof AdvancedCondition;
16
- }
9
+ function isAdvancedCondition(value) {
10
+ return "__isAdvancedCondition" in value && value.__isAdvancedCondition === true;
11
+ }
12
+ function createAdvancedCondition(condition) {
13
+ return {
14
+ __isAdvancedCondition: true,
15
+ include: [],
16
+ exclude: [],
17
+ match: Object.is,
18
+ ...brandValue(condition)
19
+ };
17
20
  }
18
- exports.AdvancedCondition = AdvancedCondition;
19
21
  function valueConditionMatches(value, condition) {
20
22
  if (condition === null)
21
23
  return true;
@@ -24,7 +26,7 @@ function valueConditionMatches(value, condition) {
24
26
  if (isCallable(condition))
25
27
  return condition(value);
26
28
  // If the condition value here is not a condition object, it must be of type T, so we can directly compare it
27
- if (!AdvancedCondition.isCondition(condition))
29
+ if (!isAdvancedCondition(condition))
28
30
  return Object.is(value, condition);
29
31
  let { include = [], exclude = [], match = Object.is } = condition;
30
32
  if (!Array.isArray(include))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptolemy2002/ts-utils",
3
- "version": "2.5.0",
3
+ "version": "2.6.0",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",