functype 1.27.2 → 2.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 +63 -88
- package/dist/_virtual/_rolldown/runtime.js +1 -0
- package/dist/branded/ValidatedBrand.d.ts +122 -0
- package/dist/branded/ValidatedBrand.js +1 -0
- package/dist/branded/index.d.ts +52 -2
- package/dist/cli/data.d.ts +37 -0
- package/dist/cli/data.js +1 -0
- package/dist/cli/exports.d.ts +5 -46
- package/dist/cli/exports.js +1 -1
- package/dist/cli/formatters.js +4 -0
- package/dist/cli/full-interfaces.d.ts +6 -0
- package/dist/cli/full-interfaces.js +1139 -0
- package/dist/cli/index.js +3 -6
- package/dist/cli/interfaces.generated.js +1 -0
- package/dist/collections/index.d.ts +13 -0
- package/dist/companion/Companion.d.ts +23 -0
- package/dist/companion/Companion.js +1 -0
- package/dist/companion/CompanionTypes.d.ts +30 -0
- package/dist/{CompanionTypes-BVqO7Kc2.js → companion/CompanionTypes.js} +1 -1
- package/dist/companion/index.d.ts +2 -1
- package/dist/companion/index.js +1 -1
- package/dist/conditional/Cond.d.ts +147 -0
- package/dist/conditional/Cond.js +1 -0
- package/dist/conditional/Match.d.ts +295 -0
- package/dist/conditional/Match.js +1 -0
- package/dist/conditional/index.d.ts +3 -2
- package/dist/conditional/index.js +1 -1
- package/dist/core/base/Base.d.ts +12 -0
- package/dist/core/base/Base.js +1 -0
- package/dist/core/throwable/Throwable.d.ts +38 -0
- package/dist/core/throwable/Throwable.js +1 -0
- package/dist/decoder/Decoder.d.ts +15 -0
- package/dist/decoder/DecoderCompanion.js +1 -0
- package/dist/decoder/DecoderError.d.ts +56 -0
- package/dist/decoder/DecoderError.js +2 -0
- package/dist/decoder/Encoder.d.ts +15 -0
- package/dist/decoder/index.d.ts +71 -2
- package/dist/decoder/index.js +1 -1
- package/dist/decoder/tagged.js +1 -0
- package/dist/do/index.d.ts +246 -2
- package/dist/do/index.js +1 -1
- package/dist/do/protocol.d.ts +29 -0
- package/dist/either/index.d.ts +279 -2
- package/dist/either/index.js +1 -1
- package/dist/error/ErrorChain.d.ts +60 -0
- package/dist/error/ErrorChain.js +4 -0
- package/dist/error/ErrorFormatter.d.ts +67 -0
- package/dist/error/ErrorFormatter.js +6 -0
- package/dist/error/ParseError.d.ts +6 -0
- package/dist/error/ParseError.js +1 -0
- package/dist/error/typed/TypedError.d.ts +146 -0
- package/dist/error/typed/TypedError.js +1 -0
- package/dist/error/typed/Validation.d.ts +76 -0
- package/dist/error/typed/Validation.js +1 -0
- package/dist/extractable/Extractable.d.ts +62 -0
- package/dist/extractable/Extractable.js +1 -0
- package/dist/fetch/Http.d.ts +32 -0
- package/dist/fetch/Http.js +1 -0
- package/dist/fetch/HttpClient.d.ts +80 -0
- package/dist/fetch/HttpClient.js +1 -0
- package/dist/fetch/HttpError.d.ts +57 -0
- package/dist/fetch/HttpError.js +1 -0
- package/dist/fetch/HttpRequest.d.ts +103 -0
- package/dist/fetch/index.d.ts +5 -2
- package/dist/fetch/index.js +1 -1
- package/dist/foldable/Foldable.d.ts +27 -0
- package/dist/foldable/index.d.ts +55 -0
- package/dist/foldable/index.js +1 -0
- package/dist/functype/Functype.d.ts +76 -0
- package/dist/functype/FunctypeSum.d.ts +39 -0
- package/dist/functype/index.d.ts +4 -2
- package/dist/hkt/index.d.ts +68 -0
- package/dist/hkt/index.js +1 -0
- package/dist/identity/Identity.d.ts +34 -0
- package/dist/identity/Identity.js +1 -0
- package/dist/index.d.ts +74 -8
- package/dist/index.js +1 -1
- package/dist/internal/base64.js +1 -0
- package/dist/internal/call-alone.js +1 -0
- package/dist/internal/concurrency.d.ts +11 -0
- package/dist/internal/concurrency.js +1 -0
- package/dist/internal/define.js +1 -0
- package/dist/internal/mutation-utils.js +1 -0
- package/dist/internal/signal.js +1 -0
- package/dist/internal/stringify.js +1 -0
- package/dist/invariant/index.d.ts +32 -2
- package/dist/io/Context.d.ts +112 -0
- package/dist/io/Context.js +1 -0
- package/dist/io/Exit.d.ts +249 -0
- package/dist/io/Exit.js +1 -0
- package/dist/io/IO.d.ts +1243 -0
- package/dist/io/IO.js +1 -0
- package/dist/io/Layer.d.ts +139 -0
- package/dist/io/Layer.js +1 -0
- package/dist/io/SpanTracer.d.ts +78 -0
- package/dist/io/SpanTracer.js +1 -0
- package/dist/io/Tag.d.ts +75 -0
- package/dist/io/Tag.js +1 -0
- package/dist/io/TestClock.d.ts +133 -0
- package/dist/io/TestClock.js +1 -0
- package/dist/io/index.d.ts +41 -2
- package/dist/io/index.js +1 -1
- package/dist/lazy/Lazy.d.ts +312 -0
- package/dist/lazy/Lazy.js +1 -0
- package/dist/lazy/index.d.ts +2 -2
- package/dist/lazy/index.js +1 -1
- package/dist/list/LazyList.d.ts +182 -0
- package/dist/list/LazyList.js +1 -0
- package/dist/list/index.d.ts +137 -2
- package/dist/list/index.js +1 -1
- package/dist/logger/index.d.ts +24 -2
- package/dist/map/index.d.ts +95 -2
- package/dist/map/index.js +1 -1
- package/dist/map/shim.d.ts +13 -0
- package/dist/map/shim.js +1 -0
- package/dist/matchable/Matchable.d.ts +38 -0
- package/dist/matchable/Matchable.js +1 -0
- package/dist/obj/Obj.d.ts +212 -0
- package/dist/obj/Obj.js +1 -0
- package/dist/obj/index.d.ts +2 -2
- package/dist/obj/index.js +1 -1
- package/dist/option/index.d.ts +304 -2
- package/dist/option/index.js +1 -1
- package/dist/package.json.js +1 -0
- package/dist/pipe/index.d.ts +14 -0
- package/dist/ref/Ref.d.ts +77 -0
- package/dist/ref/Ref.js +1 -0
- package/dist/reshapeable/Reshapeable.d.ts +90 -0
- package/dist/serializable/Serializable.d.ts +39 -0
- package/dist/serialization/Serialization.d.ts +147 -0
- package/dist/serialization/Serialization.js +1 -0
- package/dist/serialization/SerializationCompanion.d.ts +125 -0
- package/dist/serialization/SerializationCompanion.js +1 -0
- package/dist/serialization/error-envelope.d.ts +44 -0
- package/dist/serialization/error-envelope.js +1 -0
- package/dist/serialization/index.d.ts +4 -2
- package/dist/serialization/index.js +1 -1
- package/dist/set/index.d.ts +106 -2
- package/dist/set/index.js +1 -1
- package/dist/stack/Stack.d.ts +120 -0
- package/dist/stack/Stack.js +1 -0
- package/dist/traced/TraceEvent.d.ts +27 -0
- package/dist/traced/TracedOption.d.ts +46 -0
- package/dist/traced/TracedOption.js +1 -0
- package/dist/traced/Tracer.d.ts +26 -0
- package/dist/traversable/KVTraversable.d.ts +14 -0
- package/dist/traversable/Traversable.d.ts +30 -0
- package/dist/try/index.d.ts +238 -2
- package/dist/try/index.js +1 -1
- package/dist/tuple/index.d.ts +92 -2
- package/dist/tuple/index.js +1 -1
- package/dist/typeable/Typeable.d.ts +40 -0
- package/dist/typeable/Typeable.js +1 -0
- package/dist/typeclass/ContainerOps.d.ts +131 -0
- package/dist/typeclass/Functor.d.ts +41 -0
- package/dist/typeclass/Promisable.d.ts +33 -0
- package/dist/typeclass/index.d.ts +4 -1
- package/dist/typeclass/index.js +1 -1
- package/dist/typeclass/variance.d.ts +54 -0
- package/dist/typeclass/variance.js +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/util/index.d.ts +1 -3
- package/dist/valuable/Valuable.d.ts +28 -0
- package/dist/valuable/Valuable.js +1 -0
- package/dist/wire/index.d.ts +40 -2
- package/package.json +7 -12
- package/dist/Brand-bfnGXuum.d.ts +0 -54
- package/dist/Companion-DiOMBHDG.js +0 -1
- package/dist/CompanionTypes-Cc90cQ4P.d.ts +0 -58
- package/dist/Invariant-DEan-SxV.d.ts +0 -35
- package/dist/Logger-BmprRS8v.d.ts +0 -27
- package/dist/Tuple-B8KM-H8Y.d.ts +0 -213
- package/dist/Wire-3hvw0Ogg.d.ts +0 -42
- package/dist/core/task/index.d.ts +0 -2
- package/dist/core/task/index.js +0 -1
- package/dist/full-interfaces-BadxJrCt.js +0 -1202
- package/dist/index-CLYh2qZu.d.ts +0 -6649
- package/dist/rolldown-runtime-DK3Fl9T5.js +0 -1
- package/dist/serialization-9_SyrfdG.js +0 -3
- package/dist/src-BQPeS6p2.js +0 -10
- package/dist/typeclass-C61yDVYd.js +0 -1
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Either } from "../either/index.js";
|
|
2
|
+
import { DecoderError } from "./DecoderError.js";
|
|
3
|
+
/**
|
|
4
|
+
* A `Decoder<A>` converts an `unknown` value into either a typed `A` or a
|
|
5
|
+
* structural `DecoderError`. The shape `(raw) => Either<DecoderError, A>` is
|
|
6
|
+
* the canonical FP decoder contract (cf. circe `Decoder`, fp-ts `Decoder`,
|
|
7
|
+
* Effect-TS `Schema.decodeUnknownEither`, Elm `Json.Decode.Decoder`).
|
|
8
|
+
*
|
|
9
|
+
* Any function matching this signature IS a decoder — there is no plugin
|
|
10
|
+
* registration. Zod / TypeBox / Valibot / AJV / hand-rolled adapters are
|
|
11
|
+
* ~15 lines each. The bundled combinators in `DecoderCompanion` cover the
|
|
12
|
+
* functype-aware cases (Option, Either, List, etc.) that a generic schema
|
|
13
|
+
* library cannot express.
|
|
14
|
+
*/
|
|
15
|
+
export type Decoder<A> = (raw: unknown) => Either<DecoderError, A>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{Left as e,Right as t}from"../either/index.js";import{List as n}from"../list/index.js";import{Map as r}from"../map/index.js";import{None as i,Some as a}from"../option/index.js";import{DecoderError as o}from"./DecoderError.js";const s=n=>typeof n==`string`?t(n):e(o.leaf([],`expected string`,{received:n})),c=n=>typeof n==`number`?t(n):e(o.leaf([],`expected number`,{received:n})),l=n=>typeof n==`boolean`?t(n):e(o.leaf([],`expected boolean`,{received:n})),u=e=>t(e),d=n=>r=>{try{return t(n(r))}catch(t){return e(o.leaf([],t instanceof Error?t.message:String(t),t))}},f=e=>n=>n==null?t(null):e(n),p=n=>r=>r==null?t(i()):n(r).fold(t=>e(t),e=>t(a(e))),m={envelope:n=>r=>{if(typeof r!=`object`||!r)return e(o.leaf([],`expected envelope object {ok|err}`,{received:r}));let i=r;return`ok`in i?n.ok(i.ok).fold(t=>e(o.prepend(`ok`,t)),e=>t(t(e))):`err`in i?n.err(i.err).fold(t=>e(o.prepend(`err`,t)),n=>t(e(n))):e(o.leaf([],`expected envelope to have 'ok' or 'err' key`,{received:r}))},discriminated:(n,r,i)=>a=>{if(typeof a!=`object`||!a)return e(o.leaf([],`expected object with '${n.tag}' tag`,{received:a}));let s=a[n.tag];return s===n.rightTag?i(a).fold(t=>e(t),e=>t(t(e))):s===n.leftTag?r(a).fold(t=>e(t),n=>t(e(n))):e(o.leaf([],`expected '${n.tag}' to be '${n.rightTag}' or '${n.leftTag}'`,{received:s}))}},h=t=>r=>{if(!Array.isArray(r))return e(o.leaf([],`expected array`,{received:r}));let i=[],a=[];return r.forEach((e,n)=>{t(e).fold(e=>{i.push(o.prepend(`[${n}]`,e))},e=>{a.push(e)})}),y(i,()=>n(a))},g=t=>n=>{if(!Array.isArray(n))return e(o.leaf([],`expected array`,{received:n}));let r=[],i=[];return n.forEach((e,n)=>{t(e).fold(e=>{r.push(o.prepend(`[${n}]`,e))},e=>{i.push(e)})}),y(r,()=>i)},_=t=>n=>{if(typeof n!=`object`||!n||Array.isArray(n))return e(o.leaf([],`expected object (for Map)`,{received:n}));let i=n,a=[],s=[];for(let e of Object.keys(i))t(i[e]).fold(t=>{a.push(o.prepend(e,t))},t=>{s.push([e,t])});return y(a,()=>r(s))},v=t=>n=>{if(typeof n!=`object`||!n||Array.isArray(n))return e(o.leaf([],`expected object`,{received:n}));let r=n,i=[],a={};for(let e in t)t[e](r[e]).fold(t=>{i.push(o.prepend(e,t))},t=>{a[e]=t});return y(i,()=>a)},y=(r,i)=>r.length===0?t(i()):r.length===1?e(r[0]):e(o.composite([],n(r))),b={string:s,number:c,boolean:l,unknown:u,fromThrowing:d,nullable:f,option:p,either:m,list:h,array:g,map:_,object:v};export{b as DecoderCompanion};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { List } from "../list/index.js";
|
|
2
|
+
/**
|
|
3
|
+
* Recursive decoder error.
|
|
4
|
+
*
|
|
5
|
+
* `Leaf` represents a single decode failure at some `path` in the input.
|
|
6
|
+
* `Composite` aggregates child failures (one per failed field of an object,
|
|
7
|
+
* one per failed element of a list, etc.) and mirrors the structural shape
|
|
8
|
+
* of the input — making it possible to render `user.name: expected string`
|
|
9
|
+
* alongside `user.age: expected number` from a single response.
|
|
10
|
+
*
|
|
11
|
+
* Accumulation lives in the data, not in the wrapper. Combinators in
|
|
12
|
+
* `DecoderCompanion` (object/list/map) produce `Composite` when more than
|
|
13
|
+
* one child decoder fails, unwrapping single-child composites back to a
|
|
14
|
+
* `Leaf` for cleaner error messages. The plain `Either<DecoderError, A>`
|
|
15
|
+
* return type of `Decoder<A>` keeps composition with the rest of the
|
|
16
|
+
* library uniform.
|
|
17
|
+
*
|
|
18
|
+
* Named `DecoderError` (not `DecodeError`) to avoid collision with the
|
|
19
|
+
* existing `HttpError.DecodeError` variant — these are at different layers:
|
|
20
|
+
* the HTTP variant is the outer wrapper, this is the structural inner cause.
|
|
21
|
+
*/
|
|
22
|
+
export type DecoderError = DecoderErrorLeaf | DecoderErrorComposite;
|
|
23
|
+
export type DecoderErrorLeaf = {
|
|
24
|
+
readonly _tag: "Leaf";
|
|
25
|
+
readonly path: ReadonlyArray<string>;
|
|
26
|
+
readonly message: string;
|
|
27
|
+
readonly cause?: unknown;
|
|
28
|
+
};
|
|
29
|
+
export type DecoderErrorComposite = {
|
|
30
|
+
readonly _tag: "Composite";
|
|
31
|
+
readonly path: ReadonlyArray<string>;
|
|
32
|
+
readonly children: List<DecoderError>;
|
|
33
|
+
};
|
|
34
|
+
declare const DecoderErrorStatics: {
|
|
35
|
+
leaf: (path: ReadonlyArray<string>, message: string, cause?: unknown) => DecoderErrorLeaf;
|
|
36
|
+
composite: (path: ReadonlyArray<string>, children: List<DecoderError>) => DecoderErrorComposite;
|
|
37
|
+
isLeaf: (e: DecoderError) => e is DecoderErrorLeaf;
|
|
38
|
+
isComposite: (e: DecoderError) => e is DecoderErrorComposite;
|
|
39
|
+
match: <T>(e: DecoderError, patterns: {
|
|
40
|
+
readonly Leaf: (e: DecoderErrorLeaf) => T;
|
|
41
|
+
readonly Composite: (e: DecoderErrorComposite) => T;
|
|
42
|
+
}) => T;
|
|
43
|
+
prepend: (segment: string, e: DecoderError) => DecoderError;
|
|
44
|
+
flatten: (e: DecoderError) => List<{
|
|
45
|
+
readonly path: ReadonlyArray<string>;
|
|
46
|
+
readonly message: string;
|
|
47
|
+
}>;
|
|
48
|
+
format: (e: DecoderError) => string;
|
|
49
|
+
};
|
|
50
|
+
export declare const DecoderError: DecoderErrorCompanion;
|
|
51
|
+
/** The type of `DecoderError`: its tag and its statics, named so that errors and hovers print `DecoderErrorCompanion`. */
|
|
52
|
+
export type DecoderErrorCompanion = {
|
|
53
|
+
readonly _tag: "DecoderError";
|
|
54
|
+
} & DecoderErrorStaticsType;
|
|
55
|
+
type DecoderErrorStaticsType = typeof DecoderErrorStatics;
|
|
56
|
+
export {};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{Companion as e}from"../companion/Companion.js";import{List as t}from"../list/index.js";const n=(e,t,n)=>({_tag:`Leaf`,path:e,message:t,cause:n}),r=(e,t)=>({_tag:`Composite`,path:e,children:t}),i=e=>e._tag===`Leaf`,a=e=>e._tag===`Composite`,o=(e,t)=>e._tag===`Leaf`?t.Leaf(e):t.Composite(e),s=(e,n)=>n._tag===`Leaf`?{...n,path:[e,...n.path]}:{...n,children:t(n.children.toArray().map(t=>s(e,t)))},c=e=>e._tag===`Leaf`?t([{path:e.path,message:e.message}]):t(e.children.toArray().flatMap(e=>c(e).toArray())),l=e=>c(e).toArray().map(({path:e,message:t})=>`${u(e)}: ${t}`).join(`
|
|
2
|
+
`),u=e=>e.length===0?`(root)`:e.reduce((e,t)=>t.startsWith(`[`)&&t.endsWith(`]`)?`${e}${t}`:e===``?t:`${e}.${t}`,``),d=e({_tag:`DecoderError`},{leaf:n,composite:r,isLeaf:i,isComposite:a,match:o,prepend:s,flatten:c,format:l});export{d as DecoderError};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { JSONValue } from "../serialization/Serialization.js";
|
|
2
|
+
/**
|
|
3
|
+
* An `Encoder<A>` turns a typed value into the JSON that goes on the wire — the request-side
|
|
4
|
+
* counterpart of {@link Decoder}. Any function with this shape is an encoder.
|
|
5
|
+
*
|
|
6
|
+
* Pass one as `encode` on an `Http` request when `flatten` can't express the wire shape you need:
|
|
7
|
+
* custom date formats, snake_case keys, stripping fields.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* const encodeUser: Encoder<User> = (u) => ({ user_name: u.name, created_at: u.createdAt.toISOString() })
|
|
12
|
+
* Http.post("/users", { body: user, encode: encodeUser })
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export type Encoder<A> = (value: A) => JSONValue;
|
package/dist/decoder/index.d.ts
CHANGED
|
@@ -1,2 +1,71 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { Decoder as Decoder$1 } from "./Decoder.js";
|
|
2
|
+
import { Try } from "../try/index.js";
|
|
3
|
+
import { List } from "../list/index.js";
|
|
4
|
+
import { Either } from "../either/index.js";
|
|
5
|
+
import { DecoderError, DecoderErrorCompanion, DecoderErrorComposite, DecoderErrorLeaf } from "./DecoderError.js";
|
|
6
|
+
import { Encoder } from "./Encoder.js";
|
|
7
|
+
import { Map } from "../map/index.js";
|
|
8
|
+
import { Option } from "../option/index.js";
|
|
9
|
+
/**
|
|
10
|
+
* A `Decoder<A>` converts an `unknown` value into either a typed `A` or a
|
|
11
|
+
* structural `DecoderError`. The shape `(raw) => Either<DecoderError, A>` is
|
|
12
|
+
* the canonical FP decoder contract (cf. circe `Decoder`, fp-ts `Decoder`,
|
|
13
|
+
* Effect-TS `Schema.decodeUnknownEither`, Elm `Json.Decode.Decoder`).
|
|
14
|
+
*
|
|
15
|
+
* Any function matching this signature IS a decoder — there is no plugin
|
|
16
|
+
* registration. Zod / TypeBox / Valibot / AJV / hand-rolled adapters are
|
|
17
|
+
* ~15 lines each. The bundled combinators in the `Decoder` value namespace
|
|
18
|
+
* cover the functype-aware cases (Option, Either, List, etc.) that a
|
|
19
|
+
* generic schema library cannot express.
|
|
20
|
+
*/
|
|
21
|
+
export type Decoder<A> = (raw: unknown) => Either<DecoderError, A>;
|
|
22
|
+
declare const DecoderStatics: {
|
|
23
|
+
tagged: {
|
|
24
|
+
option: <A>(inner: Decoder$1<A>) => Decoder$1<Option<A>>;
|
|
25
|
+
either: <L, R>(left: Decoder$1<L>, right: Decoder$1<R>) => Decoder$1<Either<L, R>>;
|
|
26
|
+
try: <A>(inner: Decoder$1<A>) => Decoder$1<Try<A>>;
|
|
27
|
+
list: <A>(inner: Decoder$1<A>) => Decoder$1<List<A>>;
|
|
28
|
+
map: <V>(inner: Decoder$1<V>) => Decoder$1<Map<string, V>>;
|
|
29
|
+
obj: <T extends Record<string, unknown>>(shape: { [K in keyof T]: Decoder$1<T[K]>; }) => Decoder$1<T>;
|
|
30
|
+
};
|
|
31
|
+
string: Decoder$1<string>;
|
|
32
|
+
number: Decoder$1<number>;
|
|
33
|
+
boolean: Decoder$1<boolean>;
|
|
34
|
+
unknown: Decoder$1<unknown>;
|
|
35
|
+
fromThrowing: <A>(parse: (raw: unknown) => A) => Decoder$1<A>;
|
|
36
|
+
nullable: <A>(inner: Decoder$1<A>) => Decoder$1<A | null>;
|
|
37
|
+
option: <A>(inner: Decoder$1<A>) => Decoder$1<Option<A>>;
|
|
38
|
+
either: {
|
|
39
|
+
envelope: <L, R>(shape: {
|
|
40
|
+
ok: Decoder$1<R>;
|
|
41
|
+
err: Decoder$1<L>;
|
|
42
|
+
}) => Decoder$1<Either<L, R>>;
|
|
43
|
+
discriminated: <L, R>(config: {
|
|
44
|
+
tag: string;
|
|
45
|
+
leftTag: string;
|
|
46
|
+
rightTag: string;
|
|
47
|
+
}, left: Decoder$1<L>, right: Decoder$1<R>) => Decoder$1<Either<L, R>>;
|
|
48
|
+
};
|
|
49
|
+
list: <A>(inner: Decoder$1<A>) => Decoder$1<List<A>>;
|
|
50
|
+
array: <A>(inner: Decoder$1<A>) => Decoder$1<A[]>;
|
|
51
|
+
map: <V>(inner: Decoder$1<V>) => Decoder$1<Map<string, V>>;
|
|
52
|
+
object: <T extends Record<string, unknown>>(shape: { [K in keyof T]: Decoder$1<T[K]>; }) => Decoder$1<T>;
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Decoder namespace: combinators for converting `unknown` into typed values.
|
|
56
|
+
*
|
|
57
|
+
* - `Decoder.string` / `.number` / `.boolean` / `.unknown` / `.nullable(inner)` — leaf primitives
|
|
58
|
+
* - `Decoder.fromThrowing(parse)` — adapt a throwing parser (Zod `.parse`); a throw becomes a leaf error
|
|
59
|
+
* - `Decoder.option(inner)` — null-bias Option (null → None, else inner → Some)
|
|
60
|
+
* - `Decoder.either.envelope({ok, err})` / `.discriminated({...}, l, r)` — Either variants
|
|
61
|
+
* - `Decoder.list(inner)` / `.array(inner)` / `.map(inner)` / `.object(shape)` — composites; accumulate child failures
|
|
62
|
+
* - `Decoder.tagged.option/either/try/list/map/obj(inner?)` — round-trip the `{_tag, value}` shape
|
|
63
|
+
* used by functype's built-in `.toJSON()` (for functype-to-functype services)
|
|
64
|
+
*/
|
|
65
|
+
export declare const Decoder: DecoderCompanion;
|
|
66
|
+
/** The type of `Decoder`: its tag and its statics, named so that errors and hovers print `DecoderCompanion`. */
|
|
67
|
+
export type DecoderCompanion = {
|
|
68
|
+
readonly _tag: "Decoder";
|
|
69
|
+
} & DecoderStaticsType;
|
|
70
|
+
type DecoderStaticsType = typeof DecoderStatics;
|
|
71
|
+
export { DecoderError, type DecoderErrorCompanion, type DecoderErrorComposite, type DecoderErrorLeaf, type Encoder };
|
package/dist/decoder/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{Companion as e}from"../companion/Companion.js";import{DecoderCompanion as t}from"./DecoderCompanion.js";import{tagged as n}from"./tagged.js";import{DecoderError as r}from"./DecoderError.js";const i={...t,tagged:n},a=e({_tag:`Decoder`},i);export{a as Decoder,r as DecoderError};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{Left as e,Right as t}from"../either/index.js";import{List as n}from"../list/index.js";import{Map as r}from"../map/index.js";import{None as i,Some as a}from"../option/index.js";import{Try as o}from"../try/index.js";import{DecoderError as s}from"./DecoderError.js";const c=(e,t)=>s.prepend(e,t),l=(e,t,n)=>s.prepend(e,s.prepend(`[${t}]`,n)),u={option:n=>r=>{if(typeof r!=`object`||!r)return e(s.leaf([],`expected tagged Option object`,{received:r}));let o=r;return o._tag===`None`?t(i()):o._tag===`Some`?n(o.value).fold(t=>e(c(`value`,t)),e=>t(a(e))):e(s.leaf([`_tag`],`expected 'Some' or 'None'`,{received:o._tag}))},either:(n,r)=>i=>{if(typeof i!=`object`||!i)return e(s.leaf([],`expected tagged Either object`,{received:i}));let a=i;return a._tag===`Right`?r(a.value).fold(t=>e(c(`value`,t)),e=>t(t(e))):a._tag===`Left`?n(a.value).fold(t=>e(c(`value`,t)),n=>t(e(n))):e(s.leaf([`_tag`],`expected 'Left' or 'Right'`,{received:a._tag}))},try:n=>r=>{if(typeof r!=`object`||!r)return e(s.leaf([],`expected tagged Try object`,{received:r}));let i=r;if(i._tag===`Success`)return n(i.value).fold(t=>e(c(`value`,t)),e=>t(o.success(e)));if(i._tag===`Failure`){let e=typeof i.error==`string`?i.error:`unknown error`,n=Error(e);return typeof i.stack==`string`&&(n.stack=i.stack),t(o.failure(n))}return e(s.leaf([`_tag`],`expected 'Success' or 'Failure'`,{received:i._tag}))},list:r=>i=>{if(typeof i!=`object`||!i)return e(s.leaf([],`expected tagged List object`,{received:i}));let a=i;if(a._tag!==`List`)return e(s.leaf([`_tag`],`expected 'List'`,{received:a._tag}));if(!Array.isArray(a.value))return e(s.leaf([`value`],`expected array`,{received:a.value}));let o=[],c=[];return a.value.forEach((e,t)=>{r(e).fold(e=>{o.push(l(`value`,t,e))},e=>{c.push(e)})}),o.length===0?t(n(c)):o.length===1?e(o[0]):e(s.composite([],n(o)))},map:i=>a=>{if(typeof a!=`object`||!a)return e(s.leaf([],`expected tagged Map object`,{received:a}));let o=a;if(o._tag!==`Map`)return e(s.leaf([`_tag`],`expected 'Map'`,{received:o._tag}));if(!Array.isArray(o.value))return e(s.leaf([`value`],`expected entries array`,{received:o.value}));let c=[],l=[];return o.value.forEach((e,t)=>{if(!Array.isArray(e)||e.length!==2){c.push(s.leaf([`value`,`[${t}]`],`expected [key, value] tuple`,{received:e}));return}let[n,r]=e;typeof n==`string`?i(r).fold(e=>{c.push(s.prepend(`value`,s.prepend(`[${t}]`,s.prepend(`[1]`,e))))},e=>{l.push([n,e])}):c.push(s.leaf([`value`,`[${t}]`,`[0]`],`expected string key`,{received:n}))}),c.length===0?t(r(l)):c.length===1?e(c[0]):e(s.composite([],n(c)))},obj:r=>i=>{if(typeof i!=`object`||!i)return e(s.leaf([],`expected tagged Obj object`,{received:i}));let a=i;if(a._tag!==`Obj`)return e(s.leaf([`_tag`],`expected 'Obj'`,{received:a._tag}));if(typeof a.value!=`object`||a.value===null)return e(s.leaf([`value`],`expected object`,{received:a.value}));let o=a.value,c=[],l={};for(let e in r)r[e](o[e]).fold(t=>{c.push(s.prepend(`value`,s.prepend(e,t)))},t=>{l[e]=t});return c.length===0?t(l):c.length===1?e(c[0]):e(s.composite([],n(c)))}};export{u as tagged};
|
package/dist/do/index.d.ts
CHANGED
|
@@ -1,2 +1,246 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Generator-based Do-notation for monadic comprehensions
|
|
3
|
+
* Provides Scala-like for-comprehension syntax using JavaScript generators
|
|
4
|
+
*
|
|
5
|
+
* ## Scala Equivalents
|
|
6
|
+
*
|
|
7
|
+
* Functype: `const x = yield* $(Option(5))`
|
|
8
|
+
* Scala: `x <- Some(5)`
|
|
9
|
+
*
|
|
10
|
+
* Functype: `return x + y`
|
|
11
|
+
* Scala: `yield x + y`
|
|
12
|
+
*
|
|
13
|
+
* ## Core Concepts
|
|
14
|
+
*
|
|
15
|
+
* - **Generators**: Use `yield* $(monad)` to extract values from monads
|
|
16
|
+
* - **Short-circuiting**: None/Left/Failure automatically propagates
|
|
17
|
+
* - **Type inference**: The $ helper provides proper TypeScript types
|
|
18
|
+
* - **First monad wins**: Return type matches the first yielded monad
|
|
19
|
+
* - **Cartesian products**: Multiple List yields create all combinations
|
|
20
|
+
*
|
|
21
|
+
* ## Usage Rules
|
|
22
|
+
*
|
|
23
|
+
* 1. All yielded values MUST be monadic (Option, Either, List, Try)
|
|
24
|
+
* 2. Use the $ helper for type inference: `yield* $(Option(value))`
|
|
25
|
+
* 3. Raw values should be assigned directly without yielding
|
|
26
|
+
* 4. Mixed monad types are supported via Reshapeable interface
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* // Basic Option chaining (Scala: for { x <- Some(5); y <- Some(10) } yield x + y)
|
|
30
|
+
* const result = Do(function* () {
|
|
31
|
+
* const x = yield* $(Option(5)) // Extract from Option
|
|
32
|
+
* const y = yield* $(Option(10)) // Extract from another Option
|
|
33
|
+
* return x + y // Return final value
|
|
34
|
+
* })
|
|
35
|
+
* // result: Option<number> with value 15
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* // List comprehension (Scala: for { x <- List(1,2); y <- List(10,20) } yield (x,y))
|
|
39
|
+
* const pairs = Do(function* () {
|
|
40
|
+
* const x = yield* $(List([1, 2])) // Iterates: 1, 2
|
|
41
|
+
* const y = yield* $(List([10, 20])) // Iterates: 10, 20
|
|
42
|
+
* return { x, y } // All combinations
|
|
43
|
+
* })
|
|
44
|
+
* // pairs: List([{x:1,y:10}, {x:1,y:20}, {x:2,y:10}, {x:2,y:20}])
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* // Error propagation with Either
|
|
48
|
+
* const validate = Do(function* () {
|
|
49
|
+
* const email = yield* $(validateEmail(input)) // Either<string, Email>
|
|
50
|
+
* const user = yield* $(fetchUser(email)) // Either<string, User>
|
|
51
|
+
* const saved = yield* $(saveUser(user)) // Either<string, Result>
|
|
52
|
+
* return saved
|
|
53
|
+
* })
|
|
54
|
+
* // If any step returns Left, entire chain short-circuits with that error
|
|
55
|
+
*
|
|
56
|
+
* @see {@link https://github.com/jordanburke/functype/blob/main/docs/do-notation.md} Full documentation
|
|
57
|
+
* @module Do
|
|
58
|
+
*/
|
|
59
|
+
import { Either } from "../either/index.js";
|
|
60
|
+
import { List } from "../list/index.js";
|
|
61
|
+
import { Option } from "../option/index.js";
|
|
62
|
+
import { Try } from "../try/index.js";
|
|
63
|
+
import { DoResult, Doable } from "./protocol.js";
|
|
64
|
+
import { Reshapeable } from "../reshapeable/Reshapeable.js";
|
|
65
|
+
type OptionLike = {
|
|
66
|
+
_tag: "Some" | "None";
|
|
67
|
+
isSome(): boolean;
|
|
68
|
+
orThrow(): unknown;
|
|
69
|
+
doUnwrap(): unknown;
|
|
70
|
+
};
|
|
71
|
+
type EitherLike = {
|
|
72
|
+
_tag: "Left" | "Right";
|
|
73
|
+
isLeft(): boolean;
|
|
74
|
+
isRight(): boolean;
|
|
75
|
+
value: unknown;
|
|
76
|
+
};
|
|
77
|
+
type ListLike = {
|
|
78
|
+
_tag: "List";
|
|
79
|
+
toArray(): unknown[];
|
|
80
|
+
};
|
|
81
|
+
type TryLike = {
|
|
82
|
+
_tag: "Success" | "Failure";
|
|
83
|
+
isSuccess(): boolean;
|
|
84
|
+
orThrow(): unknown;
|
|
85
|
+
doUnwrap(): unknown;
|
|
86
|
+
};
|
|
87
|
+
/**
|
|
88
|
+
* Executes a generator-based monadic comprehension
|
|
89
|
+
* Returns the same monad type as the first yielded monad (Scala semantics)
|
|
90
|
+
*
|
|
91
|
+
* - Option comprehensions return Option (None on short-circuit)
|
|
92
|
+
* - Either comprehensions return Either (Left with error on short-circuit)
|
|
93
|
+
* - List comprehensions return List (empty or cartesian product)
|
|
94
|
+
* - Try comprehensions return Try (Failure with error on short-circuit)
|
|
95
|
+
*
|
|
96
|
+
* Type Inference Notes:
|
|
97
|
+
* - TypeScript infers the correct return type for homogeneous comprehensions
|
|
98
|
+
* - For mixed monad types, TypeScript returns a union type
|
|
99
|
+
* - Use a type assertion for a mix that includes Option or Try
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```typescript
|
|
103
|
+
* // Option comprehension returns Option:
|
|
104
|
+
* const result = Do(function* () {
|
|
105
|
+
* const x = yield* $(Option(5));
|
|
106
|
+
* const y = yield* $(Option(10));
|
|
107
|
+
* return x + y;
|
|
108
|
+
* });
|
|
109
|
+
* // result: Option(15)
|
|
110
|
+
*
|
|
111
|
+
* // Either comprehension returns Either:
|
|
112
|
+
* const result = Do(function* () {
|
|
113
|
+
* const x = yield* $(Right(5));
|
|
114
|
+
* const y = yield* $(Left("error"));
|
|
115
|
+
* return x + y;
|
|
116
|
+
* });
|
|
117
|
+
* // result: Left("error") - error is preserved
|
|
118
|
+
*
|
|
119
|
+
* // List comprehension returns List with cartesian product:
|
|
120
|
+
* const result = Do(function* () {
|
|
121
|
+
* const x = yield* $(List([1, 2]));
|
|
122
|
+
* const y = yield* $(List([3, 4]));
|
|
123
|
+
* return x + y;
|
|
124
|
+
* });
|
|
125
|
+
* // result: List([4, 5, 5, 6])
|
|
126
|
+
*
|
|
127
|
+
* // Mixed types - use a type assertion:
|
|
128
|
+
* const result = Do(function* () {
|
|
129
|
+
* const x = yield* $(Option(5));
|
|
130
|
+
* const y = yield* $(Right<string, number>(10));
|
|
131
|
+
* return x + y;
|
|
132
|
+
* }) as Option<number>;
|
|
133
|
+
* // result: Option(15)
|
|
134
|
+
* ```
|
|
135
|
+
*
|
|
136
|
+
* @param gen - Generator function that yields monads and returns a result
|
|
137
|
+
* @returns The same monad type as the first yield
|
|
138
|
+
*/
|
|
139
|
+
export declare function Do<T>(gen: () => Generator<never, T, unknown>): List<T>;
|
|
140
|
+
export declare function Do<T>(gen: () => Generator<OptionLike, T, unknown>): Option<T>;
|
|
141
|
+
export declare function Do<L, R>(gen: () => Generator<EitherLike, R, unknown>): Either<L, R>;
|
|
142
|
+
export declare function Do<T>(gen: () => Generator<ListLike, T, unknown>): List<T>;
|
|
143
|
+
export declare function Do<T>(gen: () => Generator<TryLike, T, unknown>): Try<T>;
|
|
144
|
+
export declare function Do<T>(gen: () => Generator<EitherLike | ListLike, T, unknown>): Reshapeable<T>;
|
|
145
|
+
export declare function Do<T>(gen: () => Generator<unknown, T, unknown>): unknown;
|
|
146
|
+
/**
|
|
147
|
+
* Executes an async generator-based monadic comprehension
|
|
148
|
+
* Returns the same monad type as the first yielded monad
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* ```typescript
|
|
152
|
+
* const result = await DoAsync(async function* () {
|
|
153
|
+
* const user = yield* $(await fetchUser(id)); // Promise<Option<User>> → User
|
|
154
|
+
* const profile = yield* $(await getProfile(user)); // Promise<Either<Error, Profile>> → Profile
|
|
155
|
+
* return { user, profile };
|
|
156
|
+
* });
|
|
157
|
+
* // result type matches first yield
|
|
158
|
+
* ```
|
|
159
|
+
*
|
|
160
|
+
* @param gen - Async generator function that yields monads/promises and returns a result
|
|
161
|
+
* @returns Promise of the same monad type as first yield
|
|
162
|
+
*/
|
|
163
|
+
export declare function DoAsync<T>(gen: () => AsyncGenerator<never, T, unknown>): Promise<List<T>>;
|
|
164
|
+
export declare function DoAsync<T>(gen: () => AsyncGenerator<OptionLike, T, unknown>): Promise<Option<T>>;
|
|
165
|
+
export declare function DoAsync<L, R>(gen: () => AsyncGenerator<EitherLike, R, unknown>): Promise<Either<L, R>>;
|
|
166
|
+
export declare function DoAsync<T>(gen: () => AsyncGenerator<ListLike, T, unknown>): Promise<List<T>>;
|
|
167
|
+
export declare function DoAsync<T>(gen: () => AsyncGenerator<TryLike, T, unknown>): Promise<Try<T>>;
|
|
168
|
+
export declare function DoAsync<T>(gen: () => AsyncGenerator<EitherLike | ListLike, T, unknown>): Promise<Reshapeable<T>>;
|
|
169
|
+
export declare function DoAsync<T>(gen: () => AsyncGenerator<unknown, T, unknown>): Promise<unknown>;
|
|
170
|
+
/**
|
|
171
|
+
* Helper function to check if a value implements the Doable interface
|
|
172
|
+
* @param value - Value to check
|
|
173
|
+
* @returns True if the value implements Doable
|
|
174
|
+
*/
|
|
175
|
+
export declare function isDoCapable<T>(value: unknown): value is Doable<T>;
|
|
176
|
+
/**
|
|
177
|
+
* Manually unwrap a monad using the Doable interface
|
|
178
|
+
* Useful for testing or when you need to unwrap outside of a Do-comprehension
|
|
179
|
+
*
|
|
180
|
+
* @param monad - Monad to unwrap
|
|
181
|
+
* @returns The unwrapped value
|
|
182
|
+
* @throws Error if the monad cannot be unwrapped
|
|
183
|
+
*/
|
|
184
|
+
export declare function unwrap<T>(monad: Doable<T>): T;
|
|
185
|
+
/**
|
|
186
|
+
* Type helper for Do-notation generators.
|
|
187
|
+
* Provides better type hints in IDEs.
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* ```typescript
|
|
191
|
+
* const result = Do(function* (): DoGenerator<number> {
|
|
192
|
+
* const x = yield* $(List([1, 2])) // x is still unknown but return type is clear
|
|
193
|
+
* const y = yield* $(List([3, 4]))
|
|
194
|
+
* return x + y
|
|
195
|
+
* })
|
|
196
|
+
* ```
|
|
197
|
+
*/
|
|
198
|
+
export type DoGenerator<T, TYield = unknown> = Generator<TYield, T, unknown>;
|
|
199
|
+
/**
|
|
200
|
+
* Extracts values from monads in Do-notation with type inference.
|
|
201
|
+
* The '$' symbol is the universal extraction operator in functional programming.
|
|
202
|
+
*
|
|
203
|
+
* @example
|
|
204
|
+
* ```typescript
|
|
205
|
+
* const result = Do(function* () {
|
|
206
|
+
* const x = yield* $(Option(5)) // x: number
|
|
207
|
+
* const y = yield* $(List([1, 2, 3])) // y: number (for cartesian product)
|
|
208
|
+
* const name = yield* $(Right("Alice")) // name: string
|
|
209
|
+
* return `${name}: ${x + y}`
|
|
210
|
+
* })
|
|
211
|
+
* ```
|
|
212
|
+
*
|
|
213
|
+
* @param monad - Any monad that can be unwrapped (Option, Either, List, Try, etc.)
|
|
214
|
+
* @returns A generator that yields the monad and returns its extracted value
|
|
215
|
+
*/
|
|
216
|
+
export declare function $<T>(monad: Option<T>): Generator<Option<T>, T, T>;
|
|
217
|
+
export declare function $<L, R>(monad: Either<L, R>): Generator<Either<L, R>, R, R>;
|
|
218
|
+
export declare function $<T>(monad: List<T>): Generator<List<T>, T, T>;
|
|
219
|
+
export declare function $<T>(monad: Try<T>): Generator<Try<T>, T, T>;
|
|
220
|
+
export declare function $<T>(monad: Doable<T>): Generator<Doable<T>, T, T>;
|
|
221
|
+
export declare function $<M>(monad: M): Generator<M, InferYieldType<M>, InferYieldType<M>>;
|
|
222
|
+
type InferYieldType<M> = M extends {
|
|
223
|
+
isSome(): boolean;
|
|
224
|
+
orThrow(): infer T;
|
|
225
|
+
doUnwrap(): unknown;
|
|
226
|
+
} ? T : M extends {
|
|
227
|
+
isRight(): boolean;
|
|
228
|
+
value: infer R;
|
|
229
|
+
} ? R : M extends {
|
|
230
|
+
toArray(): (infer T)[];
|
|
231
|
+
} ? T : M extends {
|
|
232
|
+
isSuccess(): boolean;
|
|
233
|
+
orThrow(): infer T;
|
|
234
|
+
doUnwrap(): unknown;
|
|
235
|
+
} ? T : M extends Doable<infer T> ? T : unknown;
|
|
236
|
+
export declare const NoneError: (message?: string) => Error;
|
|
237
|
+
export interface LeftErrorType<L> extends Error {
|
|
238
|
+
value: L;
|
|
239
|
+
}
|
|
240
|
+
export declare const LeftError: <L>(value: L, message?: string) => LeftErrorType<L>;
|
|
241
|
+
export declare const EmptyListError: (message?: string) => Error;
|
|
242
|
+
export interface FailureErrorType extends Error {
|
|
243
|
+
cause: Error;
|
|
244
|
+
}
|
|
245
|
+
export declare const FailureError: (cause: Error, message?: string) => FailureErrorType;
|
|
246
|
+
export type { DoResult, Doable };
|
package/dist/do/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{Left as e,Right as t}from"../either/index.js";import{List as n}from"../list/index.js";import{Option as r}from"../option/index.js";import{Try as i}from"../try/index.js";function a(e){if(!e||typeof e!=`object`||!(`_tag`in e))return`unknown`;switch(e._tag){case`Some`:case`None`:return`Option`;case`Left`:case`Right`:return`Either`;case`List`:return`List`;case`Success`:case`Failure`:return`Try`;default:return`unknown`}}const o={Option:{of:e=>r(e),empty:()=>r.none()},Either:{of:e=>t(e),empty:t=>e(t)},List:{of:e=>n([e]),empty:()=>n([])},Try:{of:e=>i(()=>e),empty:e=>i(()=>{throw e??/* @__PURE__ */ Error(`Try failed`)})}};function s(e){let t=e(),r=null,i;function s(l){let u=t.next(l);if(u.done)return i?i.of(u.value):n([u.value]);let d=u.value;if(typeof d!=`object`||!d)throw Error(`Do-notation error: All yielded values must be monadic. Use yield* $(Option(value)), yield* $(Right(value)), etc. Raw values should be assigned directly without yielding.`);if(!r&&`_tag`in d&&(r=a(d),r!==`unknown`&&r in o&&(i=o[r]),r===`List`))return c(e);if(`doUnwrap`in d){let e=d.doUnwrap();if(!e.ok){if(!i)return n([]);if(!e.empty&&`error`in e){if(r===`Either`)return i.empty(e.error);if(r===`Try`)return i.empty(e.error instanceof Error?e.error:Error(String(e.error)))}return i.empty()}return s(e.value)}throw Error(`Do-notation error: All yielded values must be monadic. Use yield* $(Option(value)), yield* $(Right(value)), etc. Raw values should be assigned directly without yielding.`)}return s()}function c(e){function t(e){if(typeof e!=`object`||!e||!(`doUnwrap`in e))return[e];let t=e;if(`toArray`in t){let e=t.toArray();return e.length===0?[]:e}let n=t.doUnwrap();return n.ok?[n.value]:[]}function r(n){let i=e(),a=[],o=[],s=0;function c(e){let l=i.next(e);if(l.done){a.push(l.value);return}if(s<n.length){let e=n[s];o.push(e),s++,c(e);return}let u=t(l.value);if(u.length!==0){if(u.length>1){let e=u.flatMap(e=>r([...o,e]));a.push(...e)}else o.push(u[0]),s++,c(u[0])}}return c(void 0),a}let i=r([]);return n(i)}async function l(e){let t=e(),r=null,i;async function s(e){let c=await t.next(e);if(c.done)return i?i.of(c.value):n([c.value]);let l=await Promise.resolve(c.value);if(typeof l!=`object`||!l)return s(l);if(!r&&`_tag`in l&&(r=a(l),r!==`unknown`&&r in o&&(i=o[r])),`doUnwrap`in l){let e=l.doUnwrap();if(!e.ok){if(!i)return n([]);if(!e.empty&&`error`in e){if(r===`Either`)return i.empty(e.error);if(r===`Try`)return i.empty(e.error instanceof Error?e.error:Error(String(e.error)))}return i.empty()}return s(e.value)}return s(l)}return s()}function u(e){return typeof e==`object`&&!!e&&`doUnwrap`in e&&typeof e.doUnwrap==`function`}function d(e){let t=e.doUnwrap();if(t.ok)return t.value;throw`error`in t?t.error:Error(`Cannot unwrap empty monad`)}function*f(e){return yield e}const p=(e=`Cannot unwrap None in Do-notation`)=>{let t=Error(e),n=Object.create(Error.prototype);return n.message=t.message,n.stack=t.stack,n.name=`NoneError`,n},m=(e,t=`Cannot unwrap Left in Do-notation`)=>{let n=Error(t),r=Object.create(Error.prototype);return r.message=n.message,r.stack=n.stack,r.name=`LeftError`,r.value=e,r},h=(e=`Cannot unwrap empty List in Do-notation`)=>{let t=Error(e),n=Object.create(Error.prototype);return n.message=t.message,n.stack=t.stack,n.name=`EmptyListError`,n},g=(e,t=`Cannot unwrap Failure in Do-notation`)=>{let n=Error(t),r=Object.create(Error.prototype);return r.message=n.message,r.stack=n.stack,r.name=`FailureError`,r.cause=e,r};export{f as $,s as Do,l as DoAsync,h as EmptyListError,g as FailureError,m as LeftError,p as NoneError,u as isDoCapable,d as unwrap};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Protocol definitions for Do-notation
|
|
3
|
+
* Separated from main Do module to avoid circular dependencies
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Result type for Do-notation unwrapping
|
|
7
|
+
* Indicates whether unwrapping succeeded and provides the value or error
|
|
8
|
+
*/
|
|
9
|
+
export type DoResult<T> = {
|
|
10
|
+
ok: true;
|
|
11
|
+
value: T;
|
|
12
|
+
} | {
|
|
13
|
+
ok: false;
|
|
14
|
+
empty: true;
|
|
15
|
+
} | {
|
|
16
|
+
ok: false;
|
|
17
|
+
empty: false;
|
|
18
|
+
error: unknown;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Interface for types that support Do-notation
|
|
22
|
+
* Implementing this interface allows a type to be yielded in Do-comprehensions
|
|
23
|
+
*
|
|
24
|
+
* The doUnwrap method should return a DoResult indicating success/failure
|
|
25
|
+
* and the contained value or error information
|
|
26
|
+
*/
|
|
27
|
+
export interface Doable<out T> {
|
|
28
|
+
doUnwrap(): DoResult<T>;
|
|
29
|
+
}
|