@nicolastoulemont/std 0.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 +1 -0
- package/dist/adt/index.d.mts +2 -0
- package/dist/adt/index.mjs +3 -0
- package/dist/adt-DraJkmij.mjs +318 -0
- package/dist/adt-DraJkmij.mjs.map +1 -0
- package/dist/apply-fn.types-CXDoeA7D.d.mts +8 -0
- package/dist/apply-fn.types-CXDoeA7D.d.mts.map +1 -0
- package/dist/brand/index.d.mts +2 -0
- package/dist/brand/index.mjs +3 -0
- package/dist/brand-CTaxGuU9.mjs +165 -0
- package/dist/brand-CTaxGuU9.mjs.map +1 -0
- package/dist/data/index.d.mts +2 -0
- package/dist/data/index.mjs +3 -0
- package/dist/data-DgzWI4R_.mjs +244 -0
- package/dist/data-DgzWI4R_.mjs.map +1 -0
- package/dist/discriminator.types-DCkkrCj4.d.mts +7 -0
- package/dist/discriminator.types-DCkkrCj4.d.mts.map +1 -0
- package/dist/either/index.d.mts +2 -0
- package/dist/either/index.mjs +3 -0
- package/dist/either-CnOBUH7a.mjs +598 -0
- package/dist/either-CnOBUH7a.mjs.map +1 -0
- package/dist/equality/index.d.mts +86 -0
- package/dist/equality/index.d.mts.map +1 -0
- package/dist/equality/index.mjs +3 -0
- package/dist/equality-YMebYwm1.mjs +201 -0
- package/dist/equality-YMebYwm1.mjs.map +1 -0
- package/dist/err/index.d.mts +2 -0
- package/dist/err/index.mjs +3 -0
- package/dist/err-BqQApH9r.mjs +169 -0
- package/dist/err-BqQApH9r.mjs.map +1 -0
- package/dist/flow/index.d.mts +2 -0
- package/dist/flow/index.mjs +3 -0
- package/dist/flow-pRdnqmMY.mjs +21 -0
- package/dist/flow-pRdnqmMY.mjs.map +1 -0
- package/dist/gen/index.d.mts +3 -0
- package/dist/gen/index.mjs +3 -0
- package/dist/gen-YfMC9sDT.mjs +42 -0
- package/dist/gen-YfMC9sDT.mjs.map +1 -0
- package/dist/index-BCrD3pEs.d.mts +222 -0
- package/dist/index-BCrD3pEs.d.mts.map +1 -0
- package/dist/index-BR7takNf.d.mts +186 -0
- package/dist/index-BR7takNf.d.mts.map +1 -0
- package/dist/index-CcPnhWje.d.mts +72 -0
- package/dist/index-CcPnhWje.d.mts.map +1 -0
- package/dist/index-CtJ8Ks9X.d.mts +105 -0
- package/dist/index-CtJ8Ks9X.d.mts.map +1 -0
- package/dist/index-CvGTIg9L.d.mts +211 -0
- package/dist/index-CvGTIg9L.d.mts.map +1 -0
- package/dist/index-DPVT0yK4.d.mts +50 -0
- package/dist/index-DPVT0yK4.d.mts.map +1 -0
- package/dist/index-DgOAEEpu.d.mts +79 -0
- package/dist/index-DgOAEEpu.d.mts.map +1 -0
- package/dist/index-Dtq3kmln.d.mts +394 -0
- package/dist/index-Dtq3kmln.d.mts.map +1 -0
- package/dist/index-bLRqTe5I.d.mts +80 -0
- package/dist/index-bLRqTe5I.d.mts.map +1 -0
- package/dist/index-tkgTLCoq.d.mts +80 -0
- package/dist/index-tkgTLCoq.d.mts.map +1 -0
- package/dist/index-wTrnFgYg.d.mts +57 -0
- package/dist/index-wTrnFgYg.d.mts.map +1 -0
- package/dist/index-yyBTq8Ys.d.mts +79 -0
- package/dist/index-yyBTq8Ys.d.mts.map +1 -0
- package/dist/index.d.mts +135 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +41 -0
- package/dist/index.mjs.map +1 -0
- package/dist/is-promise-BEl3eGZg.mjs +11 -0
- package/dist/is-promise-BEl3eGZg.mjs.map +1 -0
- package/dist/option/index.d.mts +3 -0
- package/dist/option/index.mjs +3 -0
- package/dist/option-CKHDOVea.mjs +410 -0
- package/dist/option-CKHDOVea.mjs.map +1 -0
- package/dist/option.types-D6TYG_i3.d.mts +89 -0
- package/dist/option.types-D6TYG_i3.d.mts.map +1 -0
- package/dist/pipe/index.d.mts +2 -0
- package/dist/pipe/index.mjs +3 -0
- package/dist/pipe-GYxZNkPB.mjs +10 -0
- package/dist/pipe-GYxZNkPB.mjs.map +1 -0
- package/dist/predicate/index.d.mts +2 -0
- package/dist/predicate/index.mjs +3 -0
- package/dist/predicate-B6-EdVgW.mjs +293 -0
- package/dist/predicate-B6-EdVgW.mjs.map +1 -0
- package/dist/result/index.d.mts +3 -0
- package/dist/result/index.mjs +3 -0
- package/dist/result-C5tPWR60.mjs +422 -0
- package/dist/result-C5tPWR60.mjs.map +1 -0
- package/dist/result-D7XJ96pv.mjs +1 -0
- package/dist/result.types-Ce7AEOzj.d.mts +156 -0
- package/dist/result.types-Ce7AEOzj.d.mts.map +1 -0
- package/dist/run/index.d.mts +2 -0
- package/dist/run/index.mjs +3 -0
- package/dist/run-DpXkImo9.mjs +10 -0
- package/dist/run-DpXkImo9.mjs.map +1 -0
- package/package.json +89 -0
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import { t as Discriminator } from "./discriminator.types-DCkkrCj4.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/data/data.types.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A tagged value object created by tagged().
|
|
7
|
+
* Contains the data fields plus a readonly __typename discriminator.
|
|
8
|
+
*/
|
|
9
|
+
type TaggedValue<Typename extends string, T> = Readonly<T> & Discriminator<Typename>;
|
|
10
|
+
/**
|
|
11
|
+
* A tagged value constructor created by tagged().
|
|
12
|
+
* Can be called as a function to create values, and has static methods
|
|
13
|
+
* for type checking, equality comparison, and hashing.
|
|
14
|
+
*/
|
|
15
|
+
type TaggedConstructor<Typename extends string, T> = {
|
|
16
|
+
/** The __typename discriminator value */
|
|
17
|
+
readonly __typename: Typename;
|
|
18
|
+
/** Create a new tagged value */
|
|
19
|
+
(input: T): TaggedValue<Typename, T>;
|
|
20
|
+
/** Type guard to check if a value is this tagged type */
|
|
21
|
+
is(value: unknown): value is TaggedValue<Typename, T>;
|
|
22
|
+
/** Compare two tagged values for structural equality */
|
|
23
|
+
equals(a: TaggedValue<Typename, T>, b: TaggedValue<Typename, T>): boolean;
|
|
24
|
+
/** Compute hash code for a tagged value */
|
|
25
|
+
hash(value: TaggedValue<Typename, T>): number;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* A struct value object created by struct().
|
|
29
|
+
* A frozen object with equals() and hash() methods (non-enumerable).
|
|
30
|
+
*/
|
|
31
|
+
type StructValue<T extends Record<string, unknown>> = Readonly<T> & {
|
|
32
|
+
/** Compare with another object for structural equality */
|
|
33
|
+
equals(other: T): boolean;
|
|
34
|
+
/** Compute hash code */
|
|
35
|
+
hash(): number;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* A tuple value object created by tuple().
|
|
39
|
+
* A frozen array-like with equals() and hash() methods.
|
|
40
|
+
*/
|
|
41
|
+
type TupleValue<T extends readonly unknown[]> = { readonly [K in keyof T]: T[K] } & {
|
|
42
|
+
readonly length: T["length"];
|
|
43
|
+
/** Compare with another tuple for structural equality */
|
|
44
|
+
equals(other: readonly unknown[]): boolean;
|
|
45
|
+
/** Compute hash code */
|
|
46
|
+
hash(): number;
|
|
47
|
+
[Symbol.iterator](): IterableIterator<T[number]>;
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* An array value object created by array().
|
|
51
|
+
* A frozen array with equals(), hash(), and chainable map/filter methods.
|
|
52
|
+
*
|
|
53
|
+
* Note: Does not extend ReadonlyArray to allow custom map/filter return types.
|
|
54
|
+
*/
|
|
55
|
+
type ArrayValue<T> = {
|
|
56
|
+
readonly [index: number]: T;
|
|
57
|
+
readonly length: number;
|
|
58
|
+
/** Compare with another array for structural equality */
|
|
59
|
+
equals(other: ArrayValue<T> | readonly T[]): boolean;
|
|
60
|
+
/** Compute hash code */
|
|
61
|
+
hash(): number;
|
|
62
|
+
/** Map over elements, returning a new ArrayValue */
|
|
63
|
+
map<U>(fn: (item: T, index: number, array: readonly T[]) => U): ArrayValue<U>;
|
|
64
|
+
/** Filter elements, returning a new ArrayValue */
|
|
65
|
+
filter(fn: (item: T, index: number, array: readonly T[]) => boolean): ArrayValue<T>;
|
|
66
|
+
[Symbol.iterator](): IterableIterator<T>;
|
|
67
|
+
forEach(fn: (item: T, index: number, array: readonly T[]) => void): void;
|
|
68
|
+
find(fn: (item: T, index: number, array: readonly T[]) => boolean): T | undefined;
|
|
69
|
+
findIndex(fn: (item: T, index: number, array: readonly T[]) => boolean): number;
|
|
70
|
+
some(fn: (item: T, index: number, array: readonly T[]) => boolean): boolean;
|
|
71
|
+
every(fn: (item: T, index: number, array: readonly T[]) => boolean): boolean;
|
|
72
|
+
includes(item: T): boolean;
|
|
73
|
+
indexOf(item: T): number;
|
|
74
|
+
reduce<U>(fn: (acc: U, item: T, index: number, array: readonly T[]) => U, initial: U): U;
|
|
75
|
+
slice(start?: number, end?: number): ArrayValue<T>;
|
|
76
|
+
concat(...items: readonly (T | readonly T[])[]): ArrayValue<T>;
|
|
77
|
+
join(separator?: string): string;
|
|
78
|
+
};
|
|
79
|
+
//#endregion
|
|
80
|
+
//#region src/data/data.tagged.d.ts
|
|
81
|
+
/**
|
|
82
|
+
* Create a tagged value constructor without schema validation.
|
|
83
|
+
*
|
|
84
|
+
* Unlike record(), this creates values directly without validation.
|
|
85
|
+
* Values are frozen and have structural equality via equals() and hash() methods.
|
|
86
|
+
*
|
|
87
|
+
* @template T - The data shape type (excluding __typename)
|
|
88
|
+
* @param typename - The __typename discriminator value
|
|
89
|
+
* @returns A constructor function with is(), equals(), and hash() methods
|
|
90
|
+
*
|
|
91
|
+
* @see {@link record} for validated records with schema support
|
|
92
|
+
* @see {@link struct} for untagged value objects
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```ts
|
|
96
|
+
* const Person = tagged<{ name: string; age: number }>("Person")
|
|
97
|
+
*
|
|
98
|
+
* const alice = Person({ name: "Alice", age: 30 })
|
|
99
|
+
* // => { __typename: "Person", name: "Alice", age: 30 }
|
|
100
|
+
*
|
|
101
|
+
* Person.equals(alice, Person({ name: "Alice", age: 30 })) // true
|
|
102
|
+
* Person.is(alice) // true
|
|
103
|
+
*
|
|
104
|
+
* // Type guard usage
|
|
105
|
+
* if (Person.is(unknownValue)) {
|
|
106
|
+
* console.log(unknownValue.name) // TypeScript knows it's a Person
|
|
107
|
+
* }
|
|
108
|
+
*
|
|
109
|
+
* // Values are frozen (immutable)
|
|
110
|
+
* alice.name = "Bob" // TypeError
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
declare function tagged<T extends Record<string, unknown>>(typename: string): TaggedConstructor<typeof typename, T>;
|
|
114
|
+
//#endregion
|
|
115
|
+
//#region src/data/data.struct.d.ts
|
|
116
|
+
/**
|
|
117
|
+
* Create a frozen value object with structural equality.
|
|
118
|
+
*
|
|
119
|
+
* Unlike tagged(), struct() does not add a __typename discriminator.
|
|
120
|
+
* Use this for simple value objects that don't need discrimination.
|
|
121
|
+
*
|
|
122
|
+
* The returned object is frozen (immutable) and has non-enumerable
|
|
123
|
+
* equals() and hash() methods for structural comparison.
|
|
124
|
+
*
|
|
125
|
+
* @template T - The object shape type
|
|
126
|
+
* @param input - The object to wrap
|
|
127
|
+
* @returns A frozen object with equals() and hash() methods
|
|
128
|
+
*
|
|
129
|
+
* @see {@link tagged} for discriminated value objects with __typename
|
|
130
|
+
* @see {@link array} for array value types
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* ```ts
|
|
134
|
+
* const point = struct({ x: 10, y: 20 })
|
|
135
|
+
* const point2 = struct({ x: 10, y: 20 })
|
|
136
|
+
*
|
|
137
|
+
* point.equals(point2) // true
|
|
138
|
+
* point.hash() // number
|
|
139
|
+
*
|
|
140
|
+
* // Object is frozen
|
|
141
|
+
* point.x = 5 // TypeError
|
|
142
|
+
*
|
|
143
|
+
* // equals/hash are non-enumerable
|
|
144
|
+
* Object.keys(point) // ["x", "y"]
|
|
145
|
+
* ```
|
|
146
|
+
*/
|
|
147
|
+
declare function struct<T extends Record<string, unknown>>(input: T): StructValue<T>;
|
|
148
|
+
//#endregion
|
|
149
|
+
//#region src/data/data.tuple.d.ts
|
|
150
|
+
/**
|
|
151
|
+
* Create a tuple with structural equality.
|
|
152
|
+
*
|
|
153
|
+
* The returned tuple is frozen (immutable) and has non-enumerable
|
|
154
|
+
* equals() and hash() methods for structural comparison.
|
|
155
|
+
*
|
|
156
|
+
* @template T - The tuple type as a readonly array of element types
|
|
157
|
+
* @param args - The tuple elements
|
|
158
|
+
* @returns A frozen tuple-like array with equals() and hash() methods
|
|
159
|
+
*
|
|
160
|
+
* @see {@link array} for variable-length arrays with equality
|
|
161
|
+
* @see {@link struct} for object value types
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* ```ts
|
|
165
|
+
* const t1 = tuple(1, "hello", true)
|
|
166
|
+
* const t2 = tuple(1, "hello", true)
|
|
167
|
+
*
|
|
168
|
+
* t1.equals(t2) // true
|
|
169
|
+
* t1.hash() // number
|
|
170
|
+
*
|
|
171
|
+
* // Access elements (typed)
|
|
172
|
+
* t1[0] // number
|
|
173
|
+
* t1[1] // string
|
|
174
|
+
* t1[2] // boolean
|
|
175
|
+
*
|
|
176
|
+
* // Tuple is frozen
|
|
177
|
+
* t1[0] = 5 // TypeError
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
180
|
+
declare function tuple<T extends readonly unknown[]>(...args: T): TupleValue<T>;
|
|
181
|
+
//#endregion
|
|
182
|
+
//#region src/data/data.array.d.ts
|
|
183
|
+
/**
|
|
184
|
+
* Create an array with structural equality.
|
|
185
|
+
*
|
|
186
|
+
* The returned array is frozen (immutable) and has non-enumerable
|
|
187
|
+
* equals() and hash() methods for structural comparison.
|
|
188
|
+
*
|
|
189
|
+
* The map() and filter() methods are overridden to return ArrayValue,
|
|
190
|
+
* enabling method chaining with preserved equality semantics.
|
|
191
|
+
*
|
|
192
|
+
* @template T - The element type of the array
|
|
193
|
+
* @param items - The array elements
|
|
194
|
+
* @returns A frozen array with equals(), hash(), map(), and filter()
|
|
195
|
+
*
|
|
196
|
+
* @see {@link tuple} for fixed-length typed tuples
|
|
197
|
+
* @see {@link struct} for object value types
|
|
198
|
+
*
|
|
199
|
+
* @example
|
|
200
|
+
* ```ts
|
|
201
|
+
* const arr1 = array([1, 2, 3])
|
|
202
|
+
* const arr2 = array([1, 2, 3])
|
|
203
|
+
*
|
|
204
|
+
* arr1.equals(arr2) // true
|
|
205
|
+
* arr1.hash() // number
|
|
206
|
+
*
|
|
207
|
+
* // Chainable operations return ArrayValue
|
|
208
|
+
* arr1.map(x => x * 2) // ArrayValue<number>
|
|
209
|
+
* arr1.filter(x => x > 1) // ArrayValue<number>
|
|
210
|
+
*
|
|
211
|
+
* // Chained equality check
|
|
212
|
+
* arr1.map(x => x * 2).equals(array([2, 4, 6])) // true
|
|
213
|
+
*
|
|
214
|
+
* // Array is frozen
|
|
215
|
+
* arr1[0] = 5 // TypeError
|
|
216
|
+
* arr1.push(4) // TypeError
|
|
217
|
+
* ```
|
|
218
|
+
*/
|
|
219
|
+
declare function array<T>(items: readonly T[]): ArrayValue<T>;
|
|
220
|
+
//#endregion
|
|
221
|
+
export { ArrayValue as a, TaggedValue as c, tagged as i, TupleValue as l, tuple as n, StructValue as o, struct as r, TaggedConstructor as s, array as t };
|
|
222
|
+
//# sourceMappingURL=index-BCrD3pEs.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index-BCrD3pEs.d.mts","names":[],"sources":["../src/data/data.types.ts","../src/data/data.tagged.ts","../src/data/data.struct.ts","../src/data/data.tuple.ts","../src/data/data.array.ts"],"sourcesContent":[],"mappings":";;;;;;AAUA;;AAAsD,KAA1C,WAA0C,CAAA,iBAAA,MAAA,EAAA,CAAA,CAAA,GAAA,QAAA,CAAS,CAAT,CAAA,GAAc,aAAd,CAA4B,QAA5B,CAAA;;;;AAOtD;;AAIU,KAJE,iBAIF,CAAA,iBAAA,MAAA,EAAA,CAAA,CAAA,GAAA;EAAgB;EAAU,SAAA,UAAA,EAFb,QAEa;EAAtB;EAE6B,CAAA,KAAA,EAFjC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AA,OAAA,EAAA,CAAA,CAAA,GAAA,IAAA,EGLA,CHKA,CAAA,EGLI,UHKJ,CGLe,CHKf,CAAA;;;;;AA5B7D;;;;;;AAOA;;;;;;;;;;;;;;;;;;;AAqBA;;;;;;AAeA;;;AAC6B,iBIfb,KJea,CAAA,CAAA,CAAA,CAAA,KAAA,EAAA,SIfY,CJeZ,EAAA,CAAA,EIfkB,UJelB,CIf6B,CJe7B,CAAA"}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
//#region src/err/err.types.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* A tagged error type with a discriminator and optional data.
|
|
4
|
+
* Uses `__typename` to match the existing pattern from `data/tagged`.
|
|
5
|
+
*
|
|
6
|
+
* @template Tag - The error tag (discriminator string)
|
|
7
|
+
* @template Data - Additional error data (optional)
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* type NotFoundError = TaggedError<"NotFoundError", { id: string }>
|
|
12
|
+
* type ValidationError = TaggedError<"ValidationError", { field: string; message: string }>
|
|
13
|
+
* type UnauthorizedError = TaggedError<"UnauthorizedError"> // No additional data
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
type TaggedError$1<Tag extends string, Data extends object = object> = {
|
|
17
|
+
readonly __typename: Tag;
|
|
18
|
+
} & Readonly<Data>;
|
|
19
|
+
/**
|
|
20
|
+
* Type for instances created by the TaggedError class factory.
|
|
21
|
+
* Extends Error and includes the tag, data, and Yieldable protocol.
|
|
22
|
+
*
|
|
23
|
+
* @template Tag - The error tag (discriminator string)
|
|
24
|
+
* @template Data - Additional error data (optional)
|
|
25
|
+
*/
|
|
26
|
+
type TaggedErrorInstance<Tag extends string, Data extends object = object> = Error & Readonly<Data> & {
|
|
27
|
+
readonly __typename: Tag;
|
|
28
|
+
[Symbol.iterator](): Generator<TaggedErrorInstance<Tag, Data>, never, unknown>;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Type for the class returned by TaggedError factory.
|
|
32
|
+
* Supports the Effect-style curried syntax: `TaggedError("Tag")<{ data }>` and `TaggedError("Tag")`
|
|
33
|
+
*
|
|
34
|
+
* @template Tag - The error tag (discriminator string)
|
|
35
|
+
*/
|
|
36
|
+
type TaggedErrorFactory<Tag extends string> = {
|
|
37
|
+
new <Data extends object = object>(...args: keyof Data extends never ? [] : [data: Data]): TaggedErrorInstance<Tag, Data>;
|
|
38
|
+
readonly __typename: Tag;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Extract the tag from a tagged error type.
|
|
42
|
+
*
|
|
43
|
+
* @template E - A TaggedError type
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* type NotFoundError = TaggedError<"NotFoundError", { id: string }>
|
|
48
|
+
* type Tag = ErrorTag<NotFoundError> // "NotFoundError"
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
type ErrorTag<E> = E extends TaggedError$1<infer Tag> ? Tag : never;
|
|
52
|
+
/**
|
|
53
|
+
* Extract the data (excluding __typename) from a tagged error type.
|
|
54
|
+
*
|
|
55
|
+
* @template E - A TaggedError type
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* type NotFoundError = TaggedError<"NotFoundError", { id: string }>
|
|
60
|
+
* type Data = ErrorData<NotFoundError> // { id: string }
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
type ErrorData<E> = E extends TaggedError$1<string, infer Data> ? Data : never;
|
|
64
|
+
//#endregion
|
|
65
|
+
//#region src/err/err.d.ts
|
|
66
|
+
/**
|
|
67
|
+
* Helper type to extract data fields from a TaggedError (excluding __typename)
|
|
68
|
+
*/
|
|
69
|
+
type DataOf<E extends TaggedError<string>> = Omit<E, "__typename">;
|
|
70
|
+
/**
|
|
71
|
+
* Helper type to check if data is empty (only has __typename)
|
|
72
|
+
*/
|
|
73
|
+
type IsEmptyData<Data> = keyof Data extends never ? true : false;
|
|
74
|
+
/**
|
|
75
|
+
* Create a constructor function for a tagged error type.
|
|
76
|
+
* If the error has no additional data, returns a function that takes no arguments.
|
|
77
|
+
* If the error has data, returns a function that requires the data object.
|
|
78
|
+
*
|
|
79
|
+
* @template E - The tagged error type
|
|
80
|
+
* @param tag - The error tag (must match E's __typename)
|
|
81
|
+
* @returns A constructor function for the error type
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```ts
|
|
85
|
+
* // Error with data
|
|
86
|
+
* type NotFoundError = TaggedError<"NotFoundError", { resourceId: string }>
|
|
87
|
+
* const NotFound = Err.tagged<NotFoundError>("NotFoundError")
|
|
88
|
+
* const err = NotFound({ resourceId: "user-123" })
|
|
89
|
+
* // { __typename: "NotFoundError", resourceId: "user-123" }
|
|
90
|
+
*
|
|
91
|
+
* // Error without data
|
|
92
|
+
* type UnauthorizedError = TaggedError<"UnauthorizedError">
|
|
93
|
+
* const Unauthorized = Err.tagged<UnauthorizedError>("UnauthorizedError")
|
|
94
|
+
* const err = Unauthorized()
|
|
95
|
+
* // { __typename: "UnauthorizedError" }
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Create a class-based tagged error type (Effect-style syntax).
|
|
101
|
+
* Returns a class that can be extended to create custom error types.
|
|
102
|
+
* Errors are native Error objects with proper stack traces and instanceof support.
|
|
103
|
+
* Implements Yieldable protocol so errors can be directly yielded in Do computations.
|
|
104
|
+
*
|
|
105
|
+
* @template Tag - The error tag (discriminator string)
|
|
106
|
+
* @param tag - The error tag
|
|
107
|
+
* @returns A class that can be extended with custom data
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```ts
|
|
111
|
+
* // Error with data
|
|
112
|
+
* class NotFoundError extends TaggedError("NotFoundError")<{ id: string }> {}
|
|
113
|
+
* const err = new NotFoundError({ id: "123" })
|
|
114
|
+
* err.id // "123"
|
|
115
|
+
* err.__typename // "NotFoundError"
|
|
116
|
+
* err.stack // Error stack trace
|
|
117
|
+
*
|
|
118
|
+
* // Error without data
|
|
119
|
+
* class UnauthorizedError extends TaggedError("UnauthorizedError") {}
|
|
120
|
+
* const err2 = new UnauthorizedError()
|
|
121
|
+
*
|
|
122
|
+
* // Direct yielding in Do computation
|
|
123
|
+
* const program = gen(function* () {
|
|
124
|
+
* yield* new NotFoundError({ id: "123" }) // Short-circuits with error
|
|
125
|
+
* })
|
|
126
|
+
*
|
|
127
|
+
* // instanceof checks work
|
|
128
|
+
* if (err instanceof NotFoundError) {
|
|
129
|
+
* console.log(err.id)
|
|
130
|
+
* }
|
|
131
|
+
* ```
|
|
132
|
+
*/
|
|
133
|
+
declare function TaggedError<Tag extends string>(tag: Tag): TaggedErrorFactory<Tag>;
|
|
134
|
+
/**
|
|
135
|
+
* Err namespace containing utilities for creating and checking tagged errors.
|
|
136
|
+
*
|
|
137
|
+
* Tagged errors are plain objects with a `__typename` discriminator field,
|
|
138
|
+
* making them serializable and easy to pattern match.
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* ```ts
|
|
142
|
+
* import { Err, Result, Do } from "@repo/std"
|
|
143
|
+
* import type { TaggedError } from "@repo/std"
|
|
144
|
+
*
|
|
145
|
+
* // Define error types
|
|
146
|
+
* type NotFoundError = TaggedError<"NotFoundError", { resourceId: string }>
|
|
147
|
+
* type ValidationError = TaggedError<"ValidationError", { field: string; message: string }>
|
|
148
|
+
*
|
|
149
|
+
* // Create constructors
|
|
150
|
+
* const NotFound = Err.tagged<NotFoundError>("NotFoundError")
|
|
151
|
+
* const Validation = Err.tagged<ValidationError>("ValidationError")
|
|
152
|
+
*
|
|
153
|
+
* // Use with Result
|
|
154
|
+
* function findUser(id: string): Result<User, NotFoundError> {
|
|
155
|
+
* const user = db.find(id)
|
|
156
|
+
* if (!user) return Result.err(NotFound({ resourceId: id }))
|
|
157
|
+
* return Result.ok(user)
|
|
158
|
+
* }
|
|
159
|
+
*
|
|
160
|
+
* // Use with Do computation
|
|
161
|
+
* const program = gen(function* () {
|
|
162
|
+
* const user = yield* findUser("123")
|
|
163
|
+
* return user
|
|
164
|
+
* })
|
|
165
|
+
*
|
|
166
|
+
* // Pattern match on error types
|
|
167
|
+
* pipe(
|
|
168
|
+
* program.run(),
|
|
169
|
+
* Result.match({
|
|
170
|
+
* ok: (user) => console.log(user),
|
|
171
|
+
* err: (error) => {
|
|
172
|
+
* if (Err.is<NotFoundError>("NotFoundError")(error)) {
|
|
173
|
+
* console.log(`Not found: ${error.resourceId}`)
|
|
174
|
+
* }
|
|
175
|
+
* }
|
|
176
|
+
* })
|
|
177
|
+
* )
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
180
|
+
declare const Err: {
|
|
181
|
+
readonly tagged: <E extends TaggedError<string>>(tag: E["__typename"]) => IsEmptyData<DataOf<E>> extends true ? () => E : (data: DataOf<E>) => E;
|
|
182
|
+
readonly is: <E extends TaggedError<string>>(tag: E["__typename"]) => ((error: unknown) => error is E);
|
|
183
|
+
};
|
|
184
|
+
//#endregion
|
|
185
|
+
export { TaggedError$1 as a, ErrorTag as i, TaggedError as n, TaggedErrorFactory as o, ErrorData as r, TaggedErrorInstance as s, Err as t };
|
|
186
|
+
//# sourceMappingURL=index-BR7takNf.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index-BR7takNf.d.mts","names":[],"sources":["../src/err/err.types.ts","../src/err/err.ts"],"sourcesContent":[],"mappings":";;AAcA;;;;;AAWA;;;;;;;;AAGyB,KAdb,aAca,CAAA,YAAA,MAAA,EAAA,aAAA,MAAA,GAAA,MAAA,CAAA,GAAA;EAAS,SAAA,UAAA,EAbX,GAaW;AASlC,CAAA,GArBI,QAqBQ,CArBC,IAqBD,CAAA;;;;;;;;AAkBA,KA9BA,mBA8BwB,CAAA,YAAW,MAAA,EAAA,aAAA,MAAA,GAAA,MAAA,CAAA,GA9BqC,KA8BrC,GA7B7C,QA6B6C,CA7BpC,IA6B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
+
* @template T - The type of value being tested
|
|
6
|
+
*/
|
|
7
|
+
type Predicate$1<T> = (value: T) => boolean;
|
|
8
|
+
/**
|
|
9
|
+
* A refinement predicate that narrows type T to subtype U.
|
|
10
|
+
* This is a type guard - when it returns true, TypeScript knows
|
|
11
|
+
* the value is of the narrower type U.
|
|
12
|
+
*
|
|
13
|
+
* @template T - The base type
|
|
14
|
+
* @template U - The narrower type (must extend T)
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* // Built-in refinement
|
|
19
|
+
* const isString: Refinement<unknown, string> = (v): v is string =>
|
|
20
|
+
* typeof v === "string"
|
|
21
|
+
*
|
|
22
|
+
* // Usage with filter
|
|
23
|
+
* const mixed: (string | number)[] = ["a", 1, "b", 2]
|
|
24
|
+
* const strings: string[] = mixed.filter(isString)
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
type Refinement<T, U extends T> = (value: T) => value is U;
|
|
28
|
+
//#endregion
|
|
29
|
+
//#region src/predicate/predicate.d.ts
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Predicate namespace containing composable boolean functions.
|
|
33
|
+
*
|
|
34
|
+
* Predicates are functions that test values and return booleans.
|
|
35
|
+
* This namespace provides combinators to build complex predicates
|
|
36
|
+
* from simple ones, with support for type guards (refinements).
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```ts
|
|
40
|
+
* import { Predicate, pipe } from "@repo/std"
|
|
41
|
+
*
|
|
42
|
+
* // Basic predicates
|
|
43
|
+
* const isPositive = (n: number) => n > 0
|
|
44
|
+
* const isEven = (n: number) => n % 2 === 0
|
|
45
|
+
*
|
|
46
|
+
* // Combine with and/or
|
|
47
|
+
* const isPositiveEven = Predicate.and(isPositive, isEven)
|
|
48
|
+
* const isZeroOrNegative = Predicate.or(Predicate.equals(0), Predicate.not(isPositive))
|
|
49
|
+
*
|
|
50
|
+
* // Array predicates
|
|
51
|
+
* const allPositive = Predicate.every(isPositive)
|
|
52
|
+
* const hasNegative = Predicate.some(Predicate.not(isPositive))
|
|
53
|
+
*
|
|
54
|
+
* // Use with filter
|
|
55
|
+
* [1, 2, 3, 4, 5].filter(isPositiveEven) // [2, 4]
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
declare const Predicate: {
|
|
59
|
+
readonly not: <T>(predicate: Predicate$1<T>) => Predicate$1<T>;
|
|
60
|
+
readonly and: <T>(...predicates: readonly Predicate$1<T>[]) => Predicate$1<T>;
|
|
61
|
+
readonly or: <T>(...predicates: readonly Predicate$1<T>[]) => Predicate$1<T>;
|
|
62
|
+
readonly every: <T>(predicate: Predicate$1<T>) => Predicate$1<readonly T[]>;
|
|
63
|
+
readonly some: <T>(predicate: Predicate$1<T>) => Predicate$1<readonly T[]>;
|
|
64
|
+
readonly none: <T>(predicate: Predicate$1<T>) => Predicate$1<readonly T[]>;
|
|
65
|
+
readonly andRefinement: <T, U extends T>(...refinements: readonly Refinement<T, U>[]) => Refinement<T, U>;
|
|
66
|
+
readonly orRefinement: <T, U extends T>(...refinements: readonly Refinement<T, U>[]) => Refinement<T, U>;
|
|
67
|
+
readonly constant: <T>(value: boolean) => Predicate$1<T>;
|
|
68
|
+
readonly equals: <T>(expected: T) => Predicate$1<T>;
|
|
69
|
+
};
|
|
70
|
+
//#endregion
|
|
71
|
+
export { Predicate$1 as n, Refinement as r, Predicate as t };
|
|
72
|
+
//# sourceMappingURL=index-CcPnhWje.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index-CcPnhWje.d.mts","names":[],"sources":["../src/predicate/predicate.types.ts","../src/predicate/predicate.ts"],"sourcesContent":[],"mappings":";;AAKA;AAqBA;;;AAAgE,KArBpD,WAqBoD,CAAA,CAAA,CAAA,GAAA,CAAA,KAAA,EArB7B,CAqB6B,EAAA,GAAA,OAAA;;;;;AC8QhE;;;;;;;;;;;;;;;AA5L8E,KDlFlE,UCkFkE,CAAA,CAAA,EAAA,UDlF1C,CCkF0C,CAAA,GAAA,CAAA,KAAA,EDlF7B,CCkF6B,EAAA,GAAA,KAAA,IDlFd,CCkFc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cA4LjE;+BAlRqB,YAAc,OAAK,YAAc;4CA0BpB,YAAc,SAAO,YAAc;2CA8BpC,YAAc,SAAO,YAAc;iCA8B7C,YAAc,OAAK,qBAAuB;gCAsB3C,YAAc,OAAK,qBAAuB;gCAuB1C,YAAc,OAAK,qBAAuB;wCA8BlC,4BAA4B,WAAW,GAAG,SAAO,WAAW,GAAG;uCA8BhE,4BAA4B,WAAW,GAAG,SAAO,WAAW,GAAG;4CA0B5D,YAAc;iCA0BvB,MAAI,YAAc"}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { i as Result } from "./result.types-Ce7AEOzj.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/brand/brand.types.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Unique symbol for brand keys to ensure true nominal typing.
|
|
7
|
+
* Using a symbol prevents collision with actual property keys.
|
|
8
|
+
*/
|
|
9
|
+
declare const BrandSymbol: unique symbol;
|
|
10
|
+
/**
|
|
11
|
+
* A brand type that adds nominal typing to TypeScript.
|
|
12
|
+
* The brand exists only at the type level - no runtime cost.
|
|
13
|
+
*
|
|
14
|
+
* @template K - The brand key (typically a string literal type)
|
|
15
|
+
*/
|
|
16
|
+
type Brand$1<K extends string> = {
|
|
17
|
+
readonly [BrandSymbol]: K;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Create a branded type by intersecting a base type with a brand.
|
|
21
|
+
* The resulting type is structurally compatible with T but nominally unique.
|
|
22
|
+
*
|
|
23
|
+
* @template T - The base type to brand
|
|
24
|
+
* @template K - The brand key (string literal)
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* type UserId = Branded<string, "UserId">
|
|
29
|
+
* type OrderId = Branded<string, "OrderId">
|
|
30
|
+
*
|
|
31
|
+
* // UserId and OrderId are incompatible even though both are strings
|
|
32
|
+
* declare const userId: UserId
|
|
33
|
+
* declare const orderId: OrderId
|
|
34
|
+
* userId = orderId // Type error!
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
type Branded<T, K extends string> = T & Brand$1<K>;
|
|
38
|
+
/**
|
|
39
|
+
* Extract the base type from a branded type.
|
|
40
|
+
* Useful for working with the underlying value.
|
|
41
|
+
*
|
|
42
|
+
* @template B - A branded type
|
|
43
|
+
*/
|
|
44
|
+
type Unbrand<B> = B extends Branded<infer T, string> ? T : B;
|
|
45
|
+
/**
|
|
46
|
+
* Extract the brand key from a branded type.
|
|
47
|
+
*
|
|
48
|
+
* @template B - A branded type
|
|
49
|
+
*/
|
|
50
|
+
type BrandKey<B> = B extends Brand$1<infer K> ? K : never;
|
|
51
|
+
/**
|
|
52
|
+
* A validation function that checks if a value can be branded.
|
|
53
|
+
* Returns true if the value passes validation, false otherwise.
|
|
54
|
+
*
|
|
55
|
+
* @template T - The base type being validated
|
|
56
|
+
*/
|
|
57
|
+
type Validator<T> = (value: T) => boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Error type returned when brand validation fails.
|
|
60
|
+
* Contains the original value and error message for debugging.
|
|
61
|
+
*
|
|
62
|
+
* @template T - The type of the value that failed validation
|
|
63
|
+
*/
|
|
64
|
+
type BrandError<T = unknown> = {
|
|
65
|
+
readonly __typename: "BrandError";
|
|
66
|
+
readonly value: T;
|
|
67
|
+
readonly message: string;
|
|
68
|
+
};
|
|
69
|
+
//#endregion
|
|
70
|
+
//#region src/brand/brand.d.ts
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Brand namespace containing utilities for nominal typing in TypeScript.
|
|
74
|
+
*
|
|
75
|
+
* Brand types add type-level distinctiveness to primitive types without
|
|
76
|
+
* any runtime overhead. This prevents accidental mixing of semantically
|
|
77
|
+
* different values that share the same structural type.
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```ts
|
|
81
|
+
* import { Brand } from "@repo/std"
|
|
82
|
+
* import type { Branded } from "@repo/std"
|
|
83
|
+
*
|
|
84
|
+
* // Define branded types
|
|
85
|
+
* type UserId = Branded<string, "UserId">
|
|
86
|
+
* type Email = Branded<string, "Email">
|
|
87
|
+
*
|
|
88
|
+
* // Create values
|
|
89
|
+
* const userId = Brand.make<UserId>("user-123")
|
|
90
|
+
* const isValidEmail = Brand.is<Email>(s => s.includes("@"))
|
|
91
|
+
*
|
|
92
|
+
* // Type safety
|
|
93
|
+
* function sendEmail(email: Email) { ... }
|
|
94
|
+
* sendEmail(userId) // Type error! UserId is not Email
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
declare const Brand: {
|
|
98
|
+
readonly make: <B extends Branded<unknown, string>>(value: Unbrand<B>) => B;
|
|
99
|
+
readonly unsafeMake: <B extends Branded<unknown, string>>(value: Unbrand<B>) => B;
|
|
100
|
+
readonly is: <T, K extends string>(validator: Validator<T>) => ((value: T) => value is Branded<T, K>);
|
|
101
|
+
readonly refine: <B extends Branded<unknown, string>>(validator: Validator<Unbrand<B>>, errorMessage?: string | ((value: Unbrand<B>) => string)) => ((value: Unbrand<B>) => Result<B, BrandError<Unbrand<B>>>);
|
|
102
|
+
};
|
|
103
|
+
//#endregion
|
|
104
|
+
export { Branded as a, BrandKey as i, Brand$1 as n, Unbrand as o, BrandError as r, Validator as s, Brand as t };
|
|
105
|
+
//# sourceMappingURL=index-CtJ8Ks9X.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index-CtJ8Ks9X.d.mts","names":[],"sources":["../src/brand/brand.types.ts","../src/brand/brand.ts"],"sourcesContent":[],"mappings":";;;;;;;;AAYA,cARc,WAQG,EAAA,OACL,MAAA;AAqBZ;;;;;AAQA;AAAyB,KA9Bb,OA8Ba,CAAA,UAAA,MAAA,CAAA,GAAA;EAAU,UA7BvB,WAAA,CA6BuB,EA7BT,CA6BS;CAA+B;;AAOlE;AAQA;AAiBA;;;;ACqFA;;;;;;;;;;;AApF2E,KDzC/D,OCyC+D,CAAA,CAAA,EAAA,UAAA,MAAA,CAAA,GDzChC,CCyCgC,GDzC5B,OCyC4B,CDzCtB,CCyCsB,CAAA;;;;;;;AA+C9D,KDhFD,OCgFC,CAAA,CAAA,CAAA,GDhFY,CCgFZ,SDhFsB,OCgFtB,CAAA,KAAA,EAAA,EAAA,MAAA,CAAA,GAAA,CAAA,GDhFqD,CCgFrD;;;;;;AAE+C,KD3EhD,QC2EgD,CAAA,CAAA,CAAA,GD3ElC,CC2EkC,SD3ExB,OC2EwB,CAAA,KAAA,EAAA,CAAA,GAAA,CAAA,GAAA,KAAA;;;;;;;KDnEhD,uBAAuB;;;;;;;KAiBvB;;kBAEM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cCmFL;4BAlIkB,iCAAiC,QAAQ,OAAK;kCAoBxC,iCAAiC,QAAQ,OAAK;gDA0BhC,UAAU,gBAAc,eAAe,QAAQ,GAAG;8BA8CpE,qCACpB,UAAU,QAAQ,sCACI,QAAQ,4BAC/B,QAAQ,OAAO,OAAW,GAAG,WAAW,QAAQ"}
|