@ptolemy2002/ts-utils 2.6.0 → 3.1.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 +30 -36
- package/dist/index.d.ts +8 -8
- package/dist/index.js +14 -6
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -9,6 +9,11 @@ import { functionName } from '@ptolemy2002/ts-utils';
|
|
|
9
9
|
const { functionName } = require('@ptolemy2002/ts-utils');
|
|
10
10
|
```
|
|
11
11
|
|
|
12
|
+
## Type Reference
|
|
13
|
+
```typescript
|
|
14
|
+
import { Branded, WithoutBrand } from "@ptolemy2002/ts-brand-utils";
|
|
15
|
+
```
|
|
16
|
+
|
|
12
17
|
## Types
|
|
13
18
|
The following types are available in the library:
|
|
14
19
|
|
|
@@ -55,9 +60,19 @@ This type allows the user to specify a condition for matching a subset of type `
|
|
|
55
60
|
|
|
56
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.
|
|
57
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
|
+
|
|
58
68
|
### OptionalValueCondition<T>
|
|
59
69
|
This type is the same as `ValueCondition<T>` except that the value can also be `null`, indicating any value is acceptable.
|
|
60
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
|
+
|
|
61
76
|
### AdvancedCondition<T>
|
|
62
77
|
```typescript
|
|
63
78
|
declare const advancedConditionSymbol: unique symbol;
|
|
@@ -70,48 +85,17 @@ type AdvancedCondition<T> = Branded<{
|
|
|
70
85
|
}, [typeof advancedConditionSymbol]>;
|
|
71
86
|
```
|
|
72
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
|
+
|
|
73
93
|
### ValuesIntersection<T>
|
|
74
94
|
This type returns an intersection of all the possible values in an object of type `T`.
|
|
75
95
|
|
|
76
96
|
### Contains<L extends unknown[], T>
|
|
77
97
|
This type returns a boolean indicating whether the type `T` is contained in the array `L`.
|
|
78
98
|
|
|
79
|
-
### BrandTag<B extends unknown[]>
|
|
80
|
-
This type returns a record with a single key of type `__brand` and a value of type `B`. It represents a brand that can be used to differentiate between types.
|
|
81
|
-
|
|
82
|
-
Branded types are useful for making checks at runtime through assertion functions and recognizing these checks at compile time. For example:
|
|
83
|
-
```typescript
|
|
84
|
-
function isPositive(value: number): value is Branded<number, ["positive"]> {
|
|
85
|
-
return value > 0;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
function takesPositive(value: WithBrand<number, "positive">) {
|
|
89
|
-
// Do something with the positive value
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
const value: number = 5;
|
|
93
|
-
if (isPositive(value)) {
|
|
94
|
-
// Succeeds because value has been asserted to be positive
|
|
95
|
-
takesPositive(value);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// Error: value may not be positive, as we cannot guarantee the assertion succeeded
|
|
99
|
-
takesPositive(value);
|
|
100
|
-
```
|
|
101
|
-
|
|
102
|
-
### Branded<T, B extends unknown[]>
|
|
103
|
-
This type returns a type that is the same as `T` except that it is branded with the type `B`.
|
|
104
|
-
|
|
105
|
-
### WithBrand<T, B>
|
|
106
|
-
Using the `Contains` type, this type returns `T` if it is branded with `B` and `never` otherwise.
|
|
107
|
-
|
|
108
|
-
### WithoutBrand<T extends BrandTag<unknown[]>>
|
|
109
|
-
This type returns the inner type without the brand applied to it.
|
|
110
|
-
|
|
111
|
-
## Values
|
|
112
|
-
### declare const __brand
|
|
113
|
-
A unique symbol used to define type brands.
|
|
114
|
-
|
|
115
99
|
## Functions
|
|
116
100
|
The following functions are available in the library:
|
|
117
101
|
|
|
@@ -138,6 +122,15 @@ This function creates an instance of `AdvancedCondition` with the specified argu
|
|
|
138
122
|
#### Returns
|
|
139
123
|
`AdvancedCondition<T>` - The advanced condition instance.
|
|
140
124
|
|
|
125
|
+
### createSerializableAdvancedCondition<T>
|
|
126
|
+
#### Description
|
|
127
|
+
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.
|
|
128
|
+
|
|
129
|
+
#### Parameters
|
|
130
|
+
- `condition` (`WithoutBrand<Omit<SerializableAdvancedCondition<T>, "__isAdvancedCondition">>`) - The arguments to use in constructing the condition.
|
|
131
|
+
- `include` - The value or values that must be included in the condition. If this is `false`, it will be ignored.
|
|
132
|
+
- `exclude` - The value or values that must be excluded from the condition. If this is `false`, it will be ignored.
|
|
133
|
+
|
|
141
134
|
### valueConditionMatches<T>
|
|
142
135
|
#### Description
|
|
143
136
|
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.
|
|
@@ -162,6 +155,7 @@ This function takes an object of type `T` and a list of keys `K` and returns a n
|
|
|
162
155
|
|
|
163
156
|
## Peer Dependencies
|
|
164
157
|
- `is-callable^1.2.7`
|
|
158
|
+
- `@ptolemy2002/ts-brand-utils^1.0.0`
|
|
165
159
|
|
|
166
160
|
## Commands
|
|
167
161
|
The following commands exist in the project:
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Branded, WithoutBrand } from "@ptolemy2002/ts-brand-utils";
|
|
1
2
|
export type ValueOf<T> = T[keyof T];
|
|
2
3
|
export type MaybeTransformer<T, Args extends any[] = []> = T | ((...args: Args) => T);
|
|
3
4
|
export type MaybeTransformerRecord<T, Args extends any[] = []> = {
|
|
@@ -31,10 +32,17 @@ export type AdvancedCondition<T> = Branded<{
|
|
|
31
32
|
exclude?: T | false | ((v: T) => boolean) | (T | false | ((v: T) => boolean | false))[];
|
|
32
33
|
match?: (a: T, b: T) => boolean;
|
|
33
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
|
+
}>;
|
|
34
39
|
export declare function isAdvancedCondition(value: any): value is AdvancedCondition<any>;
|
|
35
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>;
|
|
36
42
|
export type ValueCondition<T> = AdvancedCondition<T> | T | ((v: T) => boolean) | (ValueCondition<T> | false)[];
|
|
37
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;
|
|
38
46
|
export declare function valueConditionMatches<T>(value: T, condition: OptionalValueCondition<T>): boolean;
|
|
39
47
|
export type Rename<T, K extends keyof T, N extends string> = Pick<T, Exclude<keyof T, K>> & {
|
|
40
48
|
[P in N]: T[K];
|
|
@@ -43,12 +51,4 @@ export type ValuesIntersection<T> = ValueOf<{
|
|
|
43
51
|
[K in keyof T]: (x: T[K]) => void;
|
|
44
52
|
}> extends (x: infer I) => void ? I : never;
|
|
45
53
|
export type Contains<L extends unknown[], T> = L extends [...unknown[], T] ? true : L extends [T, ...unknown[]] ? true : L extends [T] ? true : false;
|
|
46
|
-
export declare const __brand: unique symbol;
|
|
47
|
-
export type BrandTag<B extends unknown[]> = {
|
|
48
|
-
readonly [__brand]: B;
|
|
49
|
-
};
|
|
50
|
-
export type Branded<T, B extends unknown[]> = T & BrandTag<B>;
|
|
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>;
|
|
53
|
-
export declare function brandValue<B extends unknown[], T>(value: T): Branded<T, B>;
|
|
54
54
|
export declare function omit<T extends object, K extends keyof T>(obj: T, ...keys: K[]): Omit<T, K>;
|
package/dist/index.js
CHANGED
|
@@ -2,12 +2,15 @@
|
|
|
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;
|
|
6
|
-
exports.brandValue = brandValue;
|
|
7
7
|
exports.omit = omit;
|
|
8
8
|
const isCallable = require("is-callable");
|
|
9
|
+
const ts_brand_utils_1 = require("@ptolemy2002/ts-brand-utils");
|
|
9
10
|
function isAdvancedCondition(value) {
|
|
10
|
-
return
|
|
11
|
+
return (typeof value === "object" &&
|
|
12
|
+
value !== null &&
|
|
13
|
+
"__isAdvancedCondition" in value && value.__isAdvancedCondition === true);
|
|
11
14
|
}
|
|
12
15
|
function createAdvancedCondition(condition) {
|
|
13
16
|
return {
|
|
@@ -15,7 +18,15 @@ function createAdvancedCondition(condition) {
|
|
|
15
18
|
include: [],
|
|
16
19
|
exclude: [],
|
|
17
20
|
match: Object.is,
|
|
18
|
-
...
|
|
21
|
+
...(0, ts_brand_utils_1.brand)(condition)
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
function createSerializableAdvancedCondition(condition) {
|
|
25
|
+
return {
|
|
26
|
+
__isAdvancedCondition: true,
|
|
27
|
+
include: [],
|
|
28
|
+
exclude: [],
|
|
29
|
+
...(0, ts_brand_utils_1.brand)(condition)
|
|
19
30
|
};
|
|
20
31
|
}
|
|
21
32
|
function valueConditionMatches(value, condition) {
|
|
@@ -44,9 +55,6 @@ function valueConditionMatches(value, condition) {
|
|
|
44
55
|
return !excluded(value);
|
|
45
56
|
return included(value) && !excluded(value);
|
|
46
57
|
}
|
|
47
|
-
function brandValue(value) {
|
|
48
|
-
return value;
|
|
49
|
-
}
|
|
50
58
|
function omit(obj, ...keys) {
|
|
51
59
|
const _ = { ...obj };
|
|
52
60
|
keys.forEach((key) => delete _[key]);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ptolemy2002/ts-utils",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -27,10 +27,12 @@
|
|
|
27
27
|
"release-major": "bash ./scripts/release.sh major"
|
|
28
28
|
},
|
|
29
29
|
"peerDependencies": {
|
|
30
|
-
"is-callable": "^1.2.7"
|
|
30
|
+
"is-callable": "^1.2.7",
|
|
31
|
+
"@ptolemy2002/ts-brand-utils": "^1.0.0"
|
|
31
32
|
},
|
|
32
33
|
"devDependencies": {
|
|
33
34
|
"@types/is-callable": "~1.1.2",
|
|
35
|
+
"@ptolemy2002/ts-brand-utils": "^1.0.0",
|
|
34
36
|
"is-callable": "^1.2.7"
|
|
35
37
|
}
|
|
36
38
|
}
|