@carbonteq/fp 0.5.4 → 0.7.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.
Files changed (52) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +36 -0
  3. package/dist/chunk-7QVYU63E.js +7 -0
  4. package/dist/chunk-7QVYU63E.js.map +1 -0
  5. package/dist/index.cjs +46 -0
  6. package/dist/index.cjs.map +1 -0
  7. package/dist/index.d.cts +4 -0
  8. package/dist/index.d.ts +4 -0
  9. package/dist/index.js +16 -0
  10. package/dist/index.js.map +1 -0
  11. package/dist/match.cjs +43 -0
  12. package/dist/match.cjs.map +1 -0
  13. package/dist/match.d.cts +14 -0
  14. package/dist/match.d.ts +14 -0
  15. package/dist/match.js +20 -0
  16. package/dist/match.js.map +1 -0
  17. package/dist/option.cjs +227 -0
  18. package/dist/option.cjs.map +1 -0
  19. package/dist/option.d.cts +58 -0
  20. package/dist/option.d.ts +58 -0
  21. package/dist/option.js +204 -0
  22. package/dist/option.js.map +1 -0
  23. package/dist/result.cjs +288 -0
  24. package/dist/result.cjs.map +1 -0
  25. package/dist/result.d.cts +150 -0
  26. package/{lib → dist}/result.d.ts +52 -24
  27. package/dist/result.js +264 -0
  28. package/dist/result.js.map +1 -0
  29. package/dist/tuple.cjs +41 -0
  30. package/dist/tuple.cjs.map +1 -0
  31. package/dist/tuple.d.cts +9 -0
  32. package/dist/tuple.d.ts +9 -0
  33. package/dist/tuple.js +18 -0
  34. package/dist/tuple.js.map +1 -0
  35. package/dist/unit.cjs +29 -0
  36. package/dist/unit.cjs.map +1 -0
  37. package/dist/unit.d.cts +4 -0
  38. package/dist/unit.d.ts +4 -0
  39. package/dist/unit.js +6 -0
  40. package/dist/unit.js.map +1 -0
  41. package/package.json +65 -72
  42. package/lib/index.d.ts +0 -7
  43. package/lib/index.d.ts.map +0 -1
  44. package/lib/index.js +0 -1
  45. package/lib/index.mjs +0 -1
  46. package/lib/match.d.ts +0 -11
  47. package/lib/match.d.ts.map +0 -1
  48. package/lib/option.d.ts +0 -43
  49. package/lib/option.d.ts.map +0 -1
  50. package/lib/result.d.ts.map +0 -1
  51. package/lib/unit.d.ts +0 -3
  52. package/lib/unit.d.ts.map +0 -1
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Carbonteq
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 CHANGED
@@ -1 +1,37 @@
1
1
  # FP Utils
2
+
3
+ ## Installation
4
+
5
+ ```sh
6
+ npm i @carbonteq/fp
7
+ ```
8
+
9
+ ```sh
10
+ pnpm i @carbonteq/fp
11
+ ```
12
+
13
+ ```sh
14
+ yarn add @carbonteq/fp
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ```typescript
20
+ import { Option, matchOpt } from "@carbonteq/fp";
21
+
22
+ const safeDiv = (num: number, denom: number): Option<number> => {
23
+ if (denom === 0) return Option.None;
24
+
25
+ return Option.Some(num / denom);
26
+ };
27
+
28
+ const getFormatted = (opt: Option<number>): string => {
29
+ return matchOpt(opt, {
30
+ Some: (n) => `Result: ${n}`,
31
+ None: () => "Cannot divide by zero",
32
+ });
33
+ };
34
+
35
+ console.log(getFormatted(safeDiv(10, 2))); // Result: 5
36
+ console.log(getFormatted(safeDiv(10, 0))); // Cannot divide by zero
37
+ ```
@@ -0,0 +1,7 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ export {
5
+ __name
6
+ };
7
+ //# sourceMappingURL=chunk-7QVYU63E.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
package/dist/index.cjs ADDED
@@ -0,0 +1,46 @@
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 __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var src_exports = {};
20
+ __export(src_exports, {
21
+ Option: () => import_option.Option,
22
+ Result: () => import_result.Result,
23
+ UNIT: () => import_unit.UNIT,
24
+ UnwrappedErrWithOk: () => import_result.UnwrappedErrWithOk,
25
+ UnwrappedNone: () => import_option.UnwrappedNone,
26
+ UnwrappedOkWithErr: () => import_result.UnwrappedOkWithErr,
27
+ matchOpt: () => import_match.matchOpt,
28
+ matchRes: () => import_match.matchRes
29
+ });
30
+ module.exports = __toCommonJS(src_exports);
31
+ var import_result = require("./result.js");
32
+ var import_option = require("./option.js");
33
+ var import_match = require("./match.js");
34
+ var import_unit = require("./unit.js");
35
+ // Annotate the CommonJS export names for ESM import in node:
36
+ 0 && (module.exports = {
37
+ Option,
38
+ Result,
39
+ UNIT,
40
+ UnwrappedErrWithOk,
41
+ UnwrappedNone,
42
+ UnwrappedOkWithErr,
43
+ matchOpt,
44
+ matchRes
45
+ });
46
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["export { Result, UnwrappedErrWithOk, UnwrappedOkWithErr } from \"./result.js\";\nexport type { CombineResults, UnitResult, UnwrapResult } from \"./result.ts\";\nexport { Option, UnwrappedNone } from \"./option.js\";\nexport type { UnitOption, UnwrapOption } from \"./option.ts\";\nexport { matchOpt, matchRes } from \"./match.js\";\nexport { UNIT } from \"./unit.js\";\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;;oBAA+D;AAE/D,oBAAsC;AAEtC,mBAAmC;AACnC,kBAAqB;","names":[]}
@@ -0,0 +1,4 @@
1
+ export { CombineResults, Result, UnitResult, UnwrapResult, UnwrappedErrWithOk, UnwrappedOkWithErr } from './result.cjs';
2
+ export { Option, UnitOption, UnwrapOption, UnwrappedNone } from './option.cjs';
3
+ export { matchOpt, matchRes } from './match.cjs';
4
+ export { UNIT } from './unit.cjs';
@@ -0,0 +1,4 @@
1
+ export { CombineResults, Result, UnitResult, UnwrapResult, UnwrappedErrWithOk, UnwrappedOkWithErr } from './result.js';
2
+ export { Option, UnitOption, UnwrapOption, UnwrappedNone } from './option.js';
3
+ export { matchOpt, matchRes } from './match.js';
4
+ export { UNIT } from './unit.js';
package/dist/index.js ADDED
@@ -0,0 +1,16 @@
1
+ import "./chunk-7QVYU63E.js";
2
+ import { Result, UnwrappedErrWithOk, UnwrappedOkWithErr } from "./result.js";
3
+ import { Option, UnwrappedNone } from "./option.js";
4
+ import { matchOpt, matchRes } from "./match.js";
5
+ import { UNIT } from "./unit.js";
6
+ export {
7
+ Option,
8
+ Result,
9
+ UNIT,
10
+ UnwrappedErrWithOk,
11
+ UnwrappedNone,
12
+ UnwrappedOkWithErr,
13
+ matchOpt,
14
+ matchRes
15
+ };
16
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["export { Result, UnwrappedErrWithOk, UnwrappedOkWithErr } from \"./result.js\";\nexport type { CombineResults, UnitResult, UnwrapResult } from \"./result.ts\";\nexport { Option, UnwrappedNone } from \"./option.js\";\nexport type { UnitOption, UnwrapOption } from \"./option.ts\";\nexport { matchOpt, matchRes } from \"./match.js\";\nexport { UNIT } from \"./unit.js\";\n"],"mappings":";AAAA,SAASA,QAAQC,oBAAoBC,0BAA0B;AAE/D,SAASC,QAAQC,qBAAqB;AAEtC,SAASC,UAAUC,gBAAgB;AACnC,SAASC,YAAY;","names":["Result","UnwrappedErrWithOk","UnwrappedOkWithErr","Option","UnwrappedNone","matchOpt","matchRes","UNIT"]}
package/dist/match.cjs ADDED
@@ -0,0 +1,43 @@
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 match_exports = {};
21
+ __export(match_exports, {
22
+ matchOpt: () => matchOpt,
23
+ matchRes: () => matchRes
24
+ });
25
+ module.exports = __toCommonJS(match_exports);
26
+ const matchRes = /* @__PURE__ */ __name((r, branches) => {
27
+ if (r.isOk()) {
28
+ return branches.Ok(r.unwrap());
29
+ }
30
+ return branches.Err(r.unwrapErr());
31
+ }, "matchRes");
32
+ const matchOpt = /* @__PURE__ */ __name((o, branches) => {
33
+ if (o.isSome()) {
34
+ return branches.Some(o.unwrap());
35
+ }
36
+ return branches.None();
37
+ }, "matchOpt");
38
+ // Annotate the CommonJS export names for ESM import in node:
39
+ 0 && (module.exports = {
40
+ matchOpt,
41
+ matchRes
42
+ });
43
+ //# sourceMappingURL=match.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/match.ts"],"sourcesContent":["import type { Option } from \"./option.ts\";\nimport type { Result } from \"./result.ts\";\n\nexport const matchRes = <T, E, U>(\n r: Result<T, E>,\n branches: { Ok: (val: T) => U; Err: (err: E) => U },\n): U => {\n if (r.isOk()) {\n return branches.Ok(r.unwrap());\n }\n return branches.Err(r.unwrapErr());\n};\n\nexport const matchOpt = <T, U>(\n o: Option<T>,\n branches: { Some: (val: T) => U; None: () => U },\n): U => {\n if (o.isSome()) {\n return branches.Some(o.unwrap());\n }\n return branches.None();\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAGA;;;;;;AAAO,MAAMA,WAAW,wBACtBC,GACAC,aAAAA;AAEA,MAAID,EAAEE,KAAI,GAAI;AACZ,WAAOD,SAASE,GAAGH,EAAEI,OAAM,CAAA;EAC7B;AACA,SAAOH,SAASI,IAAIL,EAAEM,UAAS,CAAA;AACjC,GARwB;AAUjB,MAAMC,WAAW,wBACtBC,GACAP,aAAAA;AAEA,MAAIO,EAAEC,OAAM,GAAI;AACd,WAAOR,SAASS,KAAKF,EAAEJ,OAAM,CAAA;EAC/B;AACA,SAAOH,SAASU,KAAI;AACtB,GARwB;","names":["matchRes","r","branches","isOk","Ok","unwrap","Err","unwrapErr","matchOpt","o","isSome","Some","None"]}
@@ -0,0 +1,14 @@
1
+ import { Result } from './result.cjs';
2
+ import { Option } from './option.cjs';
3
+ import './unit.cjs';
4
+
5
+ declare const matchRes: <T, E, U>(r: Result<T, E>, branches: {
6
+ Ok: (val: T) => U;
7
+ Err: (err: E) => U;
8
+ }) => U;
9
+ declare const matchOpt: <T, U>(o: Option<T>, branches: {
10
+ Some: (val: T) => U;
11
+ None: () => U;
12
+ }) => U;
13
+
14
+ export { matchOpt, matchRes };
@@ -0,0 +1,14 @@
1
+ import { Result } from './result.js';
2
+ import { Option } from './option.js';
3
+ import './unit.js';
4
+
5
+ declare const matchRes: <T, E, U>(r: Result<T, E>, branches: {
6
+ Ok: (val: T) => U;
7
+ Err: (err: E) => U;
8
+ }) => U;
9
+ declare const matchOpt: <T, U>(o: Option<T>, branches: {
10
+ Some: (val: T) => U;
11
+ None: () => U;
12
+ }) => U;
13
+
14
+ export { matchOpt, matchRes };
package/dist/match.js ADDED
@@ -0,0 +1,20 @@
1
+ import {
2
+ __name
3
+ } from "./chunk-7QVYU63E.js";
4
+ const matchRes = /* @__PURE__ */ __name((r, branches) => {
5
+ if (r.isOk()) {
6
+ return branches.Ok(r.unwrap());
7
+ }
8
+ return branches.Err(r.unwrapErr());
9
+ }, "matchRes");
10
+ const matchOpt = /* @__PURE__ */ __name((o, branches) => {
11
+ if (o.isSome()) {
12
+ return branches.Some(o.unwrap());
13
+ }
14
+ return branches.None();
15
+ }, "matchOpt");
16
+ export {
17
+ matchOpt,
18
+ matchRes
19
+ };
20
+ //# sourceMappingURL=match.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/match.ts"],"sourcesContent":["import type { Option } from \"./option.ts\";\nimport type { Result } from \"./result.ts\";\n\nexport const matchRes = <T, E, U>(\n r: Result<T, E>,\n branches: { Ok: (val: T) => U; Err: (err: E) => U },\n): U => {\n if (r.isOk()) {\n return branches.Ok(r.unwrap());\n }\n return branches.Err(r.unwrapErr());\n};\n\nexport const matchOpt = <T, U>(\n o: Option<T>,\n branches: { Some: (val: T) => U; None: () => U },\n): U => {\n if (o.isSome()) {\n return branches.Some(o.unwrap());\n }\n return branches.None();\n};\n"],"mappings":";;;AAGO,MAAMA,WAAW,wBACtBC,GACAC,aAAAA;AAEA,MAAID,EAAEE,KAAI,GAAI;AACZ,WAAOD,SAASE,GAAGH,EAAEI,OAAM,CAAA;EAC7B;AACA,SAAOH,SAASI,IAAIL,EAAEM,UAAS,CAAA;AACjC,GARwB;AAUjB,MAAMC,WAAW,wBACtBC,GACAP,aAAAA;AAEA,MAAIO,EAAEC,OAAM,GAAI;AACd,WAAOR,SAASS,KAAKF,EAAEJ,OAAM,CAAA;EAC/B;AACA,SAAOH,SAASU,KAAI;AACtB,GARwB;","names":["matchRes","r","branches","isOk","Ok","unwrap","Err","unwrapErr","matchOpt","o","isSome","Some","None"]}
@@ -0,0 +1,227 @@
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 option_exports = {};
21
+ __export(option_exports, {
22
+ Option: () => Option,
23
+ UnwrappedNone: () => UnwrappedNone
24
+ });
25
+ module.exports = __toCommonJS(option_exports);
26
+ var import_types = require("node:util/types");
27
+ var import_unit = require("./unit.js");
28
+ class UnwrappedNone extends Error {
29
+ static {
30
+ __name(this, "UnwrappedNone");
31
+ }
32
+ constructor() {
33
+ super("Unwrapped Option<None>");
34
+ }
35
+ }
36
+ const UNWRAPPED_NONE_ERR = new UnwrappedNone();
37
+ const NONE_VAL = Symbol.for("None");
38
+ class Option {
39
+ static {
40
+ __name(this, "Option");
41
+ }
42
+ val;
43
+ constructor(val) {
44
+ this.val = val;
45
+ }
46
+ static None = new Option(NONE_VAL);
47
+ static UNIT_OPT = new Option(import_unit.UNIT);
48
+ static Some(val) {
49
+ return new Option(val);
50
+ }
51
+ static fromNullable(val) {
52
+ return val === null ? Option.None : Option.Some(val);
53
+ }
54
+ isSome() {
55
+ return this.val !== NONE_VAL;
56
+ }
57
+ isNone() {
58
+ return this.val === NONE_VAL;
59
+ }
60
+ isUnit() {
61
+ return this.val === import_unit.UNIT;
62
+ }
63
+ unwrap() {
64
+ if (this.val !== NONE_VAL) {
65
+ return this.val;
66
+ }
67
+ throw UNWRAPPED_NONE_ERR;
68
+ }
69
+ /* Useful to serialize. Inverse operation for `fromNullable` */
70
+ safeUnwrap() {
71
+ if (this.val !== NONE_VAL) return this.val;
72
+ return null;
73
+ }
74
+ toString() {
75
+ if (this.isNone()) return "Option::None";
76
+ return `Option::Some(${this.val})`;
77
+ }
78
+ map(fn) {
79
+ if (this.isNone()) return Option.None;
80
+ const val = fn(this.val);
81
+ if ((0, import_types.isPromise)(val)) return val.then((u) => Option.Some(u));
82
+ return Option.Some(val);
83
+ }
84
+ mapOr(def, fn) {
85
+ if (this.isNone()) return Option.Some(def);
86
+ const val = fn(this.val);
87
+ if ((0, import_types.isPromise)(val)) return val.then((u) => Option.Some(u));
88
+ return Option.Some(val);
89
+ }
90
+ flatMap(fn) {
91
+ if (this.val !== NONE_VAL) return fn(this.val);
92
+ return Option.None;
93
+ }
94
+ bind(fn) {
95
+ if (this.val !== NONE_VAL) return fn(this.val);
96
+ return Option.None;
97
+ }
98
+ and(opt) {
99
+ if (this.val !== NONE_VAL && opt.val !== NONE_VAL) return Option.Some([
100
+ this.val,
101
+ opt.val
102
+ ]);
103
+ return Option.None;
104
+ }
105
+ tap(fn) {
106
+ if (this.val !== NONE_VAL) fn(this.val);
107
+ return this;
108
+ }
109
+ async tapAsync(fn) {
110
+ if (this.val !== NONE_VAL) {
111
+ await fn(this.val);
112
+ }
113
+ return this;
114
+ }
115
+ zip(fn) {
116
+ if (this.val === NONE_VAL) return Option.None;
117
+ const r = fn(this.val);
118
+ if ((0, import_types.isPromise)(r)) {
119
+ return r.then((u) => Option.Some([
120
+ this.val,
121
+ u
122
+ ]));
123
+ }
124
+ return Option.Some([
125
+ this.val,
126
+ r
127
+ ]);
128
+ }
129
+ flatZip(fn) {
130
+ if (this.val === NONE_VAL) return Option.None;
131
+ const r = fn(this.val);
132
+ if ((0, import_types.isPromise)(r)) {
133
+ return r.then((opt) => opt.map((u) => [
134
+ this.val,
135
+ u
136
+ ]));
137
+ }
138
+ return r.map((u) => [
139
+ this.val,
140
+ u
141
+ ]);
142
+ }
143
+ // flatZip<U>(
144
+ // fn: (val: T) => Promise<Option<U>>,
145
+ // ): Promise<Option<AppendToTuple<T, U>>>;
146
+ // flatZip<U>(fn: (val: T) => Option<U>): Option<AppendToTuple<T, U>>;
147
+ // flatZip<U>(fn: OptMapper<T, U> | AsyncOptMapper<T, U>) {
148
+ // if (this.val === NONE_VAL) return Option.None;
149
+ //
150
+ // type Tup = AppendToTuple<T, U>;
151
+ // const isTuple = Array.isArray(this.val);
152
+ // const r = fn(this.val);
153
+ // if (isPromise(r)) {
154
+ // return r.then((opt) =>
155
+ // opt.map(
156
+ // (u) =>
157
+ // setAsTuple(
158
+ // isTuple ? [...this.val, u] : [this.val, u],
159
+ // ) as Tuple<Tup>,
160
+ // ),
161
+ // );
162
+ // }
163
+ //
164
+ // return r.map(
165
+ // (u) =>
166
+ // setAsTuple(isTuple ? [...this.val, u] : [this.val, u]) as Tuple<Tup>,
167
+ // );
168
+ // }
169
+ static any(seq) {
170
+ for (const el of seq) {
171
+ if (el.val !== NONE_VAL) return true;
172
+ }
173
+ return false;
174
+ }
175
+ static all(seq) {
176
+ for (const el of seq) {
177
+ if (el.val === NONE_VAL) return false;
178
+ }
179
+ return true;
180
+ }
181
+ static sequence(...options) {
182
+ const vals = [];
183
+ for (const opt of options) {
184
+ if (opt.val === NONE_VAL) return Option.None;
185
+ vals.push(opt.val);
186
+ }
187
+ return Option.Some(vals);
188
+ }
189
+ static async lift(opt) {
190
+ if (opt.isNone()) return Promise.resolve(Option.None);
191
+ return opt.val.then((val) => Option.Some(val));
192
+ }
193
+ // innerMap<Inner, U>(
194
+ // this: Option<Array<Inner>>,
195
+ // mapper: (val: Inner) => U,
196
+ // ): Option<Array<U>>;
197
+ // innerMap<U>(this: Option<T>, mapper: (val: never) => U): never;
198
+ // innerMap<U>(this: Option<T>, mapper: (val: T) => U) {
199
+ // if (this.isNone()) return this;
200
+ //
201
+ // if (Array.isArray(this.val)) {
202
+ // return Option.Some(this.val.map(mapper));
203
+ // }
204
+ //
205
+ // throw new Error("Can only be called for Option<Array<T>>");
206
+ // }
207
+ innerMap(mapper) {
208
+ if (this.val === NONE_VAL) return this;
209
+ if (Array.isArray(this.val)) {
210
+ return Option.Some(this.val.map(mapper));
211
+ }
212
+ throw new Error("Can only be called for Option<Array<T>>");
213
+ }
214
+ pipe(...fns) {
215
+ let x = this;
216
+ for (const fn of fns) {
217
+ x = x.map(fn);
218
+ }
219
+ return x;
220
+ }
221
+ }
222
+ // Annotate the CommonJS export names for ESM import in node:
223
+ 0 && (module.exports = {
224
+ Option,
225
+ UnwrappedNone
226
+ });
227
+ //# sourceMappingURL=option.cjs.map
@@ -0,0 +1 @@
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 // @ts-expect-error\n x = x.map(fn);\n }\n return x;\n }\n}\n"],"mappings":"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","names":["UnwrappedNone","Error","constructor","UNWRAPPED_NONE_ERR","NONE_VAL","Symbol","for","Option","val","None","UNIT_OPT","UNIT","Some","fromNullable","isSome","isNone","isUnit","unwrap","safeUnwrap","toString","map","fn","isPromise","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"]}
@@ -0,0 +1,58 @@
1
+ import { UNIT } from './unit.cjs';
2
+
3
+ type InnerMapMapper<T, U> = T extends Array<infer Inner> ? (val: Inner) => U : never;
4
+ type InnerMapReturn<T, U> = T extends Array<unknown> ? Option<Array<U>> : never;
5
+ declare class UnwrappedNone extends Error {
6
+ constructor();
7
+ }
8
+ type UnitOption = Option<UNIT>;
9
+ type UnwrapOption<T> = T extends Option<infer R> ? R : never;
10
+ type CombinedOptions<T extends Option<unknown>[]> = {
11
+ [K in keyof T]: UnwrapOption<T[K]>;
12
+ };
13
+ declare class Option<T> {
14
+ readonly val: T;
15
+ private constructor();
16
+ static readonly None: Option<never>;
17
+ static readonly UNIT_OPT: UnitOption;
18
+ static Some<Inner>(val: Inner): Option<Inner>;
19
+ static fromNullable<T>(val: T): Option<NonNullable<T>>;
20
+ isSome(): this is Option<T>;
21
+ isNone(): this is Option<never>;
22
+ isUnit(): this is Option<UNIT>;
23
+ unwrap(): T;
24
+ safeUnwrap(): T | null;
25
+ toString(): string;
26
+ map<U>(fn: (val: T) => U): Option<U>;
27
+ map<U>(fn: (val: T) => Promise<U>): Promise<Option<U>>;
28
+ mapOr<U>(def: U, fn: (val: T) => U): Option<U>;
29
+ mapOr<U>(def: U, fn: (val: T) => Promise<U>): Promise<Option<U>>;
30
+ flatMap<U>(fn: (val: T) => Option<U>): Option<U>;
31
+ flatMap<U>(fn: (val: T) => Promise<Option<U>>): Promise<Option<U>>;
32
+ bind<U>(fn: (val: T) => Option<U>): Option<U>;
33
+ bind<U>(fn: (val: T) => Promise<Option<U>>): Promise<Option<U>>;
34
+ and<U>(opt: Option<U>): Option<[T, U]>;
35
+ tap(fn: (val: T) => void): Option<T>;
36
+ tapAsync(fn: (val: T) => Promise<unknown>): Promise<Option<T>>;
37
+ zip<U>(fn: (val: T) => Promise<U>): Promise<Option<[T, U]>>;
38
+ zip<U>(fn: (val: T) => U): Option<[T, U]>;
39
+ flatZip<U>(fn: (val: T) => Promise<Option<U>>): Promise<Option<[T, U]>>;
40
+ flatZip<U>(fn: (val: T) => Option<U>): Option<[T, U]>;
41
+ static any<T>(seq: Option<T>[]): boolean;
42
+ static all<T>(seq: Option<T>[]): boolean;
43
+ static sequence<T extends Option<unknown>[]>(...options: T): Option<CombinedOptions<T>>;
44
+ static lift<T>(opt: Option<Promise<T>>): Promise<Option<T>>;
45
+ innerMap<U>(mapper: InnerMapMapper<T, U>): InnerMapReturn<T, U>;
46
+ pipe<U>(fn: (val: T) => U): Option<U>;
47
+ pipe<U, V>(fn: (val: T) => U, fn2: (val: U) => V): Option<V>;
48
+ pipe<U, V, W>(fn: (val: T) => U, fn2: (val: U) => V, fn3: (val: V) => W): Option<W>;
49
+ pipe<U, V, W, X>(fn: (val: T) => U, fn2: (val: U) => V, fn3: (val: V) => W, fn4: (val: W) => X): Option<X>;
50
+ pipe<U, V, W, X, Y>(fn: (val: T) => U, fn2: (val: U) => V, fn3: (val: V) => W, fn4: (val: W) => X, fn5: (val: X) => Y): Option<Y>;
51
+ pipe<U, V, W, X, Y, Z>(fn: (val: T) => U, fn2: (val: U) => V, fn3: (val: V) => W, fn4: (val: W) => X, fn5: (val: X) => Y, fn6: (val: Y) => Z): Option<Z>;
52
+ pipe<U, V, W, X, Y, Z, A>(fn: (val: T) => U, fn2: (val: U) => V, fn3: (val: V) => W, fn4: (val: W) => X, fn5: (val: X) => Y, fn6: (val: Y) => Z, fn7: (val: Z) => A): Option<A>;
53
+ pipe<U, V, W, X, Y, Z, A, B>(fn: (val: T) => U, fn2: (val: U) => V, fn3: (val: V) => W, fn4: (val: W) => X, fn5: (val: X) => Y, fn6: (val: Y) => Z, fn7: (val: Z) => A, fn8: (val: A) => B): Option<B>;
54
+ pipe<U, V, W, X, Y, Z, A, B, C>(fn: (val: T) => U, fn2: (val: U) => V, fn3: (val: V) => W, fn4: (val: W) => X, fn5: (val: X) => Y, fn6: (val: Y) => Z, fn7: (val: Z) => A, fn8: (val: A) => B, fn9: (val: B) => C): Option<C>;
55
+ pipe<U, V, W, X, Y, Z, A, B, C, D>(fn: (val: T) => U, fn2: (val: U) => V, fn3: (val: V) => W, fn4: (val: W) => X, fn5: (val: X) => Y, fn6: (val: Y) => Z, fn7: (val: Z) => A, fn8: (val: A) => B, fn9: (val: B) => C, fn10: (val: C) => D): Option<D>;
56
+ }
57
+
58
+ export { Option, type UnitOption, type UnwrapOption, UnwrappedNone };
@@ -0,0 +1,58 @@
1
+ import { UNIT } from './unit.js';
2
+
3
+ type InnerMapMapper<T, U> = T extends Array<infer Inner> ? (val: Inner) => U : never;
4
+ type InnerMapReturn<T, U> = T extends Array<unknown> ? Option<Array<U>> : never;
5
+ declare class UnwrappedNone extends Error {
6
+ constructor();
7
+ }
8
+ type UnitOption = Option<UNIT>;
9
+ type UnwrapOption<T> = T extends Option<infer R> ? R : never;
10
+ type CombinedOptions<T extends Option<unknown>[]> = {
11
+ [K in keyof T]: UnwrapOption<T[K]>;
12
+ };
13
+ declare class Option<T> {
14
+ readonly val: T;
15
+ private constructor();
16
+ static readonly None: Option<never>;
17
+ static readonly UNIT_OPT: UnitOption;
18
+ static Some<Inner>(val: Inner): Option<Inner>;
19
+ static fromNullable<T>(val: T): Option<NonNullable<T>>;
20
+ isSome(): this is Option<T>;
21
+ isNone(): this is Option<never>;
22
+ isUnit(): this is Option<UNIT>;
23
+ unwrap(): T;
24
+ safeUnwrap(): T | null;
25
+ toString(): string;
26
+ map<U>(fn: (val: T) => U): Option<U>;
27
+ map<U>(fn: (val: T) => Promise<U>): Promise<Option<U>>;
28
+ mapOr<U>(def: U, fn: (val: T) => U): Option<U>;
29
+ mapOr<U>(def: U, fn: (val: T) => Promise<U>): Promise<Option<U>>;
30
+ flatMap<U>(fn: (val: T) => Option<U>): Option<U>;
31
+ flatMap<U>(fn: (val: T) => Promise<Option<U>>): Promise<Option<U>>;
32
+ bind<U>(fn: (val: T) => Option<U>): Option<U>;
33
+ bind<U>(fn: (val: T) => Promise<Option<U>>): Promise<Option<U>>;
34
+ and<U>(opt: Option<U>): Option<[T, U]>;
35
+ tap(fn: (val: T) => void): Option<T>;
36
+ tapAsync(fn: (val: T) => Promise<unknown>): Promise<Option<T>>;
37
+ zip<U>(fn: (val: T) => Promise<U>): Promise<Option<[T, U]>>;
38
+ zip<U>(fn: (val: T) => U): Option<[T, U]>;
39
+ flatZip<U>(fn: (val: T) => Promise<Option<U>>): Promise<Option<[T, U]>>;
40
+ flatZip<U>(fn: (val: T) => Option<U>): Option<[T, U]>;
41
+ static any<T>(seq: Option<T>[]): boolean;
42
+ static all<T>(seq: Option<T>[]): boolean;
43
+ static sequence<T extends Option<unknown>[]>(...options: T): Option<CombinedOptions<T>>;
44
+ static lift<T>(opt: Option<Promise<T>>): Promise<Option<T>>;
45
+ innerMap<U>(mapper: InnerMapMapper<T, U>): InnerMapReturn<T, U>;
46
+ pipe<U>(fn: (val: T) => U): Option<U>;
47
+ pipe<U, V>(fn: (val: T) => U, fn2: (val: U) => V): Option<V>;
48
+ pipe<U, V, W>(fn: (val: T) => U, fn2: (val: U) => V, fn3: (val: V) => W): Option<W>;
49
+ pipe<U, V, W, X>(fn: (val: T) => U, fn2: (val: U) => V, fn3: (val: V) => W, fn4: (val: W) => X): Option<X>;
50
+ pipe<U, V, W, X, Y>(fn: (val: T) => U, fn2: (val: U) => V, fn3: (val: V) => W, fn4: (val: W) => X, fn5: (val: X) => Y): Option<Y>;
51
+ pipe<U, V, W, X, Y, Z>(fn: (val: T) => U, fn2: (val: U) => V, fn3: (val: V) => W, fn4: (val: W) => X, fn5: (val: X) => Y, fn6: (val: Y) => Z): Option<Z>;
52
+ pipe<U, V, W, X, Y, Z, A>(fn: (val: T) => U, fn2: (val: U) => V, fn3: (val: V) => W, fn4: (val: W) => X, fn5: (val: X) => Y, fn6: (val: Y) => Z, fn7: (val: Z) => A): Option<A>;
53
+ pipe<U, V, W, X, Y, Z, A, B>(fn: (val: T) => U, fn2: (val: U) => V, fn3: (val: V) => W, fn4: (val: W) => X, fn5: (val: X) => Y, fn6: (val: Y) => Z, fn7: (val: Z) => A, fn8: (val: A) => B): Option<B>;
54
+ pipe<U, V, W, X, Y, Z, A, B, C>(fn: (val: T) => U, fn2: (val: U) => V, fn3: (val: V) => W, fn4: (val: W) => X, fn5: (val: X) => Y, fn6: (val: Y) => Z, fn7: (val: Z) => A, fn8: (val: A) => B, fn9: (val: B) => C): Option<C>;
55
+ pipe<U, V, W, X, Y, Z, A, B, C, D>(fn: (val: T) => U, fn2: (val: U) => V, fn3: (val: V) => W, fn4: (val: W) => X, fn5: (val: X) => Y, fn6: (val: Y) => Z, fn7: (val: Z) => A, fn8: (val: A) => B, fn9: (val: B) => C, fn10: (val: C) => D): Option<D>;
56
+ }
57
+
58
+ export { Option, type UnitOption, type UnwrapOption, UnwrappedNone };