@carbonteq/fp 0.7.0 → 0.8.0-alpha.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/dist/option.js DELETED
@@ -1,204 +0,0 @@
1
- import {
2
- __name
3
- } from "./chunk-7QVYU63E.js";
4
- import { isPromise } from "node:util/types";
5
- import { UNIT } from "./unit.js";
6
- class UnwrappedNone extends Error {
7
- static {
8
- __name(this, "UnwrappedNone");
9
- }
10
- constructor() {
11
- super("Unwrapped Option<None>");
12
- }
13
- }
14
- const UNWRAPPED_NONE_ERR = new UnwrappedNone();
15
- const NONE_VAL = Symbol.for("None");
16
- class Option {
17
- static {
18
- __name(this, "Option");
19
- }
20
- val;
21
- constructor(val) {
22
- this.val = val;
23
- }
24
- static None = new Option(NONE_VAL);
25
- static UNIT_OPT = new Option(UNIT);
26
- static Some(val) {
27
- return new Option(val);
28
- }
29
- static fromNullable(val) {
30
- return val === null ? Option.None : Option.Some(val);
31
- }
32
- isSome() {
33
- return this.val !== NONE_VAL;
34
- }
35
- isNone() {
36
- return this.val === NONE_VAL;
37
- }
38
- isUnit() {
39
- return this.val === UNIT;
40
- }
41
- unwrap() {
42
- if (this.val !== NONE_VAL) {
43
- return this.val;
44
- }
45
- throw UNWRAPPED_NONE_ERR;
46
- }
47
- /* Useful to serialize. Inverse operation for `fromNullable` */
48
- safeUnwrap() {
49
- if (this.val !== NONE_VAL) return this.val;
50
- return null;
51
- }
52
- toString() {
53
- if (this.isNone()) return "Option::None";
54
- return `Option::Some(${this.val})`;
55
- }
56
- map(fn) {
57
- if (this.isNone()) return Option.None;
58
- const val = fn(this.val);
59
- if (isPromise(val)) return val.then((u) => Option.Some(u));
60
- return Option.Some(val);
61
- }
62
- mapOr(def, fn) {
63
- if (this.isNone()) return Option.Some(def);
64
- const val = fn(this.val);
65
- if (isPromise(val)) return val.then((u) => Option.Some(u));
66
- return Option.Some(val);
67
- }
68
- flatMap(fn) {
69
- if (this.val !== NONE_VAL) return fn(this.val);
70
- return Option.None;
71
- }
72
- bind(fn) {
73
- if (this.val !== NONE_VAL) return fn(this.val);
74
- return Option.None;
75
- }
76
- and(opt) {
77
- if (this.val !== NONE_VAL && opt.val !== NONE_VAL) return Option.Some([
78
- this.val,
79
- opt.val
80
- ]);
81
- return Option.None;
82
- }
83
- tap(fn) {
84
- if (this.val !== NONE_VAL) fn(this.val);
85
- return this;
86
- }
87
- async tapAsync(fn) {
88
- if (this.val !== NONE_VAL) {
89
- await fn(this.val);
90
- }
91
- return this;
92
- }
93
- zip(fn) {
94
- if (this.val === NONE_VAL) return Option.None;
95
- const r = fn(this.val);
96
- if (isPromise(r)) {
97
- return r.then((u) => Option.Some([
98
- this.val,
99
- u
100
- ]));
101
- }
102
- return Option.Some([
103
- this.val,
104
- r
105
- ]);
106
- }
107
- flatZip(fn) {
108
- if (this.val === NONE_VAL) return Option.None;
109
- const r = fn(this.val);
110
- if (isPromise(r)) {
111
- return r.then((opt) => opt.map((u) => [
112
- this.val,
113
- u
114
- ]));
115
- }
116
- return r.map((u) => [
117
- this.val,
118
- u
119
- ]);
120
- }
121
- // flatZip<U>(
122
- // fn: (val: T) => Promise<Option<U>>,
123
- // ): Promise<Option<AppendToTuple<T, U>>>;
124
- // flatZip<U>(fn: (val: T) => Option<U>): Option<AppendToTuple<T, U>>;
125
- // flatZip<U>(fn: OptMapper<T, U> | AsyncOptMapper<T, U>) {
126
- // if (this.val === NONE_VAL) return Option.None;
127
- //
128
- // type Tup = AppendToTuple<T, U>;
129
- // const isTuple = Array.isArray(this.val);
130
- // const r = fn(this.val);
131
- // if (isPromise(r)) {
132
- // return r.then((opt) =>
133
- // opt.map(
134
- // (u) =>
135
- // setAsTuple(
136
- // isTuple ? [...this.val, u] : [this.val, u],
137
- // ) as Tuple<Tup>,
138
- // ),
139
- // );
140
- // }
141
- //
142
- // return r.map(
143
- // (u) =>
144
- // setAsTuple(isTuple ? [...this.val, u] : [this.val, u]) as Tuple<Tup>,
145
- // );
146
- // }
147
- static any(seq) {
148
- for (const el of seq) {
149
- if (el.val !== NONE_VAL) return true;
150
- }
151
- return false;
152
- }
153
- static all(seq) {
154
- for (const el of seq) {
155
- if (el.val === NONE_VAL) return false;
156
- }
157
- return true;
158
- }
159
- static sequence(...options) {
160
- const vals = [];
161
- for (const opt of options) {
162
- if (opt.val === NONE_VAL) return Option.None;
163
- vals.push(opt.val);
164
- }
165
- return Option.Some(vals);
166
- }
167
- static async lift(opt) {
168
- if (opt.isNone()) return Promise.resolve(Option.None);
169
- return opt.val.then((val) => Option.Some(val));
170
- }
171
- // innerMap<Inner, U>(
172
- // this: Option<Array<Inner>>,
173
- // mapper: (val: Inner) => U,
174
- // ): Option<Array<U>>;
175
- // innerMap<U>(this: Option<T>, mapper: (val: never) => U): never;
176
- // innerMap<U>(this: Option<T>, mapper: (val: T) => U) {
177
- // if (this.isNone()) return this;
178
- //
179
- // if (Array.isArray(this.val)) {
180
- // return Option.Some(this.val.map(mapper));
181
- // }
182
- //
183
- // throw new Error("Can only be called for Option<Array<T>>");
184
- // }
185
- innerMap(mapper) {
186
- if (this.val === NONE_VAL) return this;
187
- if (Array.isArray(this.val)) {
188
- return Option.Some(this.val.map(mapper));
189
- }
190
- throw new Error("Can only be called for Option<Array<T>>");
191
- }
192
- pipe(...fns) {
193
- let x = this;
194
- for (const fn of fns) {
195
- x = x.map(fn);
196
- }
197
- return x;
198
- }
199
- }
200
- export {
201
- Option,
202
- UnwrappedNone
203
- };
204
- //# sourceMappingURL=option.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/option.ts"],"sourcesContent":["import { isPromise } from \"node:util/types\";\nimport { UNIT } from \"./unit.js\";\n\ntype Mapper<T, U> = (val: T) => U;\ntype AsyncMapper<T, U> = (val: T) => Promise<U>;\ntype OptMapper<T, U> = (val: T) => Option<U>;\ntype AsyncOptMapper<T, U> = (val: T) => Promise<Option<U>>;\n\ntype InnerMapMapper<T, U> = T extends Array<infer Inner>\n ? (val: Inner) => U\n : never;\n\ntype InnerMapReturn<T, U> = T extends Array<unknown> ? Option<Array<U>> : never;\n// type ArrayInner<T> = T extends Array<infer R> ? R : never;\n// type ArrayOpt<Opt extends Option<unknown>> = Opt extends Option<infer T>\n// \t? T extends Array<infer Inner>\n// \t\t? Inner\n// \t\t: never\n// \t: never;\n\nexport class UnwrappedNone extends Error {\n constructor() {\n super(\"Unwrapped Option<None>\");\n }\n}\n\nconst UNWRAPPED_NONE_ERR = new UnwrappedNone();\nconst NONE_VAL = Symbol.for(\"None\");\n\nexport type UnitOption = Option<UNIT>;\nexport type UnwrapOption<T> = T extends Option<infer R> ? R : never;\ntype CombinedOptions<T extends Option<unknown>[]> = {\n [K in keyof T]: UnwrapOption<T[K]>;\n};\n\nexport class Option<T> {\n private constructor(readonly val: T) {}\n\n static readonly None: Option<never> = new Option(NONE_VAL) as Option<never>;\n static readonly UNIT_OPT: UnitOption = new Option(UNIT);\n\n static Some<Inner>(val: Inner): Option<Inner> {\n return new Option(val);\n }\n\n static fromNullable<T>(val: T): Option<NonNullable<T>> {\n return val === null ? Option.None : Option.Some(val as NonNullable<T>);\n }\n\n isSome(): this is Option<T> {\n return this.val !== NONE_VAL;\n }\n\n isNone(): this is Option<never> {\n return this.val === NONE_VAL;\n }\n\n isUnit(): this is Option<UNIT> {\n return this.val === UNIT;\n }\n\n unwrap(): T {\n if (this.val !== NONE_VAL) {\n return this.val;\n }\n\n throw UNWRAPPED_NONE_ERR;\n }\n\n /* Useful to serialize. Inverse operation for `fromNullable` */\n safeUnwrap(): T | null {\n if (this.val !== NONE_VAL) return this.val;\n\n return null;\n }\n\n toString(): string {\n if (this.isNone()) return \"Option::None\";\n\n return `Option::Some(${this.val})`;\n }\n\n map<U>(fn: (val: T) => U): Option<U>;\n map<U>(fn: (val: T) => Promise<U>): Promise<Option<U>>;\n map<U>(fn: OptMapper<T, U> | AsyncOptMapper<T, U>) {\n if (this.isNone()) return Option.None;\n\n const val = fn(this.val);\n if (isPromise(val)) return val.then((u) => Option.Some(u));\n\n return Option.Some(val);\n }\n\n mapOr<U>(def: U, fn: (val: T) => U): Option<U>;\n mapOr<U>(def: U, fn: (val: T) => Promise<U>): Promise<Option<U>>;\n mapOr<U>(def: U, fn: (val: T) => U) {\n if (this.isNone()) return Option.Some(def);\n\n const val = fn(this.val);\n if (isPromise(val)) return val.then((u) => Option.Some(u));\n\n return Option.Some(val);\n }\n\n flatMap<U>(fn: (val: T) => Option<U>): Option<U>;\n flatMap<U>(fn: (val: T) => Promise<Option<U>>): Promise<Option<U>>;\n flatMap<U>(fn: OptMapper<T, U> | AsyncOptMapper<T, U>) {\n if (this.val !== NONE_VAL) return fn(this.val);\n\n return Option.None;\n }\n\n /* Alias for flatMap */\n bind<U>(fn: (val: T) => Option<U>): Option<U>;\n bind<U>(fn: (val: T) => Promise<Option<U>>): Promise<Option<U>>;\n bind<U>(fn: OptMapper<T, U> | AsyncOptMapper<T, U>) {\n if (this.val !== NONE_VAL) return fn(this.val);\n\n return Option.None;\n }\n\n and<U>(opt: Option<U>): Option<[T, U]> {\n if (this.val !== NONE_VAL && opt.val !== NONE_VAL)\n return Option.Some([this.val, opt.val]);\n\n return Option.None;\n }\n\n tap(fn: (val: T) => void): Option<T> {\n if (this.val !== NONE_VAL) fn(this.val);\n\n return this;\n }\n\n async tapAsync(fn: (val: T) => Promise<unknown>): Promise<Option<T>> {\n if (this.val !== NONE_VAL) {\n await fn(this.val);\n }\n\n return this;\n }\n\n zip<U>(fn: (val: T) => Promise<U>): Promise<Option<[T, U]>>;\n zip<U>(fn: (val: T) => U): Option<[T, U]>;\n zip<U>(fn: Mapper<T, U> | AsyncMapper<T, U>) {\n if (this.val === NONE_VAL) return Option.None; // await won't hurt it\n\n const r = fn(this.val);\n if (isPromise(r)) {\n return r.then((u) => Option.Some([this.val, u]));\n }\n\n return Option.Some([this.val, r]);\n }\n // zip<U>(\n // \tfn: (val: T) => Promise<U>,\n // ): Promise<Option<Tuple<AppendToTuple<T, U>>>>;\n // zip<U>(fn: (val: T) => U): Option<Tuple<AppendToTuple<T, U>>>;\n // zip<U>(fn: Mapper<T, U> | AsyncMapper<T, U>) {\n // \tif (this.val === NONE_VAL) return Option.None; // await won't hurt it\n //\n // \ttype Tup = AppendToTuple<T, U>;\n // \tconst isTuple = Array.isArray(this.val);\n //\n // \tconst r = fn(this.val);\n // \tif (isPromise(r)) {\n // \t\treturn r.then((u) => {\n // \t\t\tconst inner: Tuple<Tup> = setAsTuple(\n // \t\t\t\tisTuple ? [...this.val, u] : [this.val, u],\n // \t\t\t);\n //\n // \t\t\treturn Option.Some(inner) as Option<Tuple<Tup>>;\n // \t\t});\n // \t}\n //\n // \tconst inner: Tuple<Tup> = setAsTuple(\n // \t\tisTuple ? [...this.val, r] : [this.val, r],\n // \t);\n //\n // \treturn Option.Some(inner) as Option<Tuple<Tup>>;\n // }\n\n flatZip<U>(fn: (val: T) => Promise<Option<U>>): Promise<Option<[T, U]>>;\n flatZip<U>(fn: (val: T) => Option<U>): Option<[T, U]>;\n flatZip<U>(fn: OptMapper<T, U> | AsyncOptMapper<T, U>) {\n if (this.val === NONE_VAL) return Option.None;\n\n const r = fn(this.val);\n if (isPromise(r)) {\n return r.then((opt) => opt.map((u) => [this.val, u]));\n }\n\n return r.map((u) => [this.val, u]);\n }\n // flatZip<U>(\n // \tfn: (val: T) => Promise<Option<U>>,\n // ): Promise<Option<AppendToTuple<T, U>>>;\n // flatZip<U>(fn: (val: T) => Option<U>): Option<AppendToTuple<T, U>>;\n // flatZip<U>(fn: OptMapper<T, U> | AsyncOptMapper<T, U>) {\n // \tif (this.val === NONE_VAL) return Option.None;\n //\n // \ttype Tup = AppendToTuple<T, U>;\n // \tconst isTuple = Array.isArray(this.val);\n // \tconst r = fn(this.val);\n // \tif (isPromise(r)) {\n // \t\treturn r.then((opt) =>\n // \t\t\topt.map(\n // \t\t\t\t(u) =>\n // \t\t\t\t\tsetAsTuple(\n // \t\t\t\t\t\tisTuple ? [...this.val, u] : [this.val, u],\n // \t\t\t\t\t) as Tuple<Tup>,\n // \t\t\t),\n // \t\t);\n // \t}\n //\n // \treturn r.map(\n // \t\t(u) =>\n // \t\t\tsetAsTuple(isTuple ? [...this.val, u] : [this.val, u]) as Tuple<Tup>,\n // \t);\n // }\n\n static any<T>(seq: Option<T>[]): boolean {\n for (const el of seq) {\n if (el.val !== NONE_VAL) return true;\n }\n\n return false;\n }\n\n static all<T>(seq: Option<T>[]): boolean {\n for (const el of seq) {\n if (el.val === NONE_VAL) return false;\n }\n\n return true;\n }\n\n static sequence<T extends Option<unknown>[]>(\n ...options: T\n ): Option<CombinedOptions<T>> {\n const vals = [] as CombinedOptions<T>;\n\n for (const opt of options) {\n if (opt.val === NONE_VAL) return Option.None;\n\n vals.push(opt.val);\n }\n\n return Option.Some(vals);\n }\n\n static async lift<T>(opt: Option<Promise<T>>): Promise<Option<T>> {\n if (opt.isNone()) return Promise.resolve(Option.None);\n\n return opt.val.then((val) => Option.Some(val));\n }\n\n // innerMap<Inner, U>(\n // \tthis: Option<Array<Inner>>,\n // \tmapper: (val: Inner) => U,\n // ): Option<Array<U>>;\n // innerMap<U>(this: Option<T>, mapper: (val: never) => U): never;\n // innerMap<U>(this: Option<T>, mapper: (val: T) => U) {\n // \tif (this.isNone()) return this;\n //\n // \tif (Array.isArray(this.val)) {\n // \t\treturn Option.Some(this.val.map(mapper));\n // \t}\n //\n // \tthrow new Error(\"Can only be called for Option<Array<T>>\");\n // }\n\n innerMap<U>(mapper: InnerMapMapper<T, U>): InnerMapReturn<T, U> {\n type Ret = InnerMapReturn<T, U>;\n\n //@ts-expect-error\n if (this.val === NONE_VAL) return this as Ret;\n\n if (Array.isArray(this.val)) {\n return Option.Some(this.val.map(mapper)) as Ret;\n }\n\n throw new Error(\"Can only be called for Option<Array<T>>\");\n }\n\n //@ts-expect-error\n pipe<U>(fn: (val: T) => U): Option<U>;\n pipe<U, V>(fn: (val: T) => U, fn2: (val: U) => V): Option<V>;\n pipe<U, V, W>(\n fn: (val: T) => U,\n fn2: (val: U) => V,\n fn3: (val: V) => W,\n ): Option<W>;\n pipe<U, V, W, X>(\n fn: (val: T) => U,\n fn2: (val: U) => V,\n fn3: (val: V) => W,\n fn4: (val: W) => X,\n ): Option<X>;\n pipe<U, V, W, X, Y>(\n fn: (val: T) => U,\n fn2: (val: U) => V,\n fn3: (val: V) => W,\n fn4: (val: W) => X,\n fn5: (val: X) => Y,\n ): Option<Y>;\n pipe<U, V, W, X, Y, Z>(\n fn: (val: T) => U,\n fn2: (val: U) => V,\n fn3: (val: V) => W,\n fn4: (val: W) => X,\n fn5: (val: X) => Y,\n fn6: (val: Y) => Z,\n ): Option<Z>;\n pipe<U, V, W, X, Y, Z, A>(\n fn: (val: T) => U,\n fn2: (val: U) => V,\n fn3: (val: V) => W,\n fn4: (val: W) => X,\n fn5: (val: X) => Y,\n fn6: (val: Y) => Z,\n fn7: (val: Z) => A,\n ): Option<A>;\n pipe<U, V, W, X, Y, Z, A, B>(\n fn: (val: T) => U,\n fn2: (val: U) => V,\n fn3: (val: V) => W,\n fn4: (val: W) => X,\n fn5: (val: X) => Y,\n fn6: (val: Y) => Z,\n fn7: (val: Z) => A,\n fn8: (val: A) => B,\n ): Option<B>;\n pipe<U, V, W, X, Y, Z, A, B, C>(\n fn: (val: T) => U,\n fn2: (val: U) => V,\n fn3: (val: V) => W,\n fn4: (val: W) => X,\n fn5: (val: X) => Y,\n fn6: (val: Y) => Z,\n fn7: (val: Z) => A,\n fn8: (val: A) => B,\n fn9: (val: B) => C,\n ): Option<C>;\n pipe<U, V, W, X, Y, Z, A, B, C, D>(\n fn: (val: T) => U,\n fn2: (val: U) => V,\n fn3: (val: V) => W,\n fn4: (val: W) => X,\n fn5: (val: X) => Y,\n fn6: (val: Y) => Z,\n fn7: (val: Z) => A,\n fn8: (val: A) => B,\n fn9: (val: B) => C,\n fn10: (val: C) => D,\n ): Option<D>;\n pipe(...fns: Mapper<unknown, unknown>[]) {\n let x = this;\n for (const fn of fns) {\n // @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QC,KAAiC;AACvC,QAAIC,IAAI;AACR,eAAW/B,MAAM8B,KAAK;AAEpBC,UAAIA,EAAEhC,IAAIC,EAAAA;IACZ;AACA,WAAO+B;EACT;AACF;","names":["isPromise","UNIT","UnwrappedNone","Error","constructor","UNWRAPPED_NONE_ERR","NONE_VAL","Symbol","for","Option","val","None","UNIT_OPT","Some","fromNullable","isSome","isNone","isUnit","unwrap","safeUnwrap","toString","map","fn","then","u","mapOr","def","flatMap","bind","and","opt","tap","tapAsync","zip","r","flatZip","any","seq","el","all","sequence","options","vals","push","lift","Promise","resolve","innerMap","mapper","Array","isArray","pipe","fns","x"]}
package/dist/result.cjs DELETED
@@ -1,288 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
- var __export = (target, all) => {
8
- for (var name in all)
9
- __defProp(target, name, { get: all[name], enumerable: true });
10
- };
11
- var __copyProps = (to, from, except, desc) => {
12
- if (from && typeof from === "object" || typeof from === "function") {
13
- for (let key of __getOwnPropNames(from))
14
- if (!__hasOwnProp.call(to, key) && key !== except)
15
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
- }
17
- return to;
18
- };
19
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
- var result_exports = {};
21
- __export(result_exports, {
22
- Result: () => Result,
23
- UnwrappedErrWithOk: () => UnwrappedErrWithOk,
24
- UnwrappedOkWithErr: () => UnwrappedOkWithErr
25
- });
26
- module.exports = __toCommonJS(result_exports);
27
- var import_types = require("node:util/types");
28
- var import_unit = require("./unit.js");
29
- class UnwrappedErrWithOk extends Error {
30
- static {
31
- __name(this, "UnwrappedErrWithOk");
32
- }
33
- constructor(r) {
34
- super(`Attempted to call unwrapErr on an okay value: <${r}>`);
35
- }
36
- }
37
- class UnwrappedOkWithErr extends Error {
38
- static {
39
- __name(this, "UnwrappedOkWithErr");
40
- }
41
- constructor(r) {
42
- super(`Attempted to call unwrap on an Err value: <${r}>`);
43
- }
44
- }
45
- const okPred = /* @__PURE__ */ __name((el) => el.isOk(), "okPred");
46
- const errPred = /* @__PURE__ */ __name((el) => el.isErr(), "errPred");
47
- const preds = [
48
- okPred,
49
- errPred
50
- ];
51
- const Sentinel = Symbol.for("ResultSentinel");
52
- class Result {
53
- static {
54
- __name(this, "Result");
55
- }
56
- val;
57
- error;
58
- static UNIT_RESULT = new Result(import_unit.UNIT, null);
59
- constructor(val, error) {
60
- this.val = val;
61
- this.error = error;
62
- }
63
- static Ok(val) {
64
- return new Result(val, Sentinel);
65
- }
66
- static Err(err) {
67
- return new Result(Sentinel, err);
68
- }
69
- isOk() {
70
- return this.val !== Sentinel;
71
- }
72
- isErr() {
73
- return this.error !== Sentinel;
74
- }
75
- isUnit() {
76
- return this.val === import_unit.UNIT;
77
- }
78
- toString() {
79
- if (this.val !== Sentinel) {
80
- return `Result::Ok<${this.val}>`;
81
- }
82
- return `Result::Err<${this.error}>`;
83
- }
84
- unwrap() {
85
- if (this.val === Sentinel) {
86
- if (this.error instanceof Error) throw this.error;
87
- throw new UnwrappedOkWithErr(this);
88
- }
89
- return this.val;
90
- }
91
- unwrapOr(def) {
92
- if (this.val === Sentinel) return def;
93
- return this.val;
94
- }
95
- unwrapOrElse(def) {
96
- if (this.val === Sentinel) return def();
97
- return this.val;
98
- }
99
- safeUnwrap() {
100
- return this.val === Sentinel ? null : this.val;
101
- }
102
- unwrapErr() {
103
- if (this.isOk()) {
104
- throw new UnwrappedErrWithOk(this);
105
- }
106
- return this.error;
107
- }
108
- safeUnwrapErr() {
109
- return this.error === Sentinel ? null : this.error;
110
- }
111
- map(fn) {
112
- if (this.val === Sentinel) return Result.Err(this.error);
113
- const r = fn(this.val);
114
- if ((0, import_types.isPromise)(r)) {
115
- return r.then((val) => Result.Ok(val));
116
- }
117
- return Result.Ok(r);
118
- }
119
- /** Same as map, but for the error value instead of the Ok value */
120
- mapErr(fn) {
121
- if (this.error !== Sentinel) {
122
- const mappedErr = fn(this.error);
123
- return Result.Err(mappedErr);
124
- }
125
- return Result.Ok(this.val);
126
- }
127
- mapOr(default_, fn) {
128
- if (this.val === Sentinel) return default_;
129
- return fn(this.val);
130
- }
131
- mapOrAsync(default_, fn) {
132
- if (this.val === Sentinel) return Promise.resolve(default_);
133
- return fn(this.val);
134
- }
135
- /** For combining two results. For the lazy eval version, see {@link flatZip} and {@link zipAsync} */
136
- and(r) {
137
- if (this.isErr()) return this;
138
- if (r.isErr()) return r;
139
- return Result.Ok([
140
- this.val,
141
- r.val
142
- ]);
143
- }
144
- flatMap(fn) {
145
- if (this.val !== Sentinel) return fn(this.val);
146
- return Result.Err(this.error);
147
- }
148
- bind(fn) {
149
- if (this.val !== Sentinel) return fn(this.val);
150
- return Result.Err(this.error);
151
- }
152
- zipErr(fn) {
153
- if (this.val === Sentinel) return Result.Err(this.error);
154
- const r = fn(this.val);
155
- if ((0, import_types.isPromise)(r)) {
156
- return r.then((result) => result.map((_) => this.val));
157
- }
158
- return r.map((_) => this.val);
159
- }
160
- /** For running infallible side-effects on the contained value. Up to the programmer to ensure the underlying function doesn't fail */
161
- tap(fn) {
162
- if (this.val !== Sentinel) {
163
- fn(this.val);
164
- }
165
- return this;
166
- }
167
- /**
168
- * Async version of {@link tap}
169
- * @see {@link tap}
170
- */
171
- async tapAsync(fn) {
172
- if (this.val !== Sentinel) {
173
- await fn(this.val);
174
- }
175
- return this;
176
- }
177
- zip(fn) {
178
- if (this.val === Sentinel) return Result.Err(this.error);
179
- const r = fn(this.val);
180
- if ((0, import_types.isPromise)(r)) {
181
- return r.then((u) => Result.Ok([
182
- this.val,
183
- u
184
- ]));
185
- }
186
- return Result.Ok([
187
- this.val,
188
- r
189
- ]);
190
- }
191
- flatZip(fn) {
192
- if (this.val === Sentinel) return Result.Err(this.error);
193
- const r = fn(this.val);
194
- if ((0, import_types.isPromise)(r)) {
195
- return r.then((other) => other.map((u) => [
196
- this.val,
197
- u
198
- ]));
199
- }
200
- return r.map((u) => [
201
- this.val,
202
- u
203
- ]);
204
- }
205
- static all(c, seq) {
206
- const pred = preds[Number(c === "ok")];
207
- for (const el of seq) {
208
- if (!pred(el)) return false;
209
- }
210
- return true;
211
- }
212
- /** Whether any of the underlying results are Ok or Err */
213
- static any(c, seq) {
214
- const pred = preds[Number(c === "ok")];
215
- for (const el of seq) {
216
- if (pred(el)) return true;
217
- }
218
- return false;
219
- }
220
- /**
221
- * `Result.sequence(Result<T1, E1>, Result<T2, E2>, ...)` will return `Result<[T1, T2, ...], E1 | E2 | ...>`
222
- * @example
223
- * const r1 = Result.Ok<string, string>("abc")
224
- * const r2 = Result.Ok<number, Error>(123)
225
- * const r = Result.sequence(r1, r2) // r will be of type Result<[string, number], string | Error>
226
- */
227
- static sequence(...results) {
228
- const vals = [];
229
- for (const r of results) {
230
- if (r.isErr()) return r;
231
- vals.push(r.unwrap());
232
- }
233
- return Result.Ok(vals);
234
- }
235
- /** Convert a Result of a Promise to the Promise of a Result */
236
- static async lift(r) {
237
- if (r.isErr()) return Promise.resolve(Result.Err(r.error));
238
- return r.val.then((v) => Result.Ok(v));
239
- }
240
- //#endregion
241
- // static innerMap<T, U, E>(
242
- // r: Result<Array<T>, E>,
243
- // mapper: (val: T) => U,
244
- // ): Result<Array<U>, E> {
245
- // return r.map((inner) => inner.map(mapper));
246
- // }
247
- // innerMap<Inner, U>(
248
- // this: Result<Array<Inner>, E>,
249
- // mapper: (val: T) => U,
250
- // ): Result<Array<U>, E>;
251
- // innerMap<U>(this: Result<unknown, E>, mapper: (val: T) => U): never;
252
- // innerMap<U>(this: Result<unknown, E>, mapper: (val: T) => U) {
253
- // if (this.val === Sentinel) return this;
254
- //
255
- // if (Array.isArray(this.val)) {
256
- // return Result.Ok(this.val.map(mapper));
257
- // }
258
- //
259
- // throw new Error("Can only be called for Result<Array<T>, E>");
260
- // }
261
- innerMap(mapper) {
262
- if (this.val === Sentinel) return this;
263
- if (Array.isArray(this.val)) {
264
- return Result.Ok(this.val.map(mapper));
265
- }
266
- throw new Error("Can only be called for Result<Array<T>, E>");
267
- }
268
- pipe(...fns) {
269
- let res3 = this;
270
- for (const fn of fns) {
271
- res3 = res3.map(fn);
272
- }
273
- return res3;
274
- }
275
- }
276
- const res = Result.Ok([
277
- 1,
278
- 2,
279
- 3
280
- ]);
281
- const res2 = Result.Ok(123);
282
- // Annotate the CommonJS export names for ESM import in node:
283
- 0 && (module.exports = {
284
- Result,
285
- UnwrappedErrWithOk,
286
- UnwrappedOkWithErr
287
- });
288
- //# sourceMappingURL=result.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/result.ts"],"sourcesContent":["import { isPromise } from \"node:util/types\";\nimport { UNIT } from \"./unit.js\";\n\nexport class UnwrappedErrWithOk extends Error {\n constructor(r: Result<unknown, unknown>) {\n super(`Attempted to call unwrapErr on an okay value: <${r}>`);\n }\n}\n\nexport class UnwrappedOkWithErr extends Error {\n constructor(r: Result<unknown, unknown>) {\n super(`Attempted to call unwrap on an Err value: <${r}>`);\n }\n}\n\nexport type UnitResult<E = never> = Result<UNIT, E>;\n\ntype Mapper<T, U> = (val: T) => U;\ntype AsyncMapper<T, U> = (val: T) => Promise<U>;\ntype ResMapper<T, U, E> = (val: T) => Result<U, E>;\ntype AsyncResMapper<T, U, E> = (val: T) => Promise<Result<U, E>>;\n\ntype OkOrErr = \"ok\" | \"err\";\nconst okPred = <T, E extends Error>(el: Result<T, E>): boolean => el.isOk();\nconst errPred = <T, E extends Error>(el: Result<T, E>): boolean => el.isErr();\nconst preds = [okPred, errPred];\n\nexport type UnwrapResult<T extends Result<unknown, unknown>> = T extends Result<\n infer U,\n infer E\n>\n ? { ok: U; err: E }\n : never;\n\ntype CombinedResultOk<T extends Result<unknown, unknown>[]> = {\n [K in keyof T]: UnwrapResult<T[K]>[\"ok\"];\n};\ntype CombinedResultErr<T extends Result<unknown, unknown>[]> = {\n [K in keyof T]: UnwrapResult<T[K]>[\"err\"];\n}[number];\n\nexport type CombineResults<T extends Result<unknown, unknown>[]> = Result<\n CombinedResultOk<T>,\n CombinedResultErr<T>\n>;\n\ntype InnerMapMapper<T, U> = T extends Array<infer Inner>\n ? (val: Inner) => U\n : never;\n\ntype InnerMapReturn<T, E, U> = T extends Array<unknown>\n ? Result<Array<U>, E>\n : never;\n\n/** Sentinel value */\nconst Sentinel = Symbol.for(\"ResultSentinel\");\ntype Sentinel = typeof Sentinel;\n\nexport class Result<T, E> {\n static readonly UNIT_RESULT: UnitResult = new Result(UNIT, null as never);\n\n protected constructor(\n private readonly val: T | Sentinel,\n private readonly error: E | Sentinel,\n ) {}\n\n static Ok<T, E = never>(val: T): Result<T, E> {\n return new Result(val, Sentinel) as Result<T, E>;\n }\n\n static Err<E, T = never>(err: E): Result<T, E> {\n return new Result(Sentinel, err) as Result<T, E>;\n }\n\n isOk(): this is Result<T, never> {\n return this.val !== Sentinel;\n }\n\n isErr(): this is Result<never, E> {\n return this.error !== Sentinel;\n }\n\n isUnit(): this is Result<UNIT, never> {\n return this.val === UNIT;\n }\n\n toString(): string {\n if (this.val !== Sentinel) {\n return `Result::Ok<${this.val}>`;\n }\n\n return `Result::Err<${this.error as E}>`;\n }\n\n unwrap(): T {\n if (this.val === Sentinel) {\n if (this.error instanceof Error) throw this.error;\n\n throw new UnwrappedOkWithErr(this);\n }\n\n return this.val;\n }\n\n unwrapOr(def: T): T {\n if (this.val === Sentinel) return def;\n\n return this.val;\n }\n\n unwrapOrElse(def: () => T): T;\n unwrapOrElse(def: () => Promise<T>): Promise<T>;\n unwrapOrElse(def: () => T | Promise<T>): T | Promise<T> {\n if (this.val === Sentinel) return def();\n\n return this.val;\n }\n\n safeUnwrap(): T | null {\n return this.val === Sentinel ? null : this.val;\n }\n\n unwrapErr(): E {\n if (this.isOk()) {\n throw new UnwrappedErrWithOk(this);\n }\n\n return this.error as E;\n }\n\n safeUnwrapErr(): E | null {\n return this.error === Sentinel ? null : this.error;\n }\n\n /**\n *\n * @param {Mapper<T, U>} fn The mapping/endofunctor to apply to the internal Ok value (T -> U)\n * @returns Result<U, E> containing the mapped value\n * @example\n * const r = Result.Ok({data: 42})\n * const r2 = Result.Err(new Error(\"something went wrong\"))\n * const mapper = (d: {data: 42}) => d.data * 2\n * const result1 = r.map(mapper) // Result containing 64 as the ok value\n * const result2 = r2.map(mapper) // mapper won't be applied, as r2 was in error state\n */\n map<U>(fn: (val: T) => Promise<U>): Promise<Result<U, E>>;\n map<U>(fn: (val: T) => U): Result<U, E>;\n map<U>(\n fn: Mapper<T, U> | AsyncMapper<T, U>,\n ): Result<U, E> | Promise<Result<U, E>> {\n if (this.val === Sentinel) return Result.Err(this.error) as Result<U, E>;\n\n const r = fn(this.val);\n\n if (isPromise(r)) {\n return r.then((val) => Result.Ok(val) as Result<U, E>);\n }\n\n return Result.Ok(r) as Result<U, E>;\n }\n\n /** Same as map, but for the error value instead of the Ok value */\n mapErr<U>(fn: Mapper<E, U>): Result<T, U> {\n if (this.error !== Sentinel) {\n const mappedErr = fn(this.error);\n\n return Result.Err(mappedErr);\n }\n\n return Result.Ok(this.val as T);\n }\n\n mapOr<U>(default_: U, fn: Mapper<T, U>): U {\n if (this.val === Sentinel) return default_;\n\n return fn(this.val);\n }\n\n mapOrAsync<U>(default_: U, fn: AsyncMapper<T, U>): Promise<U> {\n if (this.val === Sentinel) return Promise.resolve(default_);\n\n return fn(this.val);\n }\n\n /** For combining two results. For the lazy eval version, see {@link flatZip} and {@link zipAsync} */\n and<U, E2>(r: Result<U, E2>): Result<[T, U], E | E2> {\n if (this.isErr()) return this;\n\n // return r.map(v => [this.val, v] as [T, U])\n if (r.isErr()) return r;\n\n return Result.Ok([this.val, r.val] as [T, U]);\n }\n\n /**\n * Also known as `andThen`, `bind` or `fmap`\n * Like Result.map but used in cases where the mapper func returns a Result instead of a containable value\n * @example\n * const r: Result<string, SomeError> = Result.Ok(\"42\")\n * const mapper = (d: string) => {\n * const n = Number.parseInt(d)\n * if(Number.isNumber(n) && !Number.isNaN(n)) return Result.Ok(n)\n *\n * return Result.Err(new InvalidNumStringError(d))\n * }\n * const result1 = r.map(mapper) // Type is Result<Result<number, InvalidNumStringError>, SomeError>\n * const result2 = r.flatMap(mapper) // Type is Result<number, SomeError | InvalidNumStringError>\n */\n flatMap<U, E2>(\n fn: (val: T) => Promise<Result<U, E | E2>>,\n ): Promise<Result<U, E | E2>>;\n flatMap<U, E2>(fn: (val: T) => Result<U, E2>): Result<U, E | E2>;\n flatMap<U, E2>(fn: ResMapper<T, U, E | E2> | AsyncResMapper<T, U, E | E2>) {\n if (this.val !== Sentinel) return fn(this.val);\n\n return Result.Err(this.error);\n }\n\n /**\n * Also known as `andThen`, `fmap` or `flatMap`\n * Like Result.map but used in cases where the mapper func returns a Result instead of a containable value\n * @example\n * const r: Result<string, SomeError> = Result.Ok(\"42\")\n * const mapper = (d: string) => {\n * const n = Number.parseInt(d)\n * if(Number.isNumber(n) && !Number.isNaN(n)) return Result.Ok(n)\n *\n * return Result.Err(new InvalidNumStringError(d))\n * }\n * const result1 = r.map(mapper) // Type is Result<Result<number, InvalidNumStringError>, SomeError>\n * const result2 = r.bind(mapper) // Type is Result<number, SomeError | InvalidNumStringError>\n */\n bind<U, E2>(\n fn: (val: T) => Promise<Result<U, E | E2>>,\n ): Promise<Result<U, E | E2>>;\n bind<U, E2>(fn: (val: T) => Result<U, E2>): Result<U, E | E2>;\n bind<U, E2>(fn: ResMapper<T, U, E | E2> | AsyncResMapper<T, U, E | E2>) {\n if (this.val !== Sentinel) return fn(this.val);\n\n return Result.Err(this.error);\n }\n\n /**\n * For combining the errors only while retaining the original value.\n * @example\n * const r: Result<string, SomeError> = Result.Ok('42')\n * const binderOk = (d: string) => Result.Ok<number, SomeOtherError>(0909)\n * const binderErr = (d: string) => Result.Err<number, SomeOtherError>(new SomeOtherError())\n *\n * const res1 = r.zipErr(binderOk) // Result<string, SomeError | SomeOtherError> - Value: Result.Ok('42') - No change in Ok type\n * const res2 = r.zipErr(binderErr) // Result<string, SomeError | SomeOtherError> - Value: Result.Err(new SomeOtherError())\n */\n zipErr<E2>(\n fn: (val: T) => Promise<Result<unknown, E2>>,\n ): Promise<Result<T, E | E2>>;\n zipErr<E2>(fn: (val: T) => Result<unknown, E2>): Result<T, E | E2>;\n zipErr<E2>(\n fn: ResMapper<T, unknown, E | E2> | AsyncResMapper<T, unknown, E | E2>,\n ) {\n if (this.val === Sentinel) return Result.Err(this.error);\n\n const r = fn(this.val);\n\n if (isPromise(r)) {\n return r.then((result) => result.map((_) => this.val));\n }\n\n return r.map((_) => this.val);\n }\n\n /** For running infallible side-effects on the contained value. Up to the programmer to ensure the underlying function doesn't fail */\n tap(fn: (val: T) => void): Result<T, E> {\n if (this.val !== Sentinel) {\n fn(this.val);\n }\n\n return this;\n }\n\n /**\n * Async version of {@link tap}\n * @see {@link tap}\n */\n async tapAsync(fn: (val: T) => Promise<void>): Promise<Result<T, E>> {\n if (this.val !== Sentinel) {\n await fn(this.val);\n }\n\n return this;\n }\n\n zip<U>(fn: (val: T) => Promise<U>): Promise<Result<[T, U], E>>;\n zip<U>(fn: (val: T) => U): Result<[T, U], E>;\n zip<U>(fn: Mapper<T, U> | AsyncMapper<T, U>) {\n if (this.val === Sentinel) return Result.Err(this.error);\n\n const r = fn(this.val);\n if (isPromise(r)) {\n return r.then((u) => Result.Ok([this.val, u]) as Result<[T, U], E>);\n }\n\n return Result.Ok([this.val, r]) as Result<[T, U], E>;\n }\n // zip<U>(fn: (val: T) => Promise<U>): Promise<Result<AppendToTuple<T, U>, E>>;\n // zip<U>(fn: (val: T) => U): Result<AppendToTuple<T, U>, E>;\n // zip<U>(fn: Mapper<T, U> | AsyncMapper<T, U>) {\n // \tif (this.val === Sentinel) return Result.Err(this.error);\n //\n // \ttype Tup = AppendToTuple<T, U>;\n // \tconst isTup = isTuple(this.val);\n //\n // \tconst r = fn(this.val);\n // \tif (isPromise(r)) {\n // \t\treturn r.then((u) => {\n // \t\t\tconst tuple: Tuple<Tup> = setAsTuple(\n // \t\t\t\t//@ts-expect-error\n // \t\t\t\tisTup ? [...this.val, u] : [this.val, u],\n // \t\t\t);\n //\n // \t\t\t//@ts-expect-error\n // \t\t\treturn Result.Ok(tuple) as Result<AppendToTuple<T, U>, E>;\n // \t\t});\n // \t}\n //\n // \tconst tuple: Tuple<Tup> = setAsTuple(\n // \t\t//@ts-expect-error\n // \t\tisTup ? [...this.val, r] : [this.val, r],\n // \t);\n //\n // \t//@ts-expect-error\n // \treturn Result.Ok(tuple) as Result<AppendToTuple<T, U>, E>;\n // }\n\n /** For combining two results lazily. For the eager eval version, see {@link and} */\n flatZip<U, E2>(\n fn: (val: T) => Promise<Result<U, E2>>,\n ): Promise<Result<[T, U], E | E2>>;\n flatZip<U, E2>(fn: (val: T) => Result<U, E2>): Result<[T, U], E | E2>;\n flatZip<U, E2>(fn: ResMapper<T, U, E | E2> | AsyncResMapper<T, U, E | E2>) {\n if (this.val === Sentinel) return Result.Err(this.error);\n\n const r = fn(this.val);\n if (isPromise(r)) {\n return r.then((other) => other.map((u) => [this.val, u]));\n }\n\n return r.map((u) => [this.val, u]);\n }\n // flatZip<U, E2>(\n // \tfn: (val: T) => Promise<Result<U, E2>>,\n // ): Promise<Result<AppendToTuple<T, U>, E | E2>>;\n // flatZip<U, E2>(\n // \tfn: (val: T) => Result<U, E2>,\n // ): Result<AppendToTuple<T, U>, E | E2>;\n // flatZip<U, E2>(fn: ResMapper<T, U, E | E2> | AsyncResMapper<T, U, E | E2>) {\n // \tif (this.val === Sentinel) return Result.Err(this.error);\n //\n // \ttype Tup = AppendToTuple<T, U>;\n // \tconst isTup = isTuple(this.val);\n //\n // \tconst r = fn(this.val);\n // \tif (isPromise(r)) {\n // \t\treturn r.then((other) =>\n // \t\t\tother.map((u) => (isTup ? [...this.val, u] : [this.val, u]) as Tup),\n // \t\t);\n // \t}\n //\n // \treturn r.map((u) => (isTup ? [...this.val, u] : [this.val, u]) as Tup);\n // }\n\n //#region General combo functions\n /** Type guard specifying all array results as Ok/Err */\n static all<T, E extends Error>(\n c: \"ok\",\n seq: Result<T, E>[],\n ): seq is Result<T, never>[];\n static all<T, E extends Error>(\n c: \"err\",\n seq: Result<T, E>[],\n ): seq is Result<never, E>[];\n static all<T, E extends Error>(c: OkOrErr, seq: Result<T, E>[]): boolean {\n const pred = preds[Number(c === \"ok\")];\n\n for (const el of seq) {\n if (!pred(el)) return false;\n }\n\n return true;\n }\n\n /** Whether any of the underlying results are Ok or Err */\n static any<T, E extends Error>(c: OkOrErr, seq: Result<T, E>[]): boolean {\n const pred = preds[Number(c === \"ok\")];\n\n for (const el of seq) {\n if (pred(el)) return true;\n }\n\n return false;\n }\n\n /**\n * `Result.sequence(Result<T1, E1>, Result<T2, E2>, ...)` will return `Result<[T1, T2, ...], E1 | E2 | ...>`\n * @example\n * const r1 = Result.Ok<string, string>(\"abc\")\n * const r2 = Result.Ok<number, Error>(123)\n * const r = Result.sequence(r1, r2) // r will be of type Result<[string, number], string | Error>\n */\n static sequence<T extends Result<unknown, unknown>[]>(\n ...results: T\n ): Result<CombinedResultOk<T>, CombinedResultErr<T>> {\n const vals = [] as CombinedResultOk<T>;\n\n for (const r of results) {\n if (r.isErr()) return r;\n\n vals.push(r.unwrap());\n }\n\n return Result.Ok(vals) as Result<CombinedResultOk<T>, CombinedResultErr<T>>;\n }\n\n /** Convert a Result of a Promise to the Promise of a Result */\n static async lift<T, E extends Error>(\n r: Result<Promise<T>, E>,\n ): Promise<Result<T, E>> {\n if (r.isErr()) return Promise.resolve(Result.Err(r.error as E));\n\n return (r.val as Promise<T>).then((v) => Result.Ok(v));\n }\n //#endregion\n\n // static innerMap<T, U, E>(\n // \tr: Result<Array<T>, E>,\n // \tmapper: (val: T) => U,\n // ): Result<Array<U>, E> {\n // \treturn r.map((inner) => inner.map(mapper));\n // }\n // innerMap<Inner, U>(\n // \tthis: Result<Array<Inner>, E>,\n // \tmapper: (val: T) => U,\n // ): Result<Array<U>, E>;\n // innerMap<U>(this: Result<unknown, E>, mapper: (val: T) => U): never;\n // innerMap<U>(this: Result<unknown, E>, mapper: (val: T) => U) {\n // \tif (this.val === Sentinel) return this;\n //\n // \tif (Array.isArray(this.val)) {\n // \t\treturn Result.Ok(this.val.map(mapper));\n // \t}\n //\n // \tthrow new Error(\"Can only be called for Result<Array<T>, E>\");\n // }\n\n innerMap<U>(mapper: InnerMapMapper<T, U>): InnerMapReturn<T, E, U> {\n type RetType = InnerMapReturn<T, E, U>;\n\n // @ts-expect-error\n if (this.val === Sentinel) return this;\n\n if (Array.isArray(this.val)) {\n return Result.Ok(this.val.map(mapper)) as RetType;\n }\n\n throw new Error(\"Can only be called for Result<Array<T>, E>\");\n }\n\n // @ts-expect-error\n pipe<U>(fn: (val: T) => U): Result<U, E>;\n pipe<U, V>(fn: (val: T) => U, fn2: (val: U) => V): Result<V, E>;\n pipe<U, V, W>(\n fn: (val: T) => U,\n fn2: (val: U) => V,\n fn3: (val: V) => W,\n ): Result<W, E>;\n pipe<U, V, W, X>(\n fn: (val: T) => U,\n fn2: (val: U) => V,\n fn3: (val: V) => W,\n fn4: (val: W) => X,\n ): Result<X, E>;\n pipe<U, V, W, X, Y>(\n fn: (val: T) => U,\n fn2: (val: U) => V,\n fn3: (val: V) => W,\n fn4: (val: W) => X,\n fn5: (val: X) => Y,\n ): Result<Y, E>;\n pipe<U, V, W, X, Y, Z>(\n fn: (val: T) => U,\n fn2: (val: U) => V,\n fn3: (val: V) => W,\n fn4: (val: W) => X,\n fn5: (val: X) => Y,\n fn6: (val: Y) => Z,\n ): Result<Z, E>;\n pipe<U, V, W, X, Y, Z, A>(\n fn: (val: T) => U,\n fn2: (val: U) => V,\n fn3: (val: V) => W,\n fn4: (val: W) => X,\n fn5: (val: X) => Y,\n fn6: (val: Y) => Z,\n fn7: (val: Z) => A,\n ): Result<A, E>;\n pipe<U, V, W, X, Y, Z, A, B>(\n fn: (val: T) => U,\n fn2: (val: U) => V,\n fn3: (val: V) => W,\n fn4: (val: W) => X,\n fn5: (val: X) => Y,\n fn6: (val: Y) => Z,\n fn7: (val: Z) => A,\n fn8: (val: A) => B,\n ): Result<B, E>;\n pipe<U, V, W, X, Y, Z, A, B, C>(\n fn: (val: T) => U,\n fn2: (val: U) => V,\n fn3: (val: V) => W,\n fn4: (val: W) => X,\n fn5: (val: X) => Y,\n fn6: (val: Y) => Z,\n fn7: (val: Z) => A,\n fn8: (val: A) => B,\n fn9: (val: B) => C,\n ): Result<C, E>;\n pipe<U, V, W, X, Y, Z, A, B, C, D>(\n fn: (val: T) => U,\n fn2: (val: U) => V,\n fn3: (val: V) => W,\n fn4: (val: W) => X,\n fn5: (val: X) => Y,\n fn6: (val: Y) => Z,\n fn7: (val: Z) => A,\n fn8: (val: A) => B,\n fn9: (val: B) => C,\n fn10: (val: C) => D,\n ): Result<D, E>;\n pipe(...fns: Mapper<unknown, unknown>[]) {\n let res = this;\n for (const fn of fns) {\n // @ts-expect-error\n res = res.map(fn);\n }\n return res;\n }\n}\n\nconst res = Result.Ok([1, 2, 3]);\nconst res2 = Result.Ok(123);\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;;;;;;;AAAA,mBAA0B;AAC1B,kBAAqB;AAEd,MAAMA,2BAA2BC,MAAAA;EAHxC,OAGwCA;;;EACtCC,YAAYC,GAA6B;AACvC,UAAM,kDAAkDA,CAAAA,GAAI;EAC9D;AACF;AAEO,MAAMC,2BAA2BH,MAAAA;EATxC,OASwCA;;;EACtCC,YAAYC,GAA6B;AACvC,UAAM,8CAA8CA,CAAAA,GAAI;EAC1D;AACF;AAUA,MAAME,SAAS,wBAAqBC,OAA8BA,GAAGC,KAAI,GAA1D;AACf,MAAMC,UAAU,wBAAqBF,OAA8BA,GAAGG,MAAK,GAA3D;AAChB,MAAMC,QAAQ;EAACL;EAAQG;;AA8BvB,MAAMG,WAAW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errPred","isErr","preds","Sentinel","Symbol","for","Result","UNIT_RESULT","UNIT","val","error","Ok","Err","err","isUnit","toString","unwrap","unwrapOr","def","unwrapOrElse","safeUnwrap","unwrapErr","safeUnwrapErr","map","fn","isPromise","then","mapErr","mappedErr","mapOr","default_","mapOrAsync","Promise","resolve","and","flatMap","bind","zipErr","result","_","tap","tapAsync","zip","u","flatZip","other","all","c","seq","pred","Number","any","sequence","results","vals","push","lift","v","innerMap","mapper","Array","isArray","pipe","fns","res","res2"]}