@ptolemy2002/ts-utils 2.5.0 → 3.0.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 -48
- package/dist/index.d.ts +7 -17
- package/dist/index.js +17 -16
- 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
|
|
|
@@ -58,13 +63,16 @@ The condition can then be passed to the `valueConditionMatches` function along w
|
|
|
58
63
|
### OptionalValueCondition<T>
|
|
59
64
|
This type is the same as `ValueCondition<T>` except that the value can also be `null`, indicating any value is acceptable.
|
|
60
65
|
|
|
61
|
-
###
|
|
66
|
+
### AdvancedCondition<T>
|
|
62
67
|
```typescript
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
68
|
+
declare const advancedConditionSymbol: unique symbol;
|
|
69
|
+
|
|
70
|
+
type AdvancedCondition<T> = Branded<{
|
|
71
|
+
__isAdvancedCondition: true,
|
|
72
|
+
include?: T | false | ((v: T) => boolean) | (T | false | ((v: T) => boolean) | false)[],
|
|
73
|
+
exclude?: T | false | ((v: T) => boolean) | (T | false | ((v: T) => boolean | false))[],
|
|
66
74
|
match?: (a: T, b: T) => boolean
|
|
67
|
-
}
|
|
75
|
+
}, [typeof advancedConditionSymbol]>;
|
|
68
76
|
```
|
|
69
77
|
|
|
70
78
|
### ValuesIntersection<T>
|
|
@@ -73,54 +81,31 @@ This type returns an intersection of all the possible values in an object of typ
|
|
|
73
81
|
### Contains<L extends unknown[], T>
|
|
74
82
|
This type returns a boolean indicating whether the type `T` is contained in the array `L`.
|
|
75
83
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
Branded types are useful for making checks at runtime through assertion functions and recognizing these checks at compile time. For example:
|
|
80
|
-
```typescript
|
|
81
|
-
function isPositive(value: number): value is Branded<number, ["positive"]> {
|
|
82
|
-
return value > 0;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
function takesPositive(value: WithBrand<number, "positive">) {
|
|
86
|
-
// Do something with the positive value
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const value: number = 5;
|
|
90
|
-
if (isPositive(value)) {
|
|
91
|
-
// Succeeds because value has been asserted to be positive
|
|
92
|
-
takesPositive(value);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
// Error: value may not be positive, as we cannot guarantee the assertion succeeded
|
|
96
|
-
takesPositive(value);
|
|
97
|
-
```
|
|
98
|
-
|
|
99
|
-
### Branded<T, B extends unknown[]>
|
|
100
|
-
This type returns a type that is the same as `T` except that it is branded with the type `B`.
|
|
84
|
+
## Functions
|
|
85
|
+
The following functions are available in the library:
|
|
101
86
|
|
|
102
|
-
###
|
|
103
|
-
|
|
87
|
+
### isAdvancedCondition
|
|
88
|
+
#### Description
|
|
89
|
+
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`.
|
|
104
90
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
A unique symbol used to define type brands.
|
|
91
|
+
#### Parameters
|
|
92
|
+
- `value` (`any`) - The value to check.
|
|
108
93
|
|
|
109
|
-
|
|
110
|
-
|
|
94
|
+
#### Returns
|
|
95
|
+
`value is AdvancedCondition<any>` - Boolean determining whether the value is an instance of `AdvancedCondition`. It functions as a type guard in Typescript.
|
|
111
96
|
|
|
112
|
-
###
|
|
97
|
+
### createAdvancedCondition<T>
|
|
113
98
|
#### Description
|
|
114
|
-
This
|
|
99
|
+
This function creates an instance of `AdvancedCondition` with the specified arguments and sensible defaults applied.
|
|
115
100
|
|
|
116
|
-
####
|
|
117
|
-
- `
|
|
118
|
-
- `include
|
|
119
|
-
- `exclude
|
|
120
|
-
- `match
|
|
101
|
+
#### Parameters
|
|
102
|
+
- `condition` (`WithoutBrand<Omit<AdvancedCondition<T>, "__isAdvancedCondition">>`) - The arguments to use in constructing the condition.
|
|
103
|
+
- `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.
|
|
104
|
+
- `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.
|
|
105
|
+
- `match` - The function used to determine if two values are equal. If this is not specified, it will default to `Object.is`.
|
|
121
106
|
|
|
122
|
-
|
|
123
|
-
|
|
107
|
+
#### Returns
|
|
108
|
+
`AdvancedCondition<T>` - The advanced condition instance.
|
|
124
109
|
|
|
125
110
|
### valueConditionMatches<T>
|
|
126
111
|
#### Description
|
|
@@ -131,7 +116,7 @@ This function takes a value of type `T` and a condition of type `OptionalValueCo
|
|
|
131
116
|
- `condition` (`OptionalValueCondition<T>`) - The condition to check against the value.
|
|
132
117
|
|
|
133
118
|
#### Returns
|
|
134
|
-
|
|
119
|
+
`boolean` - Whether the value meets the condition.
|
|
135
120
|
|
|
136
121
|
### omit<T, K extends keyof T>
|
|
137
122
|
#### Description
|
|
@@ -142,10 +127,11 @@ This function takes an object of type `T` and a list of keys `K` and returns a n
|
|
|
142
127
|
- `keys` (`K[]`) - The keys to omit from the object.
|
|
143
128
|
|
|
144
129
|
#### Returns
|
|
145
|
-
|
|
130
|
+
`Omit<T, K>` - The object with the keys omitted.
|
|
146
131
|
|
|
147
132
|
## Peer Dependencies
|
|
148
133
|
- `is-callable^1.2.7`
|
|
134
|
+
- `@ptolemy2002/ts-brand-utils^1.0.0`
|
|
149
135
|
|
|
150
136
|
## Commands
|
|
151
137
|
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[] = []> = {
|
|
@@ -24,19 +25,15 @@ export type AtLeastOne<T, U = {
|
|
|
24
25
|
[K in keyof T]: Pick<T, K>;
|
|
25
26
|
}> = Partial<T> & U[keyof U];
|
|
26
27
|
export type Override<T, U> = Omit<T, keyof U> & U;
|
|
27
|
-
export
|
|
28
|
+
export declare const advancedConditionSymbol: unique symbol;
|
|
29
|
+
export type AdvancedCondition<T> = Branded<{
|
|
30
|
+
__isAdvancedCondition: true;
|
|
28
31
|
include?: T | false | ((v: T) => boolean) | (T | false | ((v: T) => boolean) | false)[];
|
|
29
32
|
exclude?: T | false | ((v: T) => boolean) | (T | false | ((v: T) => boolean | false))[];
|
|
30
33
|
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
|
-
}
|
|
34
|
+
}, [typeof advancedConditionSymbol]>;
|
|
35
|
+
export declare function isAdvancedCondition(value: any): value is AdvancedCondition<any>;
|
|
36
|
+
export declare function createAdvancedCondition<T>(condition: WithoutBrand<Omit<AdvancedCondition<T>, "__isAdvancedCondition">>): AdvancedCondition<T>;
|
|
40
37
|
export type ValueCondition<T> = AdvancedCondition<T> | T | ((v: T) => boolean) | (ValueCondition<T> | false)[];
|
|
41
38
|
export type OptionalValueCondition<T> = ValueCondition<T> | null;
|
|
42
39
|
export declare function valueConditionMatches<T>(value: T, condition: OptionalValueCondition<T>): boolean;
|
|
@@ -47,11 +44,4 @@ export type ValuesIntersection<T> = ValueOf<{
|
|
|
47
44
|
[K in keyof T]: (x: T[K]) => void;
|
|
48
45
|
}> extends (x: infer I) => void ? I : never;
|
|
49
46
|
export type Contains<L extends unknown[], T> = L extends [...unknown[], T] ? true : L extends [T, ...unknown[]] ? true : L extends [T] ? true : false;
|
|
50
|
-
export declare const __brand: unique symbol;
|
|
51
|
-
export type BrandTag<B extends unknown[]> = {
|
|
52
|
-
readonly [__brand]: B;
|
|
53
|
-
};
|
|
54
|
-
export type Branded<T, B extends unknown[]> = T & BrandTag<B>;
|
|
55
|
-
export type WithBrand<T, B> = T extends BrandTag<unknown[]> ? (Contains<T[typeof __brand], B> extends true ? T : never) : never;
|
|
56
|
-
export declare function brandValue<B extends unknown[], T>(value: T): Branded<T, B>;
|
|
57
47
|
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,25 @@
|
|
|
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
|
-
exports.brandValue = brandValue;
|
|
6
6
|
exports.omit = omit;
|
|
7
7
|
const isCallable = require("is-callable");
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
8
|
+
const ts_brand_utils_1 = require("@ptolemy2002/ts-brand-utils");
|
|
9
|
+
function isAdvancedCondition(value) {
|
|
10
|
+
return (typeof value === "object" &&
|
|
11
|
+
value !== null &&
|
|
12
|
+
"__isAdvancedCondition" in value && value.__isAdvancedCondition === true);
|
|
13
|
+
}
|
|
14
|
+
function createAdvancedCondition(condition) {
|
|
15
|
+
return {
|
|
16
|
+
__isAdvancedCondition: true,
|
|
17
|
+
include: [],
|
|
18
|
+
exclude: [],
|
|
19
|
+
match: Object.is,
|
|
20
|
+
...(0, ts_brand_utils_1.brand)(condition)
|
|
21
|
+
};
|
|
17
22
|
}
|
|
18
|
-
exports.AdvancedCondition = AdvancedCondition;
|
|
19
23
|
function valueConditionMatches(value, condition) {
|
|
20
24
|
if (condition === null)
|
|
21
25
|
return true;
|
|
@@ -24,7 +28,7 @@ function valueConditionMatches(value, condition) {
|
|
|
24
28
|
if (isCallable(condition))
|
|
25
29
|
return condition(value);
|
|
26
30
|
// If the condition value here is not a condition object, it must be of type T, so we can directly compare it
|
|
27
|
-
if (!
|
|
31
|
+
if (!isAdvancedCondition(condition))
|
|
28
32
|
return Object.is(value, condition);
|
|
29
33
|
let { include = [], exclude = [], match = Object.is } = condition;
|
|
30
34
|
if (!Array.isArray(include))
|
|
@@ -42,9 +46,6 @@ function valueConditionMatches(value, condition) {
|
|
|
42
46
|
return !excluded(value);
|
|
43
47
|
return included(value) && !excluded(value);
|
|
44
48
|
}
|
|
45
|
-
function brandValue(value) {
|
|
46
|
-
return value;
|
|
47
|
-
}
|
|
48
49
|
function omit(obj, ...keys) {
|
|
49
50
|
const _ = { ...obj };
|
|
50
51
|
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.0.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
|
}
|