@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 +34 -18
- package/dist/index.d.ts +7 -10
- package/dist/index.js +14 -12
- package/package.json +1 -1
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
|
-
###
|
|
61
|
+
### AdvancedCondition<T>
|
|
62
62
|
```typescript
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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
|
-
##
|
|
110
|
-
The following
|
|
115
|
+
## Functions
|
|
116
|
+
The following functions are available in the library:
|
|
111
117
|
|
|
112
|
-
###
|
|
118
|
+
### isAdvancedCondition
|
|
113
119
|
#### Description
|
|
114
|
-
This
|
|
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
|
-
####
|
|
117
|
-
- `
|
|
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
|
-
|
|
123
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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
|
|
33
|
-
|
|
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.
|
|
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
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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 (!
|
|
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))
|