@carbonteq/fp 0.7.0 → 0.8.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1,46 +1,44 @@
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
1
+ 'use strict';
2
+
3
+ require('./chunk-PK6SKIKE.cjs');
4
+ var result_js = require('./result.js');
5
+ var option_js = require('./option.js');
6
+ var match_js = require('./match.js');
7
+ var unit_js = require('./unit.js');
8
+
9
+
10
+
11
+ Object.defineProperty(exports, "Result", {
12
+ enumerable: true,
13
+ get: function () { return result_js.Result; }
45
14
  });
15
+ Object.defineProperty(exports, "UnwrappedErrWithOk", {
16
+ enumerable: true,
17
+ get: function () { return result_js.UnwrappedErrWithOk; }
18
+ });
19
+ Object.defineProperty(exports, "UnwrappedOkWithErr", {
20
+ enumerable: true,
21
+ get: function () { return result_js.UnwrappedOkWithErr; }
22
+ });
23
+ Object.defineProperty(exports, "Option", {
24
+ enumerable: true,
25
+ get: function () { return option_js.Option; }
26
+ });
27
+ Object.defineProperty(exports, "UnwrappedNone", {
28
+ enumerable: true,
29
+ get: function () { return option_js.UnwrappedNone; }
30
+ });
31
+ Object.defineProperty(exports, "matchOpt", {
32
+ enumerable: true,
33
+ get: function () { return match_js.matchOpt; }
34
+ });
35
+ Object.defineProperty(exports, "matchRes", {
36
+ enumerable: true,
37
+ get: function () { return match_js.matchRes; }
38
+ });
39
+ Object.defineProperty(exports, "UNIT", {
40
+ enumerable: true,
41
+ get: function () { return unit_js.UNIT; }
42
+ });
43
+ //# sourceMappingURL=index.cjs.map
46
44
  //# sourceMappingURL=index.cjs.map
@@ -1 +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":[]}
1
+ {"version":3,"sources":[],"names":[],"mappings":"","file":"index.cjs","sourcesContent":[]}
package/dist/index.js CHANGED
@@ -1,16 +1,7 @@
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
- };
1
+ import './chunk-7QVYU63E.js';
2
+ export { Result, UnwrappedErrWithOk, UnwrappedOkWithErr } from './result.js';
3
+ export { Option, UnwrappedNone } from './option.js';
4
+ export { matchOpt, matchRes } from './match.js';
5
+ export { UNIT } from './unit.js';
6
+ //# sourceMappingURL=index.js.map
16
7
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +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"]}
1
+ {"version":3,"sources":[],"names":[],"mappings":"","file":"index.js","sourcesContent":[]}
package/dist/match.cjs CHANGED
@@ -1,43 +1,21 @@
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) => {
1
+ 'use strict';
2
+
3
+ var chunkPK6SKIKE_cjs = require('./chunk-PK6SKIKE.cjs');
4
+
5
+ const matchRes = /* @__PURE__ */ chunkPK6SKIKE_cjs.__name((r, branches) => {
27
6
  if (r.isOk()) {
28
7
  return branches.Ok(r.unwrap());
29
8
  }
30
9
  return branches.Err(r.unwrapErr());
31
10
  }, "matchRes");
32
- const matchOpt = /* @__PURE__ */ __name((o, branches) => {
11
+ const matchOpt = /* @__PURE__ */ chunkPK6SKIKE_cjs.__name((o, branches) => {
33
12
  if (o.isSome()) {
34
13
  return branches.Some(o.unwrap());
35
14
  }
36
15
  return branches.None();
37
16
  }, "matchOpt");
38
- // Annotate the CommonJS export names for ESM import in node:
39
- 0 && (module.exports = {
40
- matchOpt,
41
- matchRes
42
- });
17
+
18
+ exports.matchOpt = matchOpt;
19
+ exports.matchRes = matchRes;
20
+ //# sourceMappingURL=match.cjs.map
43
21
  //# sourceMappingURL=match.cjs.map
@@ -1 +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"]}
1
+ {"version":3,"sources":["../src/match.ts"],"names":["matchRes","r","branches","isOk","Ok","unwrap","Err","unwrapErr","matchOpt","o","isSome","Some","None"],"mappings":";;;;AAGaA,MAAAA,QAAAA,mBACXC,wBAAAA,CAAAA,CAAAA,CAAAA,EACAC,QAAAA,KAAAA;AAEA,EAAID,IAAAA,CAAAA,CAAEE,MAAQ,EAAA;AACZ,IAAA,OAAOD,QAASE,CAAAA,EAAAA,CAAGH,CAAEI,CAAAA,MAAAA,EAAM,CAAA;AAC7B;AAEA,EAAA,OAAOH,QAASI,CAAAA,GAAAA,CAAIL,CAAEM,CAAAA,SAAAA,EAAS,CAAA;AACjC,CATwB,EAAA,UAAA;AAWXC,MAAAA,QAAAA,mBACXC,wBAAAA,CAAAA,CAAAA,CAAAA,EACAP,QAAAA,KAAAA;AAEA,EAAIO,IAAAA,CAAAA,CAAEC,QAAU,EAAA;AACd,IAAA,OAAOR,QAASS,CAAAA,IAAAA,CAAKF,CAAEJ,CAAAA,MAAAA,EAAM,CAAA;AAC/B;AACA,EAAA,OAAOH,SAASU,IAAI,EAAA;AACtB,CARwB,EAAA,UAAA","file":"match.cjs","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\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"]}
package/dist/match.js CHANGED
@@ -1,6 +1,5 @@
1
- import {
2
- __name
3
- } from "./chunk-7QVYU63E.js";
1
+ import { __name } from './chunk-7QVYU63E.js';
2
+
4
3
  const matchRes = /* @__PURE__ */ __name((r, branches) => {
5
4
  if (r.isOk()) {
6
5
  return branches.Ok(r.unwrap());
@@ -13,8 +12,7 @@ const matchOpt = /* @__PURE__ */ __name((o, branches) => {
13
12
  }
14
13
  return branches.None();
15
14
  }, "matchOpt");
16
- export {
17
- matchOpt,
18
- matchRes
19
- };
15
+
16
+ export { matchOpt, matchRes };
17
+ //# sourceMappingURL=match.js.map
20
18
  //# sourceMappingURL=match.js.map
package/dist/match.js.map CHANGED
@@ -1 +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"]}
1
+ {"version":3,"sources":["../src/match.ts"],"names":["matchRes","r","branches","isOk","Ok","unwrap","Err","unwrapErr","matchOpt","o","isSome","Some","None"],"mappings":";;AAGaA,MAAAA,QAAAA,mBACXC,MAAAA,CAAAA,CAAAA,CAAAA,EACAC,QAAAA,KAAAA;AAEA,EAAID,IAAAA,CAAAA,CAAEE,MAAQ,EAAA;AACZ,IAAA,OAAOD,QAASE,CAAAA,EAAAA,CAAGH,CAAEI,CAAAA,MAAAA,EAAM,CAAA;AAC7B;AAEA,EAAA,OAAOH,QAASI,CAAAA,GAAAA,CAAIL,CAAEM,CAAAA,SAAAA,EAAS,CAAA;AACjC,CATwB,EAAA,UAAA;AAWXC,MAAAA,QAAAA,mBACXC,MAAAA,CAAAA,CAAAA,CAAAA,EACAP,QAAAA,KAAAA;AAEA,EAAIO,IAAAA,CAAAA,CAAEC,QAAU,EAAA;AACd,IAAA,OAAOR,QAASS,CAAAA,IAAAA,CAAKF,CAAEJ,CAAAA,MAAAA,EAAM,CAAA;AAC/B;AACA,EAAA,OAAOH,SAASU,IAAI,EAAA;AACtB,CARwB,EAAA,UAAA","file":"match.js","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\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"]}
package/dist/option.cjs CHANGED
@@ -1,33 +1,12 @@
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");
1
+ 'use strict';
2
+
3
+ var chunkPK6SKIKE_cjs = require('./chunk-PK6SKIKE.cjs');
4
+ var types = require('node:util/types');
5
+ var unit_js = require('./unit.js');
6
+
28
7
  class UnwrappedNone extends Error {
29
8
  static {
30
- __name(this, "UnwrappedNone");
9
+ chunkPK6SKIKE_cjs.__name(this, "UnwrappedNone");
31
10
  }
32
11
  constructor() {
33
12
  super("Unwrapped Option<None>");
@@ -37,16 +16,30 @@ const UNWRAPPED_NONE_ERR = new UnwrappedNone();
37
16
  const NONE_VAL = Symbol.for("None");
38
17
  class Option {
39
18
  static {
40
- __name(this, "Option");
19
+ chunkPK6SKIKE_cjs.__name(this, "Option");
41
20
  }
42
21
  val;
43
- constructor(val) {
22
+ #ctx;
23
+ constructor(val, ctx) {
44
24
  this.val = val;
25
+ this.#ctx = ctx;
26
+ }
27
+ static None = new Option(NONE_VAL, {
28
+ promiseNoneSlot: true
29
+ });
30
+ static safeMap(p, mapper, ctx) {
31
+ return p.then((v) => {
32
+ if (v === NONE_VAL) {
33
+ ctx.promiseNoneSlot = true;
34
+ return NONE_VAL;
35
+ }
36
+ return mapper(v);
37
+ });
45
38
  }
46
- static None = new Option(NONE_VAL);
47
- static UNIT_OPT = new Option(import_unit.UNIT);
48
39
  static Some(val) {
49
- return new Option(val);
40
+ return new Option(val, {
41
+ promiseNoneSlot: false
42
+ });
50
43
  }
51
44
  static fromNullable(val) {
52
45
  return val === null ? Option.None : Option.Some(val);
@@ -55,173 +48,183 @@ class Option {
55
48
  return this.val !== NONE_VAL;
56
49
  }
57
50
  isNone() {
58
- return this.val === NONE_VAL;
51
+ return this.val === NONE_VAL || types.isPromise(this.val) && this.#ctx.promiseNoneSlot;
59
52
  }
60
53
  isUnit() {
61
- return this.val === import_unit.UNIT;
54
+ return this.val === unit_js.UNIT;
62
55
  }
63
56
  unwrap() {
64
- if (this.val !== NONE_VAL) {
65
- return this.val;
57
+ if (this.isNone()) {
58
+ throw UNWRAPPED_NONE_ERR;
66
59
  }
67
- throw UNWRAPPED_NONE_ERR;
60
+ return this.val;
68
61
  }
69
- /* Useful to serialize. Inverse operation for `fromNullable` */
70
62
  safeUnwrap() {
71
- if (this.val !== NONE_VAL) return this.val;
72
- return null;
63
+ if (this.isNone()) return null;
64
+ return this.val;
73
65
  }
74
- toString() {
75
- if (this.isNone()) return "Option::None";
76
- return `Option::Some(${this.val})`;
77
- }
78
- map(fn) {
66
+ map(mapper) {
79
67
  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;
68
+ const ctx = this.#ctx;
69
+ const curr = this.val;
70
+ if (types.isPromise(curr)) {
71
+ const p = curr;
72
+ const safetlyMapped = Option.safeMap(p, mapper, ctx);
73
+ return new Option(safetlyMapped, ctx);
74
+ }
75
+ const transformed = mapper(curr);
76
+ return new Option(transformed, ctx);
77
+ }
78
+ mapOr(default_, mapper) {
79
+ if (this.isNone()) return new Option(default_, this.#ctx);
80
+ const ctx = this.#ctx;
81
+ const curr = this.val;
82
+ if (types.isPromise(curr)) {
83
+ const p = curr;
84
+ const safetlyMapped = Option.safeMap(p, mapper, ctx);
85
+ return new Option(safetlyMapped, ctx);
86
+ }
87
+ const transformed = mapper(curr);
88
+ return new Option(transformed, ctx);
108
89
  }
109
- async tapAsync(fn) {
110
- if (this.val !== NONE_VAL) {
111
- await fn(this.val);
90
+ flatMap(mapper) {
91
+ if (this.isNone()) return Option.None;
92
+ const ctx = this.#ctx;
93
+ const curr = this.val;
94
+ if (types.isPromise(curr)) {
95
+ const p = curr;
96
+ const castedNone = NONE_VAL;
97
+ const newP = new Promise((resolve, reject) => {
98
+ p.then((curr2) => {
99
+ if (curr2 === NONE_VAL) {
100
+ ctx.promiseNoneSlot = true;
101
+ resolve(castedNone);
102
+ } else {
103
+ const r = mapper(curr2);
104
+ if (types.isPromise(r)) {
105
+ r.then((innerOpt) => resolve(innerOpt.val), reject);
106
+ } else {
107
+ resolve(r.val);
108
+ }
109
+ }
110
+ });
111
+ });
112
+ return new Option(newP, ctx);
113
+ }
114
+ const mapped = mapper(curr);
115
+ if (types.isPromise(mapped)) {
116
+ return Option.fromPromise(mapped);
112
117
  }
113
- return this;
118
+ return mapped;
114
119
  }
115
120
  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
- ]));
121
+ if (this.isNone()) return Option.None;
122
+ const curr = this.val;
123
+ const ctx = this.#ctx;
124
+ if (types.isPromise(curr)) {
125
+ const val = curr;
126
+ const p = val.then(async (v) => {
127
+ if (v === NONE_VAL) {
128
+ ctx.promiseNoneSlot = true;
129
+ return NONE_VAL;
130
+ }
131
+ const next = await fn(v);
132
+ return [
133
+ v,
134
+ next
135
+ ];
136
+ });
137
+ return new Option(p, ctx);
123
138
  }
124
- return Option.Some([
125
- this.val,
126
- r
127
- ]);
139
+ const u = fn(curr);
140
+ if (types.isPromise(u)) {
141
+ const p = u.then((uu) => [
142
+ curr,
143
+ uu
144
+ ]);
145
+ return new Option(p, ctx);
146
+ }
147
+ return new Option([
148
+ curr,
149
+ u
150
+ ], ctx);
128
151
  }
129
152
  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
- ]));
153
+ if (this.isNone()) return Option.None;
154
+ const curr = this.val;
155
+ const ctx = this.#ctx;
156
+ if (types.isPromise(curr)) {
157
+ const val = curr;
158
+ const p = val.then(async (v) => {
159
+ if (v === NONE_VAL) {
160
+ ctx.promiseNoneSlot = true;
161
+ return NONE_VAL;
162
+ }
163
+ const next = await fn(v);
164
+ if (next.isNone()) {
165
+ return NONE_VAL;
166
+ }
167
+ return [
168
+ v,
169
+ next.val
170
+ ];
171
+ });
172
+ return new Option(p, ctx);
137
173
  }
138
- return r.map((u) => [
139
- this.val,
140
- u
174
+ const c = curr;
175
+ const u = fn(c);
176
+ if (types.isPromise(u)) {
177
+ const p = u.then((uu) => {
178
+ if (uu.isNone()) {
179
+ return NONE_VAL;
180
+ }
181
+ return [
182
+ c,
183
+ uu.val
184
+ ];
185
+ });
186
+ return new Option(p, ctx);
187
+ }
188
+ return u.map((inner) => [
189
+ c,
190
+ inner
141
191
  ]);
142
192
  }
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;
193
+ async toPromise() {
194
+ const curr = this.val;
195
+ let inner;
196
+ if (types.isPromise(curr)) {
197
+ const awaited = await curr;
198
+ inner = awaited;
199
+ } else {
200
+ inner = curr;
172
201
  }
173
- return false;
202
+ return new Option(inner, this.#ctx);
203
+ }
204
+ static fromPromise(o) {
205
+ const ctx = {
206
+ promiseNoneSlot: false
207
+ };
208
+ const p = new Promise((resolve, reject) => o.then((innerOpt) => {
209
+ ctx.promiseNoneSlot ||= innerOpt.#ctx.promiseNoneSlot;
210
+ resolve(innerOpt.val);
211
+ }, reject));
212
+ return new Option(p, ctx);
174
213
  }
175
- static all(seq) {
176
- for (const el of seq) {
177
- if (el.val === NONE_VAL) return false;
178
- }
179
- return true;
214
+ toString() {
215
+ if (this.isNone()) return "Option::None";
216
+ return `Option::Some(${this.val})`;
180
217
  }
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
218
  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);
219
+ if (this.isNone()) return Option.None;
220
+ if (!Array.isArray(this.val)) {
221
+ throw new Error("Can only be called for Option<Array<T>>");
218
222
  }
219
- return x;
223
+ return new Option(this.val.map(mapper), this.#ctx);
220
224
  }
221
225
  }
222
- // Annotate the CommonJS export names for ESM import in node:
223
- 0 && (module.exports = {
224
- Option,
225
- UnwrappedNone
226
- });
226
+
227
+ exports.Option = Option;
228
+ exports.UnwrappedNone = UnwrappedNone;
229
+ //# sourceMappingURL=option.cjs.map
227
230
  //# sourceMappingURL=option.cjs.map