@ptolemy2002/ts-utils 2.0.0 → 2.0.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 +23 -1
- package/dist/index.d.ts +14 -7
- package/dist/index.js +13 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -36,7 +36,7 @@ This type returns a type that is the same as `T` except that the keys in `K` are
|
|
|
36
36
|
### ValueCondition<T>
|
|
37
37
|
This type allows the user to specify a condition for matching a subset of type `T`. It can be one of the following:
|
|
38
38
|
- A value of type `T`
|
|
39
|
-
- An
|
|
39
|
+
- An instance of `AdvancedCondition<T>`
|
|
40
40
|
- An array of objects of type `T | ValueCondition<T> | false`.
|
|
41
41
|
- A function that takes a value of type `T` and returns a boolean.
|
|
42
42
|
|
|
@@ -45,6 +45,28 @@ The condition can then be passed to the `valueConditionMatches` function along w
|
|
|
45
45
|
### OptionalValueCondition<T>
|
|
46
46
|
This type is the same as `ValueCondition<T>` except that the value can also be `null`, indicating any value is acceptable.
|
|
47
47
|
|
|
48
|
+
### AdvancedConditionConstructorArgs<T>
|
|
49
|
+
```typescript
|
|
50
|
+
type AdvancedConditionConstructorArgs<T> = {
|
|
51
|
+
include?: T | ((v: T) => boolean) | (T | ((v: T) => boolean) | false)[],
|
|
52
|
+
exclude?: T | ((v: T) => boolean) | (T | ((v: T) => boolean | false))[],
|
|
53
|
+
match?: (a: T, b: T) => boolean
|
|
54
|
+
};
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Classes
|
|
58
|
+
The following classes are available in the library:
|
|
59
|
+
|
|
60
|
+
### AdvancedCondition<T>
|
|
61
|
+
#### Description
|
|
62
|
+
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.
|
|
63
|
+
|
|
64
|
+
#### Properties
|
|
65
|
+
- `args: AdvancedConditionConstructorArgs<T>` - The arguments used to construct the condition.
|
|
66
|
+
- `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.
|
|
67
|
+
- `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.
|
|
68
|
+
- `match?` (`AdvancedConditionConstructorArgs["match"]`) - The function used to determine if two values are equal. If this is not specified, it will default to `Object.is`.
|
|
69
|
+
|
|
48
70
|
## Functions
|
|
49
71
|
The following functions are available in the library:
|
|
50
72
|
|
package/dist/index.d.ts
CHANGED
|
@@ -22,12 +22,19 @@ export type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
|
22
22
|
export type AtLeastOne<T, U = {
|
|
23
23
|
[K in keyof T]: Pick<T, K>;
|
|
24
24
|
}> = Partial<T> & U[keyof U];
|
|
25
|
-
export type
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
25
|
+
export type AdvancedConditionConstructorArgs<T> = {
|
|
26
|
+
include?: T | false | ((v: T) => boolean) | (T | false | ((v: T) => boolean) | false)[];
|
|
27
|
+
exclude?: T | false | ((v: T) => boolean) | (T | false | ((v: T) => boolean | false))[];
|
|
28
|
+
match?: (a: T, b: T) => boolean;
|
|
29
|
+
};
|
|
30
|
+
export declare class AdvancedCondition<T> {
|
|
31
|
+
private kind;
|
|
32
|
+
include: AdvancedConditionConstructorArgs<T>["include"];
|
|
33
|
+
exclude: AdvancedConditionConstructorArgs<T>["exclude"];
|
|
34
|
+
match?: AdvancedConditionConstructorArgs<T>["match"];
|
|
35
|
+
constructor({ include, exclude, match }?: AdvancedConditionConstructorArgs<T>);
|
|
36
|
+
static isCondition<T>(value: any): value is AdvancedCondition<T>;
|
|
37
|
+
}
|
|
38
|
+
export type ValueCondition<T> = AdvancedCondition<T> | T | ((v: T) => boolean) | (ValueCondition<T> | false)[];
|
|
32
39
|
export type OptionalValueCondition<T> = ValueCondition<T> | null;
|
|
33
40
|
export declare function valueConditionMatches<T>(value: T, condition: OptionalValueCondition<T>): boolean;
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AdvancedCondition = void 0;
|
|
3
4
|
exports.valueConditionMatches = valueConditionMatches;
|
|
4
5
|
const isCallable = require("is-callable");
|
|
6
|
+
class AdvancedCondition {
|
|
7
|
+
constructor({ include = [], exclude = [], match = Object.is } = {}) {
|
|
8
|
+
this.include = include;
|
|
9
|
+
this.exclude = exclude;
|
|
10
|
+
this.match = match;
|
|
11
|
+
}
|
|
12
|
+
static isCondition(value) {
|
|
13
|
+
return value instanceof AdvancedCondition;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.AdvancedCondition = AdvancedCondition;
|
|
5
17
|
function valueConditionMatches(value, condition) {
|
|
6
18
|
if (condition === null)
|
|
7
19
|
return true;
|
|
@@ -10,7 +22,7 @@ function valueConditionMatches(value, condition) {
|
|
|
10
22
|
if (isCallable(condition))
|
|
11
23
|
return condition(value);
|
|
12
24
|
// If the condition value here is not a condition object, it must be of type T, so we can directly compare it
|
|
13
|
-
if (condition
|
|
25
|
+
if (!AdvancedCondition.isCondition(condition))
|
|
14
26
|
return Object.is(value, condition);
|
|
15
27
|
let { include = [], exclude = [], match = Object.is } = condition;
|
|
16
28
|
if (!Array.isArray(include))
|