@ptolemy2002/ts-utils 2.3.0 → 2.5.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 +47 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +10 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -70,6 +70,42 @@ type AdvancedConditionConstructorArgs<T> = {
|
|
|
70
70
|
### ValuesIntersection<T>
|
|
71
71
|
This type returns an intersection of all the possible values in an object of type `T`.
|
|
72
72
|
|
|
73
|
+
### Contains<L extends unknown[], T>
|
|
74
|
+
This type returns a boolean indicating whether the type `T` is contained in the array `L`.
|
|
75
|
+
|
|
76
|
+
### BrandTag<B extends unknown[]>
|
|
77
|
+
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.
|
|
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`.
|
|
101
|
+
|
|
102
|
+
### WithBrand<T, B>
|
|
103
|
+
Using the `Contains` type, this type returns `T` if it is branded with `B` and `never` otherwise.
|
|
104
|
+
|
|
105
|
+
## Values
|
|
106
|
+
### declare const __brand
|
|
107
|
+
A unique symbol used to define type brands.
|
|
108
|
+
|
|
73
109
|
## Classes
|
|
74
110
|
The following classes are available in the library:
|
|
75
111
|
|
|
@@ -97,6 +133,17 @@ This function takes a value of type `T` and a condition of type `OptionalValueCo
|
|
|
97
133
|
#### Returns
|
|
98
134
|
- `boolean` - Whether the value meets the condition.
|
|
99
135
|
|
|
136
|
+
### omit<T, K extends keyof T>
|
|
137
|
+
#### Description
|
|
138
|
+
This function takes an object of type `T` and a list of keys `K` and returns a new object with all the keys in `K` removed.
|
|
139
|
+
|
|
140
|
+
#### Parameters
|
|
141
|
+
- `object` (`T`) - The object to omit keys from.
|
|
142
|
+
- `keys` (`K[]`) - The keys to omit from the object.
|
|
143
|
+
|
|
144
|
+
#### Returns
|
|
145
|
+
- `Omit<T, K>` - The object with the keys omitted.
|
|
146
|
+
|
|
100
147
|
## Peer Dependencies
|
|
101
148
|
- `is-callable^1.2.7`
|
|
102
149
|
|
package/dist/index.d.ts
CHANGED
|
@@ -46,3 +46,12 @@ export type Rename<T, K extends keyof T, N extends string> = Pick<T, Exclude<key
|
|
|
46
46
|
export type ValuesIntersection<T> = ValueOf<{
|
|
47
47
|
[K in keyof T]: (x: T[K]) => void;
|
|
48
48
|
}> extends (x: infer I) => void ? I : never;
|
|
49
|
+
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
|
+
export declare function omit<T extends object, K extends keyof T>(obj: T, ...keys: K[]): Omit<T, K>;
|
package/dist/index.js
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.AdvancedCondition = void 0;
|
|
4
4
|
exports.valueConditionMatches = valueConditionMatches;
|
|
5
|
+
exports.brandValue = brandValue;
|
|
6
|
+
exports.omit = omit;
|
|
5
7
|
const isCallable = require("is-callable");
|
|
6
8
|
class AdvancedCondition {
|
|
7
9
|
constructor({ include = [], exclude = [], match = Object.is } = {}) {
|
|
@@ -40,3 +42,11 @@ function valueConditionMatches(value, condition) {
|
|
|
40
42
|
return !excluded(value);
|
|
41
43
|
return included(value) && !excluded(value);
|
|
42
44
|
}
|
|
45
|
+
function brandValue(value) {
|
|
46
|
+
return value;
|
|
47
|
+
}
|
|
48
|
+
function omit(obj, ...keys) {
|
|
49
|
+
const _ = { ...obj };
|
|
50
|
+
keys.forEach((key) => delete _[key]);
|
|
51
|
+
return _;
|
|
52
|
+
}
|