galvorn 1.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 axelhamil
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,155 @@
1
+ # galvorn
2
+
3
+ Rust's `Option` and `Result` for TypeScript. Fully typed, tested, zero dependencies.
4
+
5
+ ```bash
6
+ pnpm add galvorn
7
+ ```
8
+
9
+ ## Why
10
+
11
+ TypeScript lacks a standard way to represent optional values and fallible operations without `null`, `undefined`, or `try/catch`. Galvorn brings Rust's algebraic types to TypeScript with an idiomatic API and complete type safety.
12
+
13
+ - **Option\<T\>** replaces `T | null | undefined`
14
+ - **Result\<T, E\>** replaces `try/catch` and `{ data, error }` patterns
15
+ - **match()** provides exhaustive pattern matching for both types
16
+ - 100% test coverage, 112 tests, zero runtime dependencies
17
+
18
+ ## Usage
19
+
20
+ ### Option
21
+
22
+ ```typescript
23
+ import { Some, None, Option } from 'galvorn';
24
+
25
+ const name = Some('Alice');
26
+ const empty = None<string>();
27
+
28
+ // Transform
29
+ const upper = name.map((n) => n.toUpperCase()); // Some("ALICE")
30
+
31
+ // Chain
32
+ const first = name.flatMap((n) => (n.length > 0 ? Some(n[0]) : None()));
33
+
34
+ // Extract
35
+ const value = empty.unwrapOr('default'); // "default"
36
+
37
+ // Pattern match
38
+ name.match({
39
+ Some: (n) => console.log(`Hello, ${n}`),
40
+ None: () => console.log('No name'),
41
+ });
42
+
43
+ // Convert from nullable
44
+ const opt = Option.fromNullable(document.getElementById('app'));
45
+ ```
46
+
47
+ #### Option API
48
+
49
+ | Method | Description |
50
+ |---|---|
51
+ | `isSome()` | Returns `true` if the option contains a value |
52
+ | `isNone()` | Returns `true` if the option is empty |
53
+ | `unwrap()` | Returns the value or throws |
54
+ | `expect(msg)` | Returns the value or throws with a custom message |
55
+ | `unwrapOr(default)` | Returns the value or a default |
56
+ | `unwrapOrElse(fn)` | Returns the value or computes a default |
57
+ | `map(fn)` | Transforms the inner value |
58
+ | `flatMap(fn)` | Transforms and flattens `Option<Option<U>>` |
59
+ | `filter(predicate)` | Returns `None` if the predicate fails |
60
+ | `and(other)` | Returns `other` if both are `Some`, otherwise `None` |
61
+ | `or(other)` | Returns `self` if `Some`, otherwise `other` |
62
+ | `orElse(fn)` | Returns `self` if `Some`, otherwise calls `fn` |
63
+ | `xor(other)` | Returns `Some` if exactly one is `Some` |
64
+ | `zip(other)` | Combines two `Some` values into a tuple |
65
+ | `inspect(fn)` | Calls `fn` on the value without consuming it |
66
+ | `match({ Some, None })` | Exhaustive pattern matching |
67
+ | `toUndefined()` | Converts to `T \| undefined` |
68
+ | `toNull()` | Converts to `T \| null` |
69
+ | `Option.fromNullable(v)` | Creates `Some(v)` or `None` from a nullable value |
70
+
71
+ ### Result
72
+
73
+ ```typescript
74
+ import { Ok, Fail, Result } from 'galvorn';
75
+
76
+ const success = Ok(42);
77
+ const failure = Fail('something went wrong');
78
+
79
+ // Transform
80
+ const doubled = success.map((n) => n * 2); // Ok(84)
81
+
82
+ // Chain
83
+ const parsed = Ok('42').flatMap((s) => {
84
+ const n = Number(s);
85
+ return Number.isNaN(n) ? Fail('not a number') : Ok(n);
86
+ });
87
+
88
+ // Extract
89
+ const value = failure.unwrapOr(0); // 0
90
+
91
+ // Pattern match
92
+ parsed.match({
93
+ Ok: (n) => console.log(`Parsed: ${n}`),
94
+ Err: (e) => console.error(`Error: ${e}`),
95
+ });
96
+
97
+ // Catch exceptions
98
+ const result = Result.fromThrowable(() => JSON.parse(input));
99
+
100
+ // Combine multiple results
101
+ const combined = Result.combine([Ok(1), Ok(2), Ok(3)]); // Ok([1, 2, 3])
102
+ ```
103
+
104
+ #### Result API
105
+
106
+ | Method | Description |
107
+ |---|---|
108
+ | `isOk()` | Returns `true` if the result is `Ok` |
109
+ | `isErr()` | Returns `true` if the result is `Err` |
110
+ | `unwrap()` | Returns the value or throws |
111
+ | `unwrapErr()` | Returns the error or throws |
112
+ | `expect(msg)` | Returns the value or throws with a custom message |
113
+ | `expectErr(msg)` | Returns the error or throws with a custom message |
114
+ | `unwrapOr(default)` | Returns the value or a default |
115
+ | `unwrapOrElse(fn)` | Returns the value or computes from the error |
116
+ | `map(fn)` | Transforms the `Ok` value |
117
+ | `mapErr(fn)` | Transforms the `Err` value |
118
+ | `mapOr(default, fn)` | Transforms `Ok` or returns a default |
119
+ | `mapOrElse(errFn, okFn)` | Transforms both branches |
120
+ | `flatMap(fn)` | Transforms and flattens `Result<Result<U, E>, E>` |
121
+ | `and(other)` | Returns `other` if both are `Ok`, otherwise the first `Err` |
122
+ | `or(other)` | Returns `self` if `Ok`, otherwise `other` |
123
+ | `orElse(fn)` | Returns `self` if `Ok`, otherwise calls `fn` with the error |
124
+ | `inspect(fn)` | Calls `fn` on the `Ok` value without consuming it |
125
+ | `inspectErr(fn)` | Calls `fn` on the `Err` value without consuming it |
126
+ | `match({ Ok, Err })` | Exhaustive pattern matching |
127
+ | `Result.fromThrowable(fn)` | Wraps a throwing function into `Result<T, Error>` |
128
+ | `Result.combine(results)` | Collects an array of results into `Result<T[], E>` |
129
+
130
+ ### Standalone match
131
+
132
+ ```typescript
133
+ import { match, Some, Ok } from 'galvorn';
134
+
135
+ // Works with both Option and Result
136
+ const msg = match(Some(42), {
137
+ Some: (v) => `got ${v}`,
138
+ None: () => 'nothing',
139
+ });
140
+
141
+ const status = match(Ok(200), {
142
+ Ok: (code) => `status ${code}`,
143
+ Err: (e) => `failed: ${e}`,
144
+ });
145
+ ```
146
+
147
+ ## Compatibility
148
+
149
+ - Node.js >= 20
150
+ - TypeScript >= 5.0
151
+ - ESM only
152
+
153
+ ## License
154
+
155
+ MIT
@@ -0,0 +1,331 @@
1
+ //#region src/option.d.ts
2
+ /**
3
+ * Represents an optional value: either `Some(value)` or `None`.
4
+ *
5
+ * Use {@link Some} and {@link None} factory functions to create instances.
6
+ *
7
+ * @typeParam T - The type of the contained value.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * const name = Some('Alice');
12
+ * const greeting = name.map(n => `Hello, ${n}`).unwrapOr('Hello, stranger');
13
+ * ```
14
+ */
15
+ declare abstract class Option<T> {
16
+ /** Returns `true` if the option contains a value. */
17
+ abstract isSome(): boolean;
18
+ /** Returns `true` if the option is empty. */
19
+ abstract isNone(): boolean;
20
+ /**
21
+ * Returns the contained value.
22
+ * @throws If the option is `None`.
23
+ */
24
+ abstract unwrap(): T;
25
+ /**
26
+ * Returns the contained value.
27
+ * @param msg - Error message to throw if the option is `None`.
28
+ * @throws With `msg` if the option is `None`.
29
+ */
30
+ abstract expect(msg: string): T;
31
+ /**
32
+ * Returns the contained value, or `defaultValue` if `None`.
33
+ * @param defaultValue - Value to return if `None`.
34
+ */
35
+ abstract unwrapOr(defaultValue: T): T;
36
+ /**
37
+ * Returns the contained value, or computes it from `f` if `None`.
38
+ * @param f - Function to compute the default value.
39
+ */
40
+ abstract unwrapOrElse(f: () => T): T;
41
+ /**
42
+ * Transforms the contained value by applying `f`.
43
+ * Returns `None` if the option is `None`.
44
+ * @param f - Function to apply to the value.
45
+ */
46
+ abstract map<U>(f: (value: T) => U): Option<U>;
47
+ /**
48
+ * Transforms the contained value by applying `f`, which itself returns an `Option`.
49
+ * Flattens the result (avoids `Option<Option<U>>`).
50
+ * @param f - Function returning an `Option`.
51
+ */
52
+ abstract flatMap<U>(f: (value: T) => Option<U>): Option<U>;
53
+ /**
54
+ * Returns `None` if the option is `None` or the predicate returns `false`.
55
+ * @param predicate - Function to test the contained value.
56
+ */
57
+ abstract filter(predicate: (value: T) => boolean): Option<T>;
58
+ /**
59
+ * Returns `None` if this option is `None`, otherwise returns `optb`.
60
+ * @param optb - The other option.
61
+ */
62
+ abstract and<U>(optb: Option<U>): Option<U>;
63
+ /**
64
+ * Returns this option if it is `Some`, otherwise returns `optb`.
65
+ * @param optb - Fallback option.
66
+ */
67
+ abstract or(optb: Option<T>): Option<T>;
68
+ /**
69
+ * Returns this option if it is `Some`, otherwise calls `f` and returns the result.
70
+ * @param f - Function producing the fallback option.
71
+ */
72
+ abstract orElse(f: () => Option<T>): Option<T>;
73
+ /**
74
+ * Returns `Some` if exactly one of `this` and `optb` is `Some`, otherwise `None`.
75
+ * @param optb - The other option.
76
+ */
77
+ abstract xor(optb: Option<T>): Option<T>;
78
+ /**
79
+ * Zips this option with another. Returns `Some([a, b])` if both are `Some`, otherwise `None`.
80
+ * @param other - The other option to zip with.
81
+ */
82
+ abstract zip<U>(other: Option<U>): Option<[T, U]>;
83
+ /**
84
+ * Calls `f` with the contained value if `Some`, then returns `this` unchanged.
85
+ * Useful for side effects in a chain.
86
+ * @param f - Function to call with the value.
87
+ */
88
+ abstract inspect(f: (value: T) => void): Option<T>;
89
+ /**
90
+ * Exhaustive pattern matching on the option.
91
+ * @param patterns - Object with `Some` and `None` handlers.
92
+ *
93
+ * @example
94
+ * ```ts
95
+ * Some(42).match({
96
+ * Some: v => `got ${v}`,
97
+ * None: () => 'nothing',
98
+ * });
99
+ * ```
100
+ */
101
+ abstract match<U>(patterns: {
102
+ Some: (value: T) => U;
103
+ None: () => U;
104
+ }): U;
105
+ /** Converts to `T | undefined`. Returns `undefined` if `None`. */
106
+ abstract toUndefined(): T | undefined;
107
+ /** Converts to `T | null`. Returns `null` if `None`. */
108
+ abstract toNull(): T | null;
109
+ /**
110
+ * Creates an `Option` from a nullable value.
111
+ * Returns `Some(value)` if non-null/undefined, otherwise `None`.
112
+ * @param value - A possibly null or undefined value.
113
+ */
114
+ static fromNullable<T>(value: T | null | undefined): Option<T>;
115
+ }
116
+ /**
117
+ * Creates an `Option` containing a value.
118
+ * @param value - The value to wrap.
119
+ *
120
+ * @example
121
+ * ```ts
122
+ * const opt = Some(42);
123
+ * opt.unwrap(); // 42
124
+ * ```
125
+ */
126
+ declare function Some<T>(value: T): Option<T>;
127
+ /**
128
+ * Creates an empty `Option`.
129
+ *
130
+ * @example
131
+ * ```ts
132
+ * const opt = None<number>();
133
+ * opt.isNone(); // true
134
+ * ```
135
+ */
136
+ declare function None<T>(): Option<T>;
137
+ //# sourceMappingURL=option.d.ts.map
138
+ //#endregion
139
+ //#region src/result.d.ts
140
+ /**
141
+ * Represents the outcome of a fallible operation: either `Ok(value)` or `Fail(error)`.
142
+ *
143
+ * Use {@link Ok} and {@link Fail} factory functions to create instances.
144
+ *
145
+ * @typeParam T - The type of the success value.
146
+ * @typeParam E - The type of the error value. Defaults to `string`.
147
+ *
148
+ * @example
149
+ * ```ts
150
+ * const parsed = Result.fromThrowable(() => JSON.parse(input));
151
+ * const value = parsed.unwrapOr({});
152
+ * ```
153
+ */
154
+ declare abstract class Result<T, E = string> {
155
+ /** Returns `true` if the result is `Ok`. */
156
+ abstract isOk(): boolean;
157
+ /** Returns `true` if the result is `Err`. */
158
+ abstract isErr(): boolean;
159
+ /**
160
+ * Returns the success value.
161
+ * @throws If the result is `Err`.
162
+ */
163
+ abstract unwrap(): T;
164
+ /**
165
+ * Returns the error value.
166
+ * @throws If the result is `Ok`.
167
+ */
168
+ abstract unwrapErr(): E;
169
+ /**
170
+ * Returns the success value.
171
+ * @param msg - Error message to throw if the result is `Err`.
172
+ * @throws With `msg` if the result is `Err`.
173
+ */
174
+ abstract expect(msg: string): T;
175
+ /**
176
+ * Returns the error value.
177
+ * @param msg - Error message to throw if the result is `Ok`.
178
+ * @throws With `msg` if the result is `Ok`.
179
+ */
180
+ abstract expectErr(msg: string): E;
181
+ /**
182
+ * Returns the success value, or `defaultValue` if `Err`.
183
+ * @param defaultValue - Value to return if `Err`.
184
+ */
185
+ abstract unwrapOr(defaultValue: T): T;
186
+ /**
187
+ * Returns the success value, or computes it from the error using `f`.
188
+ * @param f - Function to compute the default from the error.
189
+ */
190
+ abstract unwrapOrElse(f: (error: E) => T): T;
191
+ /**
192
+ * Transforms the success value by applying `f`.
193
+ * Returns the original `Err` if the result is `Err`.
194
+ * @param f - Function to apply to the success value.
195
+ */
196
+ abstract map<U>(f: (value: T) => U): Result<U, E>;
197
+ /**
198
+ * Transforms the error value by applying `f`.
199
+ * Returns the original `Ok` if the result is `Ok`.
200
+ * @param f - Function to apply to the error value.
201
+ */
202
+ abstract mapErr<F>(f: (error: E) => F): Result<T, F>;
203
+ /**
204
+ * Applies `f` to the success value, or returns `defaultValue` if `Err`.
205
+ * @param defaultValue - Value to return if `Err`.
206
+ * @param f - Function to apply to the success value.
207
+ */
208
+ abstract mapOr<U>(defaultValue: U, f: (value: T) => U): U;
209
+ /**
210
+ * Applies `f` to the success value, or `defaultFn` to the error.
211
+ * @param defaultFn - Function to apply to the error.
212
+ * @param f - Function to apply to the success value.
213
+ */
214
+ abstract mapOrElse<U>(defaultFn: (error: E) => U, f: (value: T) => U): U;
215
+ /**
216
+ * Transforms the success value by applying `f`, which itself returns a `Result`.
217
+ * Flattens the result (avoids `Result<Result<U, E>, E>`).
218
+ * @param f - Function returning a `Result`.
219
+ */
220
+ abstract flatMap<U>(f: (value: T) => Result<U, E>): Result<U, E>;
221
+ /**
222
+ * Returns `Err` if this result is `Err`, otherwise returns `res`.
223
+ * @param res - The other result.
224
+ */
225
+ abstract and<U>(res: Result<U, E>): Result<U, E>;
226
+ /**
227
+ * Returns this result if it is `Ok`, otherwise returns `res`.
228
+ * @param res - Fallback result.
229
+ */
230
+ abstract or<F>(res: Result<T, F>): Result<T, F>;
231
+ /**
232
+ * Returns this result if it is `Ok`, otherwise calls `f` with the error and returns the result.
233
+ * @param f - Function producing the fallback result from the error.
234
+ */
235
+ abstract orElse<F>(f: (error: E) => Result<T, F>): Result<T, F>;
236
+ /**
237
+ * Calls `f` with the success value if `Ok`, then returns `this` unchanged.
238
+ * Useful for side effects in a chain.
239
+ * @param f - Function to call with the success value.
240
+ */
241
+ abstract inspect(f: (value: T) => void): Result<T, E>;
242
+ /**
243
+ * Calls `f` with the error value if `Err`, then returns `this` unchanged.
244
+ * Useful for side effects in a chain.
245
+ * @param f - Function to call with the error value.
246
+ */
247
+ abstract inspectErr(f: (error: E) => void): Result<T, E>;
248
+ /**
249
+ * Exhaustive pattern matching on the result.
250
+ * @param patterns - Object with `Ok` and `Err` handlers.
251
+ *
252
+ * @example
253
+ * ```ts
254
+ * Ok(42).match({
255
+ * Ok: v => `got ${v}`,
256
+ * Err: e => `error: ${e}`,
257
+ * });
258
+ * ```
259
+ */
260
+ abstract match<U>(patterns: {
261
+ Ok: (value: T) => U;
262
+ Err: (error: E) => U;
263
+ }): U;
264
+ /**
265
+ * Wraps a potentially throwing function into a `Result`.
266
+ * Returns `Ok(value)` on success, `Fail(error)` on exception.
267
+ * Non-`Error` exceptions are wrapped in `new Error(String(e))`.
268
+ * @param f - Function that may throw.
269
+ */
270
+ static fromThrowable<T>(f: () => T): Result<T, Error>;
271
+ /**
272
+ * Collects an array of results into a single result.
273
+ * Returns `Ok(values[])` if all succeed, or the first `Err` encountered.
274
+ * @param results - Array of results to combine.
275
+ *
276
+ * @example
277
+ * ```ts
278
+ * Result.combine([Ok(1), Ok(2), Ok(3)]); // Ok([1, 2, 3])
279
+ * Result.combine([Ok(1), Fail('no')]); // Fail('no')
280
+ * ```
281
+ */
282
+ static combine<E = string>(results: Result<unknown, E>[]): Result<unknown[], E>;
283
+ }
284
+ /**
285
+ * Creates a successful `Result` containing a value.
286
+ * @param value - The success value to wrap.
287
+ *
288
+ * @example
289
+ * ```ts
290
+ * const res = Ok(42);
291
+ * res.unwrap(); // 42
292
+ * ```
293
+ */
294
+ declare function Ok<T, E = string>(value: T): Result<T, E>;
295
+ /**
296
+ * Creates a failed `Result` containing an error.
297
+ * @param error - The error value to wrap.
298
+ *
299
+ * @example
300
+ * ```ts
301
+ * const res = Fail('not found');
302
+ * res.unwrapErr(); // 'not found'
303
+ * ```
304
+ */
305
+ declare function Fail<T = never, E = string>(error: E): Result<T, E>;
306
+ //# sourceMappingURL=result.d.ts.map
307
+ //#endregion
308
+ //#region src/match.d.ts
309
+ /**
310
+ * Standalone pattern matching for `Option`.
311
+ * @param value - The `Option` to match on.
312
+ * @param patterns - Object with `Some` and `None` handlers.
313
+ */
314
+ declare function match<T, U>(value: Option<T>, patterns: {
315
+ Some: (value: T) => U;
316
+ None: () => U;
317
+ }): U;
318
+ /**
319
+ * Standalone pattern matching for `Result`.
320
+ * @param value - The `Result` to match on.
321
+ * @param patterns - Object with `Ok` and `Err` handlers.
322
+ */
323
+ declare function match<T, E, U>(value: Result<T, E>, patterns: {
324
+ Ok: (value: T) => U;
325
+ Err: (error: E) => U;
326
+ }): U;
327
+ //# sourceMappingURL=match.d.ts.map
328
+
329
+ //#endregion
330
+ export { Fail, None, Ok, Option, Result, Some, match };
331
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/option.ts","../src/result.ts","../src/match.ts"],"sourcesContent":[],"mappings":";;AAaA;;;;;;;;;;;;AA4CiC,uBA5CX,MA4CW,CAAA,CAAA,CAAA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EAcI,CAAA,CAAA,EAAA,IAAA,MAAA,CAAA,CAAA,KAAA,EAdqB,CAcrB,CAAA,EAdyB,MAczB,CAdgC,CAchC,EAdmC,CAcnC,CAAA;;;;;;;;;;AC7WpB;AAAqB,iBD6WL,IC7WK,CAAA,IAAA,KAAA,EAAA,IAAA,MAAA,CAAA,CAAA,KAAA,ED6W8B,CC7W9B,CAAA,ED6WkC,MC7WlC,CD6WyC,CC7WzC,ED6W4C,CC7W5C,CAAA;;;;;;;;;AFmCY,iBEnCjB,KFmCiB,CAAA,CAAA,EAAA,CAAA,CAAA,CAAA,KAAA,EElCxB,MFkCwB,CElCjB,CFkCiB,CAAA,EAAA,QAAA,EAAA;MAAI,EAAA,CAAA,KAAA,EEjCT,CFiCS,EAAA,GEjCH,CFiCG;MAOR,EAAA,GAAA,GExCoB,CFwCpB;IEvC1B;;;;;;AF8CuD,iBExC1C,KFwC0C,CAAA,CAAA,EAAA,CAAA,EAAA,CAAA,CAAA,CAAA,KAAA,EEvCjD,MFuCiD,CEvC1C,CFuC0C,EEvCvC,CFuCuC,CAAA,EAAA,QAAA,EAAA;MAAP,CAAA,KAAA,EEtCzB,CFsCyB,EAAA,GEtCnB,CFsCmB;KAMd,EAAA,CAAA,KAAA,EE5CW,CF4CX,EAAA,GE5CiB,CF4CjB;IE3ClC"}
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ function e(e,t){return e.match(t)}var t=class{static fromNullable(e){return e==null?a():i(e)}},n=class extends t{constructor(e){super(),this.value=e}isSome(){return!0}isNone(){return!1}unwrap(){return this.value}expect(e){return this.value}unwrapOr(e){return this.value}unwrapOrElse(e){return this.value}map(e){return i(e(this.value))}flatMap(e){return e(this.value)}filter(e){return e(this.value)?this:a()}and(e){return e}or(e){return this}orElse(e){return this}xor(e){return e.isNone()?this:a()}zip(e){return e.isSome()?i([this.value,e.unwrap()]):a()}inspect(e){return e(this.value),this}match(e){return e.Some(this.value)}toUndefined(){return this.value}toNull(){return this.value}toString(){return`Some(${this.value})`}},r=class extends t{isSome(){return!1}isNone(){return!0}unwrap(){throw Error(`Called unwrap on a None value`)}expect(e){throw Error(e)}unwrapOr(e){return e}unwrapOrElse(e){return e()}map(e){return a()}flatMap(e){return a()}filter(e){return this}and(e){return a()}or(e){return e}orElse(e){return e()}xor(e){return e.isSome()?e:a()}zip(e){return a()}inspect(e){return this}match(e){return e.None()}toUndefined(){}toNull(){return null}toString(){return`None`}};function i(e){return new n(e)}function a(){return new r}var o=class{static fromThrowable(e){try{return l(e())}catch(e){return u(e instanceof Error?e:Error(String(e)))}}static combine(e){let t=[];for(let n of e){if(n.isErr())return u(n.unwrapErr());t.push(n.unwrap())}return l(t)}},s=class extends o{constructor(e){super(),this.value=e}isOk(){return!0}isErr(){return!1}unwrap(){return this.value}unwrapErr(){throw Error(`Called unwrapErr on an Ok value`)}expect(e){return this.value}expectErr(e){throw Error(e)}unwrapOr(e){return this.value}unwrapOrElse(e){return this.value}map(e){return l(e(this.value))}mapErr(e){return l(this.value)}mapOr(e,t){return t(this.value)}mapOrElse(e,t){return t(this.value)}flatMap(e){return e(this.value)}and(e){return e}or(e){return l(this.value)}orElse(e){return l(this.value)}inspect(e){return e(this.value),this}inspectErr(e){return this}match(e){return e.Ok(this.value)}toString(){return`Ok(${this.value})`}},c=class extends o{constructor(e){super(),this.error=e}isOk(){return!1}isErr(){return!0}unwrap(){throw Error(`Called unwrap on an Err value: ${this.error}`)}unwrapErr(){return this.error}expect(e){throw Error(e)}expectErr(e){return this.error}unwrapOr(e){return e}unwrapOrElse(e){return e(this.error)}map(e){return u(this.error)}mapErr(e){return u(e(this.error))}mapOr(e,t){return e}mapOrElse(e,t){return e(this.error)}flatMap(e){return u(this.error)}and(e){return u(this.error)}or(e){return e}orElse(e){return e(this.error)}inspect(e){return this}inspectErr(e){return e(this.error),this}match(e){return e.Err(this.error)}toString(){return`Err(${this.error})`}};function l(e){return new s(e)}function u(e){return new c(e)}export{u as Fail,a as None,l as Ok,t as Option,o as Result,i as Some,e as match};
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/match.ts","../src/option.ts","../src/result.ts"],"sourcesContent":["import type { Option } from './option.js';\nimport type { Result } from './result.js';\n\n/**\n * Standalone pattern matching for `Option`.\n * @param value - The `Option` to match on.\n * @param patterns - Object with `Some` and `None` handlers.\n */\nexport function match<T, U>(\n value: Option<T>,\n patterns: { Some: (value: T) => U; None: () => U },\n): U;\n/**\n * Standalone pattern matching for `Result`.\n * @param value - The `Result` to match on.\n * @param patterns - Object with `Ok` and `Err` handlers.\n */\nexport function match<T, E, U>(\n value: Result<T, E>,\n patterns: { Ok: (value: T) => U; Err: (error: E) => U },\n): U;\nexport function match(\n value: Option<unknown> | Result<unknown, unknown>,\n patterns: unknown,\n): unknown {\n return value.match(patterns as never);\n}\n","/**\n * Represents an optional value: either `Some(value)` or `None`.\n *\n * Use {@link Some} and {@link None} factory functions to create instances.\n *\n * @typeParam T - The type of the contained value.\n *\n * @example\n * ```ts\n * const name = Some('Alice');\n * const greeting = name.map(n => `Hello, ${n}`).unwrapOr('Hello, stranger');\n * ```\n */\nexport abstract class Option<T> {\n /** Returns `true` if the option contains a value. */\n abstract isSome(): boolean;\n\n /** Returns `true` if the option is empty. */\n abstract isNone(): boolean;\n\n /**\n * Returns the contained value.\n * @throws If the option is `None`.\n */\n abstract unwrap(): T;\n\n /**\n * Returns the contained value.\n * @param msg - Error message to throw if the option is `None`.\n * @throws With `msg` if the option is `None`.\n */\n abstract expect(msg: string): T;\n\n /**\n * Returns the contained value, or `defaultValue` if `None`.\n * @param defaultValue - Value to return if `None`.\n */\n abstract unwrapOr(defaultValue: T): T;\n\n /**\n * Returns the contained value, or computes it from `f` if `None`.\n * @param f - Function to compute the default value.\n */\n abstract unwrapOrElse(f: () => T): T;\n\n /**\n * Transforms the contained value by applying `f`.\n * Returns `None` if the option is `None`.\n * @param f - Function to apply to the value.\n */\n abstract map<U>(f: (value: T) => U): Option<U>;\n\n /**\n * Transforms the contained value by applying `f`, which itself returns an `Option`.\n * Flattens the result (avoids `Option<Option<U>>`).\n * @param f - Function returning an `Option`.\n */\n abstract flatMap<U>(f: (value: T) => Option<U>): Option<U>;\n\n /**\n * Returns `None` if the option is `None` or the predicate returns `false`.\n * @param predicate - Function to test the contained value.\n */\n abstract filter(predicate: (value: T) => boolean): Option<T>;\n\n /**\n * Returns `None` if this option is `None`, otherwise returns `optb`.\n * @param optb - The other option.\n */\n abstract and<U>(optb: Option<U>): Option<U>;\n\n /**\n * Returns this option if it is `Some`, otherwise returns `optb`.\n * @param optb - Fallback option.\n */\n abstract or(optb: Option<T>): Option<T>;\n\n /**\n * Returns this option if it is `Some`, otherwise calls `f` and returns the result.\n * @param f - Function producing the fallback option.\n */\n abstract orElse(f: () => Option<T>): Option<T>;\n\n /**\n * Returns `Some` if exactly one of `this` and `optb` is `Some`, otherwise `None`.\n * @param optb - The other option.\n */\n abstract xor(optb: Option<T>): Option<T>;\n\n /**\n * Zips this option with another. Returns `Some([a, b])` if both are `Some`, otherwise `None`.\n * @param other - The other option to zip with.\n */\n abstract zip<U>(other: Option<U>): Option<[T, U]>;\n\n /**\n * Calls `f` with the contained value if `Some`, then returns `this` unchanged.\n * Useful for side effects in a chain.\n * @param f - Function to call with the value.\n */\n abstract inspect(f: (value: T) => void): Option<T>;\n\n /**\n * Exhaustive pattern matching on the option.\n * @param patterns - Object with `Some` and `None` handlers.\n *\n * @example\n * ```ts\n * Some(42).match({\n * Some: v => `got ${v}`,\n * None: () => 'nothing',\n * });\n * ```\n */\n abstract match<U>(patterns: { Some: (value: T) => U; None: () => U }): U;\n\n /** Converts to `T | undefined`. Returns `undefined` if `None`. */\n abstract toUndefined(): T | undefined;\n\n /** Converts to `T | null`. Returns `null` if `None`. */\n abstract toNull(): T | null;\n\n /**\n * Creates an `Option` from a nullable value.\n * Returns `Some(value)` if non-null/undefined, otherwise `None`.\n * @param value - A possibly null or undefined value.\n */\n static fromNullable<T>(value: T | null | undefined): Option<T> {\n return value != null ? Some(value) : None<T>();\n }\n}\n\nclass SomeOption<T> extends Option<T> {\n constructor(private readonly value: T) {\n super();\n }\n\n isSome(): boolean {\n return true;\n }\n\n isNone(): boolean {\n return false;\n }\n\n unwrap(): T {\n return this.value;\n }\n\n expect(_msg: string): T {\n return this.value;\n }\n\n unwrapOr(_defaultValue: T): T {\n return this.value;\n }\n\n unwrapOrElse(_f: () => T): T {\n return this.value;\n }\n\n map<U>(f: (value: T) => U): Option<U> {\n return Some(f(this.value));\n }\n\n flatMap<U>(f: (value: T) => Option<U>): Option<U> {\n return f(this.value);\n }\n\n filter(predicate: (value: T) => boolean): Option<T> {\n return predicate(this.value) ? this : None<T>();\n }\n\n and<U>(optb: Option<U>): Option<U> {\n return optb;\n }\n\n or(_optb: Option<T>): Option<T> {\n return this;\n }\n\n orElse(_f: () => Option<T>): Option<T> {\n return this;\n }\n\n xor(optb: Option<T>): Option<T> {\n return optb.isNone() ? this : None<T>();\n }\n\n zip<U>(other: Option<U>): Option<[T, U]> {\n return other.isSome() ? Some<[T, U]>([this.value, other.unwrap()]) : None<[T, U]>();\n }\n\n inspect(f: (value: T) => void): Option<T> {\n f(this.value);\n return this;\n }\n\n match<U>(patterns: { Some: (value: T) => U; None: () => U }): U {\n return patterns.Some(this.value);\n }\n\n toUndefined(): T | undefined {\n return this.value;\n }\n\n toNull(): T | null {\n return this.value;\n }\n\n toString(): string {\n return `Some(${this.value})`;\n }\n}\n\nclass NoneOption<T> extends Option<T> {\n isSome(): boolean {\n return false;\n }\n\n isNone(): boolean {\n return true;\n }\n\n unwrap(): T {\n throw new Error('Called unwrap on a None value');\n }\n\n expect(msg: string): T {\n throw new Error(msg);\n }\n\n unwrapOr(defaultValue: T): T {\n return defaultValue;\n }\n\n unwrapOrElse(f: () => T): T {\n return f();\n }\n\n map<U>(_f: (value: T) => U): Option<U> {\n return None<U>();\n }\n\n flatMap<U>(_f: (value: T) => Option<U>): Option<U> {\n return None<U>();\n }\n\n filter(_predicate: (value: T) => boolean): Option<T> {\n return this;\n }\n\n and<U>(_optb: Option<U>): Option<U> {\n return None<U>();\n }\n\n or(optb: Option<T>): Option<T> {\n return optb;\n }\n\n orElse(f: () => Option<T>): Option<T> {\n return f();\n }\n\n xor(optb: Option<T>): Option<T> {\n return optb.isSome() ? optb : None<T>();\n }\n\n zip<U>(_other: Option<U>): Option<[T, U]> {\n return None<[T, U]>();\n }\n\n inspect(_f: (value: T) => void): Option<T> {\n return this;\n }\n\n match<U>(patterns: { Some: (value: T) => U; None: () => U }): U {\n return patterns.None();\n }\n\n toUndefined(): T | undefined {\n return undefined;\n }\n\n toNull(): T | null {\n return null;\n }\n\n toString(): string {\n return 'None';\n }\n}\n\n/**\n * Creates an `Option` containing a value.\n * @param value - The value to wrap.\n *\n * @example\n * ```ts\n * const opt = Some(42);\n * opt.unwrap(); // 42\n * ```\n */\nexport function Some<T>(value: T): Option<T> {\n return new SomeOption(value);\n}\n\n/**\n * Creates an empty `Option`.\n *\n * @example\n * ```ts\n * const opt = None<number>();\n * opt.isNone(); // true\n * ```\n */\nexport function None<T>(): Option<T> {\n return new NoneOption<T>();\n}\n","/**\n * Represents the outcome of a fallible operation: either `Ok(value)` or `Fail(error)`.\n *\n * Use {@link Ok} and {@link Fail} factory functions to create instances.\n *\n * @typeParam T - The type of the success value.\n * @typeParam E - The type of the error value. Defaults to `string`.\n *\n * @example\n * ```ts\n * const parsed = Result.fromThrowable(() => JSON.parse(input));\n * const value = parsed.unwrapOr({});\n * ```\n */\nexport abstract class Result<T, E = string> {\n /** Returns `true` if the result is `Ok`. */\n abstract isOk(): boolean;\n\n /** Returns `true` if the result is `Err`. */\n abstract isErr(): boolean;\n\n /**\n * Returns the success value.\n * @throws If the result is `Err`.\n */\n abstract unwrap(): T;\n\n /**\n * Returns the error value.\n * @throws If the result is `Ok`.\n */\n abstract unwrapErr(): E;\n\n /**\n * Returns the success value.\n * @param msg - Error message to throw if the result is `Err`.\n * @throws With `msg` if the result is `Err`.\n */\n abstract expect(msg: string): T;\n\n /**\n * Returns the error value.\n * @param msg - Error message to throw if the result is `Ok`.\n * @throws With `msg` if the result is `Ok`.\n */\n abstract expectErr(msg: string): E;\n\n /**\n * Returns the success value, or `defaultValue` if `Err`.\n * @param defaultValue - Value to return if `Err`.\n */\n abstract unwrapOr(defaultValue: T): T;\n\n /**\n * Returns the success value, or computes it from the error using `f`.\n * @param f - Function to compute the default from the error.\n */\n abstract unwrapOrElse(f: (error: E) => T): T;\n\n /**\n * Transforms the success value by applying `f`.\n * Returns the original `Err` if the result is `Err`.\n * @param f - Function to apply to the success value.\n */\n abstract map<U>(f: (value: T) => U): Result<U, E>;\n\n /**\n * Transforms the error value by applying `f`.\n * Returns the original `Ok` if the result is `Ok`.\n * @param f - Function to apply to the error value.\n */\n abstract mapErr<F>(f: (error: E) => F): Result<T, F>;\n\n /**\n * Applies `f` to the success value, or returns `defaultValue` if `Err`.\n * @param defaultValue - Value to return if `Err`.\n * @param f - Function to apply to the success value.\n */\n abstract mapOr<U>(defaultValue: U, f: (value: T) => U): U;\n\n /**\n * Applies `f` to the success value, or `defaultFn` to the error.\n * @param defaultFn - Function to apply to the error.\n * @param f - Function to apply to the success value.\n */\n abstract mapOrElse<U>(defaultFn: (error: E) => U, f: (value: T) => U): U;\n\n /**\n * Transforms the success value by applying `f`, which itself returns a `Result`.\n * Flattens the result (avoids `Result<Result<U, E>, E>`).\n * @param f - Function returning a `Result`.\n */\n abstract flatMap<U>(f: (value: T) => Result<U, E>): Result<U, E>;\n\n /**\n * Returns `Err` if this result is `Err`, otherwise returns `res`.\n * @param res - The other result.\n */\n abstract and<U>(res: Result<U, E>): Result<U, E>;\n\n /**\n * Returns this result if it is `Ok`, otherwise returns `res`.\n * @param res - Fallback result.\n */\n abstract or<F>(res: Result<T, F>): Result<T, F>;\n\n /**\n * Returns this result if it is `Ok`, otherwise calls `f` with the error and returns the result.\n * @param f - Function producing the fallback result from the error.\n */\n abstract orElse<F>(f: (error: E) => Result<T, F>): Result<T, F>;\n\n /**\n * Calls `f` with the success value if `Ok`, then returns `this` unchanged.\n * Useful for side effects in a chain.\n * @param f - Function to call with the success value.\n */\n abstract inspect(f: (value: T) => void): Result<T, E>;\n\n /**\n * Calls `f` with the error value if `Err`, then returns `this` unchanged.\n * Useful for side effects in a chain.\n * @param f - Function to call with the error value.\n */\n abstract inspectErr(f: (error: E) => void): Result<T, E>;\n\n /**\n * Exhaustive pattern matching on the result.\n * @param patterns - Object with `Ok` and `Err` handlers.\n *\n * @example\n * ```ts\n * Ok(42).match({\n * Ok: v => `got ${v}`,\n * Err: e => `error: ${e}`,\n * });\n * ```\n */\n abstract match<U>(patterns: { Ok: (value: T) => U; Err: (error: E) => U }): U;\n\n /**\n * Wraps a potentially throwing function into a `Result`.\n * Returns `Ok(value)` on success, `Fail(error)` on exception.\n * Non-`Error` exceptions are wrapped in `new Error(String(e))`.\n * @param f - Function that may throw.\n */\n static fromThrowable<T>(f: () => T): Result<T, Error> {\n try {\n return Ok(f());\n } catch (e) {\n return Fail(e instanceof Error ? e : new Error(String(e)));\n }\n }\n\n /**\n * Collects an array of results into a single result.\n * Returns `Ok(values[])` if all succeed, or the first `Err` encountered.\n * @param results - Array of results to combine.\n *\n * @example\n * ```ts\n * Result.combine([Ok(1), Ok(2), Ok(3)]); // Ok([1, 2, 3])\n * Result.combine([Ok(1), Fail('no')]); // Fail('no')\n * ```\n */\n static combine<E = string>(results: Result<unknown, E>[]): Result<unknown[], E> {\n const values: unknown[] = [];\n for (const result of results) {\n if (result.isErr()) return Fail(result.unwrapErr());\n values.push(result.unwrap());\n }\n return Ok(values);\n }\n}\n\nclass OkResult<T, E = string> extends Result<T, E> {\n constructor(private readonly value: T) {\n super();\n }\n\n isOk(): boolean {\n return true;\n }\n\n isErr(): boolean {\n return false;\n }\n\n unwrap(): T {\n return this.value;\n }\n\n unwrapErr(): E {\n throw new Error('Called unwrapErr on an Ok value');\n }\n\n expect(_msg: string): T {\n return this.value;\n }\n\n expectErr(msg: string): E {\n throw new Error(msg);\n }\n\n unwrapOr(_defaultValue: T): T {\n return this.value;\n }\n\n unwrapOrElse(_f: (error: E) => T): T {\n return this.value;\n }\n\n map<U>(f: (value: T) => U): Result<U, E> {\n return Ok(f(this.value));\n }\n\n mapErr<F>(_f: (error: E) => F): Result<T, F> {\n return Ok(this.value);\n }\n\n mapOr<U>(_defaultValue: U, f: (value: T) => U): U {\n return f(this.value);\n }\n\n mapOrElse<U>(_defaultFn: (error: E) => U, f: (value: T) => U): U {\n return f(this.value);\n }\n\n flatMap<U>(f: (value: T) => Result<U, E>): Result<U, E> {\n return f(this.value);\n }\n\n and<U>(res: Result<U, E>): Result<U, E> {\n return res;\n }\n\n or<F>(_res: Result<T, F>): Result<T, F> {\n return Ok(this.value);\n }\n\n orElse<F>(_f: (error: E) => Result<T, F>): Result<T, F> {\n return Ok(this.value);\n }\n\n inspect(f: (value: T) => void): Result<T, E> {\n f(this.value);\n return this;\n }\n\n inspectErr(_f: (error: E) => void): Result<T, E> {\n return this;\n }\n\n match<U>(patterns: { Ok: (value: T) => U; Err: (error: E) => U }): U {\n return patterns.Ok(this.value);\n }\n\n toString(): string {\n return `Ok(${this.value})`;\n }\n}\n\nclass ErrResult<T, E = string> extends Result<T, E> {\n constructor(private readonly error: E) {\n super();\n }\n\n isOk(): boolean {\n return false;\n }\n\n isErr(): boolean {\n return true;\n }\n\n unwrap(): T {\n throw new Error(`Called unwrap on an Err value: ${this.error}`);\n }\n\n unwrapErr(): E {\n return this.error;\n }\n\n expect(msg: string): T {\n throw new Error(msg);\n }\n\n expectErr(_msg: string): E {\n return this.error;\n }\n\n unwrapOr(defaultValue: T): T {\n return defaultValue;\n }\n\n unwrapOrElse(f: (error: E) => T): T {\n return f(this.error);\n }\n\n map<U>(_f: (value: T) => U): Result<U, E> {\n return Fail(this.error);\n }\n\n mapErr<F>(f: (error: E) => F): Result<T, F> {\n return Fail(f(this.error));\n }\n\n mapOr<U>(defaultValue: U, _f: (value: T) => U): U {\n return defaultValue;\n }\n\n mapOrElse<U>(defaultFn: (error: E) => U, _f: (value: T) => U): U {\n return defaultFn(this.error);\n }\n\n flatMap<U>(_f: (value: T) => Result<U, E>): Result<U, E> {\n return Fail(this.error);\n }\n\n and<U>(_res: Result<U, E>): Result<U, E> {\n return Fail(this.error);\n }\n\n or<F>(res: Result<T, F>): Result<T, F> {\n return res;\n }\n\n orElse<F>(f: (error: E) => Result<T, F>): Result<T, F> {\n return f(this.error);\n }\n\n inspect(_f: (value: T) => void): Result<T, E> {\n return this;\n }\n\n inspectErr(f: (error: E) => void): Result<T, E> {\n f(this.error);\n return this;\n }\n\n match<U>(patterns: { Ok: (value: T) => U; Err: (error: E) => U }): U {\n return patterns.Err(this.error);\n }\n\n toString(): string {\n return `Err(${this.error})`;\n }\n}\n\n/**\n * Creates a successful `Result` containing a value.\n * @param value - The success value to wrap.\n *\n * @example\n * ```ts\n * const res = Ok(42);\n * res.unwrap(); // 42\n * ```\n */\nexport function Ok<T, E = string>(value: T): Result<T, E> {\n return new OkResult(value);\n}\n\n/**\n * Creates a failed `Result` containing an error.\n * @param error - The error value to wrap.\n *\n * @example\n * ```ts\n * const res = Fail('not found');\n * res.unwrapErr(); // 'not found'\n * ```\n */\nexport function Fail<T = never, E = string>(error: E): Result<T, E> {\n return new ErrResult(error);\n}\n"],"mappings":"AAqBA,SAAgB,EACd,EACA,EACS,CACT,OAAO,EAAM,MAAM,EAAkB,CCZvC,IAAsB,EAAtB,KAAgC,CAkH9B,OAAO,aAAgB,EAAwC,CAC7D,OAAO,GAAS,KAAqB,GAAS,CAAvB,EAAK,EAAM,GAIhC,EAAN,cAA4B,CAAU,CACpC,YAAY,EAA2B,CACrC,OAAO,CADoB,KAAA,MAAA,EAI7B,QAAkB,CAChB,MAAO,GAGT,QAAkB,CAChB,MAAO,GAGT,QAAY,CACV,OAAO,KAAK,MAGd,OAAO,EAAiB,CACtB,OAAO,KAAK,MAGd,SAAS,EAAqB,CAC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KAAK,MAAM,CACN,KAGT,MAAS,EAA4D,CACnE,OAAO,EAAS,IAAI,KAAK,MAAM,CAGjC,UAAmB,CACjB,MAAO,OAAO,KAAK,MAAM,KAc7B,SAAgB,EAAkB,EAAwB,CACxD,OAAO,IAAI,EAAS,EAAM,CAa5B,SAAgB,EAA4B,EAAwB,CAClE,OAAO,IAAI,EAAU,EAAM"}
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "galvorn",
3
+ "version": "1.0.1",
4
+ "description": "Rust's Option and Result for TypeScript. Fully typed, tested, zero dependencies.",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "author": "axelhamil",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/axelhamil/galvorn.git"
11
+ },
12
+ "bugs": {
13
+ "url": "https://github.com/axelhamil/galvorn/issues"
14
+ },
15
+ "homepage": "https://github.com/axelhamil/galvorn#readme",
16
+ "packageManager": "pnpm@10.26.0",
17
+ "engines": {
18
+ "node": ">=20"
19
+ },
20
+ "files": [
21
+ "dist"
22
+ ],
23
+ "main": "./dist/index.js",
24
+ "module": "./dist/index.js",
25
+ "types": "./dist/index.d.ts",
26
+ "exports": {
27
+ ".": {
28
+ "types": "./dist/index.d.ts",
29
+ "import": "./dist/index.js"
30
+ }
31
+ },
32
+ "scripts": {
33
+ "build": "tsdown",
34
+ "test": "vitest run",
35
+ "test:watch": "vitest",
36
+ "test:coverage": "vitest run --coverage",
37
+ "typecheck": "tsc --noEmit",
38
+ "lint": "biome check .",
39
+ "lint:fix": "biome check --fix ."
40
+ },
41
+ "devDependencies": {
42
+ "@biomejs/biome": "^2.3.14",
43
+ "@semantic-release/changelog": "^6.0.3",
44
+ "@semantic-release/git": "^10.0.1",
45
+ "@semantic-release/github": "^12.0.6",
46
+ "@vitest/coverage-v8": "^3.2.1",
47
+ "publint": "^0.3.17",
48
+ "semantic-release": "^25.0.3",
49
+ "tsdown": "^0.12.5",
50
+ "typescript": "^5.8.3",
51
+ "vitest": "^3.2.1"
52
+ }
53
+ }