@ptolemy2002/ts-utils 2.2.0 → 2.4.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 CHANGED
@@ -24,6 +24,9 @@ Given a type `T`, this type loops through every key `K` and wraps that key's val
24
24
  ### MaybePromise<T>
25
25
  This type returns a union of the type `T` and a promise that resolves to the type `T`.
26
26
 
27
+ ### MaybeArray<T>
28
+ This type returns a union of the type `T` and an array of that type.
29
+
27
30
  ### TAndOthers<T, K extends keyof any = PropertyKey>
28
31
  This type returns an object with all the properties of `T` and any number of other properties under keys that fit the type `K`. The additional properties will all have the type `any` because I couldn't figure out how to make them have a type that does not include `ValueOf<T>` due to the way Typescript handles mapped object types.
29
32
 
@@ -33,6 +36,12 @@ This type returns a union of all the keys in `T` that have a value assignable to
33
36
  ### PartialBy<T, K extends keyof T>
34
37
  This type returns a type that is the same as `T` except that the keys in `K` are optional.
35
38
 
39
+ ### AtLeastOne<T, U = {[K in keyof T]: Pick<T, K> }>
40
+ This type returns a type that is the same as `T` except that at least one key is required.
41
+
42
+ ### Override<T, U>
43
+ This type returns a type that is the same as `T` except that the keys in `U` are overridden with the values in `U`.
44
+
36
45
  ### Rename<T, K extends keyof T, N extends string>
37
46
  This type returns a type that is the same as `T` except that the key `K` is renamed to `N`.
38
47
 
@@ -61,6 +70,42 @@ type AdvancedConditionConstructorArgs<T> = {
61
70
  ### ValuesIntersection<T>
62
71
  This type returns an intersection of all the possible values in an object of type `T`.
63
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
+
64
109
  ## Classes
65
110
  The following classes are available in the library:
66
111
 
package/dist/index.d.ts CHANGED
@@ -4,6 +4,7 @@ export type MaybeTransformerRecord<T, Args extends any[] = []> = {
4
4
  [K in keyof T]: MaybeTransformer<T[K], Args>;
5
5
  };
6
6
  export type MaybePromise<T> = T | Promise<T>;
7
+ export type MaybeArray<T> = T | T[];
7
8
  export type TAndOthers<T, K extends keyof any = PropertyKey> = Record<K, any> & T;
8
9
  export type KeysMatching<T, V> = {
9
10
  [K in keyof T]-?: T[K] extends V ? K : never;
@@ -22,6 +23,7 @@ export type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
22
23
  export type AtLeastOne<T, U = {
23
24
  [K in keyof T]: Pick<T, K>;
24
25
  }> = Partial<T> & U[keyof U];
26
+ export type Override<T, U> = Omit<T, keyof U> & U;
25
27
  export type AdvancedConditionConstructorArgs<T> = {
26
28
  include?: T | false | ((v: T) => boolean) | (T | false | ((v: T) => boolean) | false)[];
27
29
  exclude?: T | false | ((v: T) => boolean) | (T | false | ((v: T) => boolean | false))[];
@@ -44,3 +46,11 @@ export type Rename<T, K extends keyof T, N extends string> = Pick<T, Exclude<key
44
46
  export type ValuesIntersection<T> = ValueOf<{
45
47
  [K in keyof T]: (x: T[K]) => void;
46
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>;
package/dist/index.js CHANGED
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AdvancedCondition = void 0;
4
4
  exports.valueConditionMatches = valueConditionMatches;
5
+ exports.brandValue = brandValue;
5
6
  const isCallable = require("is-callable");
6
7
  class AdvancedCondition {
7
8
  constructor({ include = [], exclude = [], match = Object.is } = {}) {
@@ -40,3 +41,6 @@ function valueConditionMatches(value, condition) {
40
41
  return !excluded(value);
41
42
  return included(value) && !excluded(value);
42
43
  }
44
+ function brandValue(value) {
45
+ return value;
46
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptolemy2002/ts-utils",
3
- "version": "2.2.0",
3
+ "version": "2.4.0",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",