@ptolemy2002/ts-utils 2.6.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 +6 -36
- package/dist/index.d.ts +1 -8
- package/dist/index.js +5 -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
|
|
|
@@ -76,42 +81,6 @@ This type returns an intersection of all the possible values in an object of typ
|
|
|
76
81
|
### Contains<L extends unknown[], T>
|
|
77
82
|
This type returns a boolean indicating whether the type `T` is contained in the array `L`.
|
|
78
83
|
|
|
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
84
|
## Functions
|
|
116
85
|
The following functions are available in the library:
|
|
117
86
|
|
|
@@ -162,6 +131,7 @@ This function takes an object of type `T` and a list of keys `K` and returns a n
|
|
|
162
131
|
|
|
163
132
|
## Peer Dependencies
|
|
164
133
|
- `is-callable^1.2.7`
|
|
134
|
+
- `@ptolemy2002/ts-brand-utils^1.0.0`
|
|
165
135
|
|
|
166
136
|
## Commands
|
|
167
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[] = []> = {
|
|
@@ -43,12 +44,4 @@ export type ValuesIntersection<T> = ValueOf<{
|
|
|
43
44
|
[K in keyof T]: (x: T[K]) => void;
|
|
44
45
|
}> extends (x: infer I) => void ? I : never;
|
|
45
46
|
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
47
|
export declare function omit<T extends object, K extends keyof T>(obj: T, ...keys: K[]): Omit<T, K>;
|
package/dist/index.js
CHANGED
|
@@ -3,11 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.isAdvancedCondition = isAdvancedCondition;
|
|
4
4
|
exports.createAdvancedCondition = createAdvancedCondition;
|
|
5
5
|
exports.valueConditionMatches = valueConditionMatches;
|
|
6
|
-
exports.brandValue = brandValue;
|
|
7
6
|
exports.omit = omit;
|
|
8
7
|
const isCallable = require("is-callable");
|
|
8
|
+
const ts_brand_utils_1 = require("@ptolemy2002/ts-brand-utils");
|
|
9
9
|
function isAdvancedCondition(value) {
|
|
10
|
-
return
|
|
10
|
+
return (typeof value === "object" &&
|
|
11
|
+
value !== null &&
|
|
12
|
+
"__isAdvancedCondition" in value && value.__isAdvancedCondition === true);
|
|
11
13
|
}
|
|
12
14
|
function createAdvancedCondition(condition) {
|
|
13
15
|
return {
|
|
@@ -15,7 +17,7 @@ function createAdvancedCondition(condition) {
|
|
|
15
17
|
include: [],
|
|
16
18
|
exclude: [],
|
|
17
19
|
match: Object.is,
|
|
18
|
-
...
|
|
20
|
+
...(0, ts_brand_utils_1.brand)(condition)
|
|
19
21
|
};
|
|
20
22
|
}
|
|
21
23
|
function valueConditionMatches(value, condition) {
|
|
@@ -44,9 +46,6 @@ function valueConditionMatches(value, condition) {
|
|
|
44
46
|
return !excluded(value);
|
|
45
47
|
return included(value) && !excluded(value);
|
|
46
48
|
}
|
|
47
|
-
function brandValue(value) {
|
|
48
|
-
return value;
|
|
49
|
-
}
|
|
50
49
|
function omit(obj, ...keys) {
|
|
51
50
|
const _ = { ...obj };
|
|
52
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
|
}
|