@terrygonguet/utils 0.0.11 → 0.0.12

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.
@@ -1,229 +1,260 @@
1
1
  var d = Object.defineProperty;
2
- var v = (t, e, r) => e in t ? d(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r;
3
- var _ = (t, e, r) => (v(t, typeof e != "symbol" ? e + "" : e, r), r);
4
- const o = "@terrygonguet/utils/functional/result", m = "@terrygonguet/utils/functional/result/Success", p = "@terrygonguet/utils/functional/result/Failure";
5
- function i(t) {
6
- return Object.create(
7
- {
8
- isSuccess: () => !0,
9
- isFailure: () => !1,
10
- merge(e) {
11
- return e(this.value);
12
- },
13
- match(e) {
14
- return e(this.value);
15
- },
16
- map(e) {
17
- return i(e(this.value));
18
- },
19
- flatMap(e) {
20
- return e(this.value);
21
- },
22
- toJSON() {
23
- return { $_kind: o, $_variant: m, value: this.value };
24
- }
25
- },
26
- {
27
- $_kind: { value: o, enumerable: !1, writable: !1 },
28
- $_variant: { value: m, enumerable: !1, writable: !1 },
29
- value: { value: t, writable: !1 }
30
- }
31
- );
2
+ var S = (r, e, t) => e in r ? d(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t;
3
+ var u = (r, e, t) => (S(r, typeof e != "symbol" ? e + "" : e, t), t);
4
+ const a = "@terrygonguet/utils/functional/result", l = "@terrygonguet/utils/functional/result/Success", w = "@terrygonguet/utils/functional/result/Failure";
5
+ class f {
6
+ constructor(e) {
7
+ u(this, "value");
8
+ Object.defineProperties(this, {
9
+ $_kind: { value: a, enumerable: !1, writable: !1 },
10
+ $_variant: { value: l, enumerable: !1, writable: !1 },
11
+ value: { value: e, writable: !1 }
12
+ });
13
+ }
14
+ isSuccess() {
15
+ return !0;
16
+ }
17
+ isFailure() {
18
+ return !1;
19
+ }
20
+ merge(e) {
21
+ return e(this.value);
22
+ }
23
+ match(e) {
24
+ return e(this.value);
25
+ }
26
+ map(e) {
27
+ return new f(e(this.value));
28
+ }
29
+ flatMap(e) {
30
+ return e(this.value);
31
+ }
32
+ toJSON() {
33
+ return { $_kind: a, $_variant: l, value: this.value };
34
+ }
32
35
  }
33
- function f(t) {
34
- return Object.create(
35
- {
36
- isSuccess: () => !1,
37
- isFailure: () => !0,
38
- merge(e, r) {
39
- return r(this.reason);
40
- },
41
- match(e, r) {
42
- return r(this.reason);
43
- },
44
- map() {
45
- return this;
46
- },
47
- flatMap() {
48
- return this;
49
- },
50
- toJSON() {
51
- return { $_kind: o, $_variant: p, reason: this.reason };
52
- }
53
- },
54
- {
55
- $_kind: { value: o, enumerable: !1, writable: !1 },
56
- $_variant: { value: m, enumerable: !1, writable: !1 },
57
- reason: { value: t, writable: !1 }
58
- }
59
- );
36
+ class g {
37
+ constructor(e) {
38
+ u(this, "reason");
39
+ Object.defineProperties(this, {
40
+ $_kind: { value: a, enumerable: !1, writable: !1 },
41
+ $_variant: { value: l, enumerable: !1, writable: !1 },
42
+ reason: { value: e, writable: !1 }
43
+ });
44
+ }
45
+ isSuccess() {
46
+ return !1;
47
+ }
48
+ isFailure() {
49
+ return !0;
50
+ }
51
+ merge(e, t) {
52
+ return t(this.reason);
53
+ }
54
+ match(e, t) {
55
+ return t(this.reason);
56
+ }
57
+ map() {
58
+ return this;
59
+ }
60
+ flatMap() {
61
+ return this;
62
+ }
63
+ toJSON() {
64
+ return { $_kind: a, $_variant: w, reason: this.reason };
65
+ }
60
66
  }
61
- function y(t, e) {
62
- return e ? t.toResult(e) : t.toResult();
67
+ function v(r, e) {
68
+ return e ? r.toResult(e) : r.toResult();
63
69
  }
64
- const w = {
65
- Success: i,
66
- Failure: f,
67
- try(t) {
68
- return new k(t);
70
+ const c = {
71
+ Success(r) {
72
+ return new f(r);
69
73
  },
70
- fromPromise(t, e, r) {
71
- return t.then($(e, i), $(r, f));
74
+ Failure(r) {
75
+ return new g(r);
72
76
  },
73
- fromMaybe: y,
74
- JSONReviver(t, e) {
75
- if ((e == null ? void 0 : e.$_kind) == o) {
76
- const r = e == null ? void 0 : e.$_variant;
77
- return r == m ? i(e == null ? void 0 : e.value) : r == p ? f(e == null ? void 0 : e.reason) : e;
77
+ try(r) {
78
+ return new y(r);
79
+ },
80
+ fromPromise(r, e, t) {
81
+ return r.then(
82
+ b(e, this.Success),
83
+ b(t, this.Failure)
84
+ );
85
+ },
86
+ fromMaybe: v,
87
+ JSONReviver(r, e) {
88
+ if ((e == null ? void 0 : e.$_kind) == a) {
89
+ const t = e == null ? void 0 : e.$_variant;
90
+ return t == l ? new f(e == null ? void 0 : e.value) : t == w ? new g(e == null ? void 0 : e.reason) : e;
78
91
  } else
79
92
  return e;
80
93
  }
81
94
  };
82
- class k {
95
+ class y {
83
96
  constructor(e) {
84
- _(this, "tryFn");
85
- _(this, "catchFn");
86
- this.tryFn = e, this.catchFn = S;
97
+ u(this, "tryFn");
98
+ u(this, "catchFn");
99
+ this.tryFn = e, this.catchFn = p;
87
100
  }
88
101
  catch(e) {
89
102
  return this.catchFn = e, this;
90
103
  }
91
104
  exec(e) {
92
105
  try {
93
- const r = i(this.tryFn());
94
- return e == null || e(r), r;
95
- } catch (r) {
96
- const n = f(this.catchFn(r));
106
+ const t = c.Success(this.tryFn());
107
+ return e == null || e(t), t;
108
+ } catch (t) {
109
+ const n = c.Failure(this.catchFn(t));
97
110
  return e == null || e(n), n;
98
111
  }
99
112
  }
100
113
  }
101
- const c = "@terrygonguet/utils/functional/maybe", b = "@terrygonguet/utils/functional/maybe/Some", g = "@terrygonguet/utils/functional/maybe/None";
102
- function l(t) {
103
- return Object.create(
104
- {
105
- isSome: () => !0,
106
- isNone: () => !1,
107
- orDefault() {
108
- return this.value;
109
- },
110
- map(e) {
111
- return l(e(this.value));
112
- },
113
- flatMap(e) {
114
- return e(this.value);
115
- },
116
- toResult() {
117
- return w.Success(this.value);
118
- },
119
- toJSON() {
120
- return { $_kind: c, $_variant: b, value: this.value };
121
- }
122
- },
123
- {
124
- $_kind: { value: c, enumerable: !1, writable: !1 },
125
- $_variant: { value: b, enumerable: !1, writable: !1 },
126
- value: { value: t, writable: !1 }
127
- }
128
- );
114
+ const h = "@terrygonguet/utils/functional/maybe", _ = "@terrygonguet/utils/functional/maybe/Some", $ = "@terrygonguet/utils/functional/maybe/None";
115
+ class o {
116
+ constructor(e) {
117
+ u(this, "value");
118
+ Object.defineProperties(this, {
119
+ $_kind: { value: h, enumerable: !1, writable: !1 },
120
+ $_variant: { value: _, enumerable: !1, writable: !1 },
121
+ value: { value: e, writable: !1 }
122
+ });
123
+ }
124
+ isSome() {
125
+ return !0;
126
+ }
127
+ isNone() {
128
+ return !1;
129
+ }
130
+ orDefault() {
131
+ return this.value;
132
+ }
133
+ map(e) {
134
+ return new o(e(this.value));
135
+ }
136
+ flatMap(e) {
137
+ return e(this.value);
138
+ }
139
+ toResult() {
140
+ return c.Success(this.value);
141
+ }
142
+ toJSON() {
143
+ return { $_kind: h, $_variant: _, value: this.value };
144
+ }
129
145
  }
130
- const u = Object.create(
131
- {
132
- isSome: () => !1,
133
- isNone: () => !0,
134
- orDefault: (t) => t,
135
- map: () => u,
136
- flatMap: () => u,
137
- toResult: (t) => w.Failure(t == null ? void 0 : t()),
138
- toJSON: () => ({ $_kind: c, $_variant: g })
146
+ class k {
147
+ constructor() {
148
+ }
149
+ isSome() {
150
+ return !1;
151
+ }
152
+ isNone() {
153
+ return !0;
154
+ }
155
+ orDefault(e) {
156
+ return e;
157
+ }
158
+ map() {
159
+ return this;
160
+ }
161
+ flatMap() {
162
+ return this;
163
+ }
164
+ toResult(e) {
165
+ return e ? c.Failure(e == null ? void 0 : e()) : c.Failure(void 0);
166
+ }
167
+ toJSON() {
168
+ return { $_kind: h, $_variant: $ };
169
+ }
170
+ }
171
+ const N = new k(), s = {
172
+ Some(r) {
173
+ return new o(r);
139
174
  },
140
- {
141
- $_kind: { value: c, enumerable: !1, writable: !1 },
142
- $_variant: { value: g, enumerable: !1, writable: !1 }
143
- }
144
- ), s = {
145
- Some: l,
146
- None: u,
147
- from(t) {
148
- switch (t) {
175
+ None() {
176
+ return N;
177
+ },
178
+ from(r) {
179
+ switch (r) {
149
180
  case null:
150
181
  case void 0:
151
- return u;
182
+ return this.None();
152
183
  default:
153
- return l(t);
184
+ return new o(r);
154
185
  }
155
186
  },
156
187
  /**
157
188
  * CAUTION: this method swallows errors and simply returns None!
158
189
  * Use `Result.fromPromise()` if you need error details.
159
190
  */
160
- fromPromise(t, e = S) {
161
- return t.then($(e, s.from), M(u));
191
+ fromPromise(r, e = p) {
192
+ return r.then(b(e, s.from), M(this.None()));
162
193
  },
163
- JSONReviver(t, e) {
164
- if ((e == null ? void 0 : e.$_kind) == c) {
165
- const r = e == null ? void 0 : e.$_variant;
166
- return r == b ? l(e == null ? void 0 : e.value) : r == g ? u : e;
194
+ JSONReviver(r, e) {
195
+ if ((e == null ? void 0 : e.$_kind) == h) {
196
+ const t = e == null ? void 0 : e.$_variant;
197
+ return t == _ ? new o(e == null ? void 0 : e.value) : t == $ ? s.None() : e;
167
198
  } else
168
199
  return e;
169
200
  }
170
201
  };
171
- var $ = x;
172
- function x(t, e) {
202
+ var b = F;
203
+ function F(r, e) {
173
204
  if (!arguments.length)
174
205
  throw new Error(
175
206
  "expected at least one (and probably more) function arguments"
176
207
  );
177
- var r = arguments;
208
+ var t = arguments;
178
209
  return function() {
179
- for (var n = r[0].apply(this, arguments), a = r.length, h = 1; h < a; h++)
180
- n = r[h].call(this, n);
210
+ for (var n = t[0].apply(this, arguments), i = t.length, m = 1; m < i; m++)
211
+ n = t[m].call(this, n);
181
212
  return n;
182
213
  };
183
214
  }
184
- var J = N;
185
- function N(t, ...e) {
215
+ var J = x;
216
+ function x(r, ...e) {
186
217
  if (!arguments.length)
187
218
  throw new Error("expected one value argument and least one function argument");
188
219
  if (!e.length)
189
220
  throw new Error(
190
221
  "expected at least one (and probably more) function arguments"
191
222
  );
192
- for (var r = e[0](t), n = e.length, a = 1; a < n; a++)
193
- r = e[a](r);
194
- return r;
195
- }
196
- function S(t) {
223
+ for (var t = e[0](r), n = e.length, i = 1; i < n; i++)
224
+ t = e[i](t);
197
225
  return t;
198
226
  }
199
- function M(t) {
200
- return () => t;
227
+ function p(r) {
228
+ return r;
229
+ }
230
+ function M(r) {
231
+ return () => r;
201
232
  }
202
- function F(t, e) {
203
- return typeof t == "number" ? (r) => s.from(r.at(t)) : s.from(t.at(e));
233
+ function j(r, e) {
234
+ return typeof r == "number" ? (t) => s.from(t.at(r)) : s.from(r.at(e));
204
235
  }
205
- function j(t, e) {
206
- return typeof t == "function" ? (r) => s.from(r.find(t)) : s.from(t.find(e));
236
+ function E(r, e) {
237
+ return typeof r == "function" ? (t) => s.from(t.find(r)) : s.from(r.find(e));
207
238
  }
208
- function O(t, e) {
209
- if (typeof t == "function")
210
- return (r) => {
211
- const n = r.findIndex(t);
212
- return n == -1 ? s.None : s.Some(n);
239
+ function O(r, e) {
240
+ if (typeof r == "function")
241
+ return (t) => {
242
+ const n = t.findIndex(r);
243
+ return n == -1 ? s.None() : s.Some(n);
213
244
  };
214
245
  {
215
- const r = t.findIndex(e);
216
- return r == -1 ? s.None : s.Some(r);
246
+ const t = r.findIndex(e);
247
+ return t == -1 ? s.None() : s.Some(t);
217
248
  }
218
249
  }
219
250
  export {
220
251
  s as Maybe,
221
- w as Result,
222
- F as at,
223
- $ as compose,
252
+ c as Result,
253
+ j as at,
254
+ b as compose,
224
255
  M as constant,
225
- j as find,
256
+ E as find,
226
257
  O as findIndex,
227
- S as identity,
258
+ p as identity,
228
259
  J as pipe
229
260
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@terrygonguet/utils",
3
- "version": "0.0.11",
3
+ "version": "0.0.12",
4
4
  "type": "module",
5
5
  "module": "./dist/index.js",
6
6
  "author": {
@@ -36,10 +36,10 @@ export function findIndex<T>(dataOrPredicate: T[] | Predicate<T>, predicate?: Pr
36
36
  if (typeof dataOrPredicate == "function") {
37
37
  return (data: T[]) => {
38
38
  const idx = data.findIndex(dataOrPredicate)
39
- return idx == -1 ? Maybe.None : Maybe.Some(idx)
39
+ return idx == -1 ? Maybe.None<number>() : Maybe.Some(idx)
40
40
  }
41
41
  } else {
42
42
  const idx = dataOrPredicate.findIndex(predicate!)
43
- return idx == -1 ? Maybe.None : Maybe.Some(idx)
43
+ return idx == -1 ? Maybe.None<number>() : Maybe.Some(idx)
44
44
  }
45
45
  }
@@ -1,80 +1,98 @@
1
1
  import { compose, constant, identity } from "./index.ts"
2
2
  import { Result } from "./result.ts"
3
3
 
4
- interface API<T> {
5
- isSome(this: Maybe<T>): this is Some<T>
6
- isNone(this: Maybe<T>): this is None<T>
7
- orDefault(this: Maybe<T>, defaultValue: T): T
8
- map<U>(this: Maybe<T>, f: (value: T) => U): Maybe<U>
9
- flatMap<U>(this: Maybe<T>, f: (value: T) => Maybe<U>): Maybe<U>
10
- toResult(this: Maybe<T>): Result<T, undefined>
11
- toResult<U>(this: Maybe<T>, mapNone: () => U): Result<T, U>
12
- toJSON(this: Maybe<T>): Object
13
- }
14
-
15
- export type Some<T> = { value: T } & API<T>
16
- export type None<T> = API<T>
17
- export type Maybe<T> = Some<T> | None<T>
18
-
19
4
  const $_kind = "@terrygonguet/utils/functional/maybe"
20
5
  const $_variant_Some = "@terrygonguet/utils/functional/maybe/Some"
21
6
  const $_variant_None = "@terrygonguet/utils/functional/maybe/None"
22
7
 
23
- function Some<T>(value: NonNullable<T>): Some<T> {
24
- return Object.create(
25
- {
26
- isSome: () => true,
27
- isNone: () => false,
28
- orDefault(this: Some<T>) {
29
- return this.value
30
- },
31
- map(this: Some<T>, f) {
32
- return Some(f(this.value)!)
33
- },
34
- flatMap(this: Some<T>, f) {
35
- return f(this.value)
36
- },
37
- toResult(this: Some<T>) {
38
- return Result.Success<T, any>(this.value)
39
- },
40
- toJSON(this: Some<T>) {
41
- return { $_kind, $_variant: $_variant_Some, value: this.value }
42
- },
43
- } satisfies API<T>,
44
- {
8
+ export interface Maybe<T> {
9
+ isSome(): this is Some<T>
10
+ isNone(): this is None<T>
11
+ orDefault(defaultValue: T): T
12
+ map<U>(f: (value: T) => U): Maybe<U>
13
+ flatMap<U>(f: (value: T) => Maybe<U>): Maybe<U>
14
+ toResult(): Result<T, undefined>
15
+ toResult<U>(mapNone: () => U): Result<T, U>
16
+ toJSON(): Object
17
+ }
18
+
19
+ class Some<T> implements Maybe<T> {
20
+ value!: T
21
+
22
+ constructor(value: T) {
23
+ Object.defineProperties(this, {
45
24
  $_kind: { value: $_kind, enumerable: false, writable: false },
46
25
  $_variant: { value: $_variant_Some, enumerable: false, writable: false },
47
26
  value: { value, writable: false },
48
- },
49
- )
27
+ })
28
+ }
29
+
30
+ isSome(): true {
31
+ return true
32
+ }
33
+ isNone(): false {
34
+ return false
35
+ }
36
+ orDefault() {
37
+ return this.value
38
+ }
39
+ map<U>(f: (value: T) => U) {
40
+ return new Some(f(this.value))
41
+ }
42
+ flatMap<U>(f: (value: T) => Maybe<U>) {
43
+ return f(this.value)
44
+ }
45
+ toResult<U>(): Result<T, U> {
46
+ return Result.Success(this.value)
47
+ }
48
+ toJSON(): Object {
49
+ return { $_kind, $_variant: $_variant_Some, value: this.value }
50
+ }
50
51
  }
51
52
 
52
- const None: None<any> = Object.create(
53
- {
54
- isSome: () => false,
55
- isNone: () => true,
56
- orDefault: defaultValue => defaultValue,
57
- map: () => None,
58
- flatMap: () => None,
59
- toResult: <U>(mapNone?: () => U) => Result.Failure(mapNone?.()),
60
- toJSON: () => ({ $_kind, $_variant: $_variant_None }),
61
- } satisfies API<any>,
62
- {
63
- $_kind: { value: $_kind, enumerable: false, writable: false },
64
- $_variant: { value: $_variant_None, enumerable: false, writable: false },
65
- },
66
- )
53
+ class None<T> implements Maybe<T> {
54
+ constructor() {}
55
+
56
+ isSome(): false {
57
+ return false
58
+ }
59
+ isNone(): true {
60
+ return true
61
+ }
62
+ orDefault(defaultValue: T) {
63
+ return defaultValue
64
+ }
65
+ map<U>() {
66
+ return this as unknown as Maybe<U>
67
+ }
68
+ flatMap<U>() {
69
+ return this as unknown as Maybe<U>
70
+ }
71
+ toResult(): Result<T, undefined>
72
+ toResult<U>(mapNone: () => U): Result<T, U>
73
+ toResult<U>(mapNone?: () => U) {
74
+ return mapNone ? Result.Failure<T, U>(mapNone?.()) : Result.Failure<T, undefined>(undefined)
75
+ }
76
+ toJSON(): Object {
77
+ return { $_kind, $_variant: $_variant_None }
78
+ }
79
+ }
67
80
 
81
+ const none = new None()
68
82
  export const Maybe = {
69
- Some,
70
- None,
83
+ Some<T>(value: T): Maybe<T> {
84
+ return new Some(value)
85
+ },
86
+ None<T>() {
87
+ return none as Maybe<T>
88
+ },
71
89
  from<T>(value: T | undefined | null): Maybe<T> {
72
90
  switch (value) {
73
91
  case null:
74
92
  case undefined:
75
- return None
93
+ return this.None()
76
94
  default:
77
- return Some(value!)
95
+ return new Some(value!)
78
96
  }
79
97
  },
80
98
  /**
@@ -82,14 +100,16 @@ export const Maybe = {
82
100
  * Use `Result.fromPromise()` if you need error details.
83
101
  */
84
102
  fromPromise<T>(promise: Promise<T>, onResolve: (value: T) => T = identity): Promise<Maybe<T>> {
85
- return promise.then(compose(onResolve, Maybe.from), constant(None))
103
+ return promise.then(compose(onResolve, Maybe.from), constant(this.None()))
86
104
  },
87
105
  JSONReviver(_key: string, value: any) {
88
106
  if (value?.$_kind == $_kind) {
89
107
  const $_variant = value?.$_variant
90
- if ($_variant == $_variant_Some) return Some<unknown>(value?.value)
91
- else if ($_variant == $_variant_None) return None
108
+ if ($_variant == $_variant_Some) return new Some<unknown>(value?.value)
109
+ else if ($_variant == $_variant_None) return Maybe.None()
92
110
  else return value
93
111
  } else return value
94
112
  },
95
113
  }
114
+
115
+ export type { Some, None }
@@ -1,80 +1,86 @@
1
1
  import { compose, identity } from "./index.ts"
2
2
  import { Maybe } from "./maybe.ts"
3
3
 
4
- interface API<S, F> {
5
- isSuccess(this: Result<S, F>): this is Success<S, F>
6
- isFailure(this: Result<S, F>): this is Failure<S, F>
7
- merge<T>(this: Result<S, F>, f: (value: S) => T, g: (reason: F) => T): T
8
- match(this: Result<S, F>, successFn: (value: S) => void, failureFn: (reason: F) => void): void
9
- map<S2>(this: Result<S, F>, f: (value: S) => S2): Result<S2, F>
10
- flatMap<S2, F2>(this: Result<S, F>, f: (value: S) => Result<S2, F | F2>): Result<S2, F | F2>
11
- toJSON(this: Result<S, F>): Object
12
- }
13
-
14
- export type Success<S, F> = { value: S } & API<S, F>
15
- export type Failure<S, F> = { reason: F } & API<S, F>
16
- export type Result<S, F> = Success<S, F> | Failure<S, F>
17
-
18
4
  const $_kind = "@terrygonguet/utils/functional/result"
19
5
  const $_variant_Success = "@terrygonguet/utils/functional/result/Success"
20
6
  const $_variant_Failure = "@terrygonguet/utils/functional/result/Failure"
21
7
 
22
- function Success<S, F>(value: S): Success<S, F> {
23
- return Object.create(
24
- {
25
- isSuccess: () => true,
26
- isFailure: () => false,
27
- merge<T>(this: Success<S, F>, f: (value: S) => T) {
28
- return f(this.value)
29
- },
30
- match(this: Success<S, F>, successFn) {
31
- return successFn(this.value)
32
- },
33
- map<S2>(this: Success<S, F>, f: (value: S) => S2) {
34
- return Success(f(this.value))
35
- },
36
- flatMap<S2, F2>(this: Success<S, F>, f: (value: S) => Result<S2, F2>) {
37
- return f(this.value)
38
- },
39
- toJSON(this: Success<S, F>) {
40
- return { $_kind, $_variant: $_variant_Success, value: this.value }
41
- },
42
- } satisfies API<S, F>,
43
- {
8
+ export interface Result<S, F> {
9
+ isSuccess(): this is Success<S, F>
10
+ isFailure(): this is Failure<S, F>
11
+ merge<T>(whenSuccess: (value: S) => T, whenFailure: (reason: F) => T): T
12
+ match(onSuccess: (value: S) => void, onFailure: (reason: F) => void): void
13
+ map<S2>(f: (value: S) => S2): Result<S2, F>
14
+ flatMap<S2, F2>(f: (value: S) => Result<S2, F | F2>): Result<S2, F | F2>
15
+ toJSON(): Object
16
+ }
17
+
18
+ class Success<S, F> implements Result<S, F> {
19
+ value!: S
20
+
21
+ constructor(value: S) {
22
+ Object.defineProperties(this, {
44
23
  $_kind: { value: $_kind, enumerable: false, writable: false },
45
24
  $_variant: { value: $_variant_Success, enumerable: false, writable: false },
46
25
  value: { value, writable: false },
47
- },
48
- )
26
+ })
27
+ }
28
+
29
+ isSuccess(): true {
30
+ return true
31
+ }
32
+ isFailure(): false {
33
+ return false
34
+ }
35
+ merge<T>(whenSuccess: (value: S) => T) {
36
+ return whenSuccess(this.value)
37
+ }
38
+ match(onSuccess: (value: S) => void) {
39
+ return onSuccess(this.value)
40
+ }
41
+ map<S2>(f: (value: S) => S2) {
42
+ return new Success<S2, F>(f(this.value))
43
+ }
44
+ flatMap<S2, F2>(f: (value: S) => Result<S2, F2>): Result<S2, F | F2> {
45
+ return f(this.value)
46
+ }
47
+ toJSON(this: Success<S, F>) {
48
+ return { $_kind, $_variant: $_variant_Success, value: this.value }
49
+ }
49
50
  }
50
51
 
51
- function Failure<S, F>(reason: F): Failure<S, F> {
52
- return Object.create(
53
- {
54
- isSuccess: () => false,
55
- isFailure: () => true,
56
- merge<T>(this: Failure<S, F>, _: (value: S) => T, g: (reason: F) => T) {
57
- return g(this.reason)
58
- },
59
- match(this: Failure<S, F>, _, failureFn) {
60
- return failureFn(this.reason)
61
- },
62
- map<S2>(this: Failure<S, F>) {
63
- return this as unknown as Result<S2, F>
64
- },
65
- flatMap<S2, F2>(this: Failure<S, F>) {
66
- return this as unknown as Result<S2, F | F2>
67
- },
68
- toJSON(this: Failure<S, F>) {
69
- return { $_kind, $_variant: $_variant_Failure, reason: this.reason }
70
- },
71
- } satisfies API<S, F>,
72
- {
52
+ class Failure<S, F> implements Result<S, F> {
53
+ reason!: F
54
+
55
+ constructor(reason: F) {
56
+ Object.defineProperties(this, {
73
57
  $_kind: { value: $_kind, enumerable: false, writable: false },
74
58
  $_variant: { value: $_variant_Success, enumerable: false, writable: false },
75
59
  reason: { value: reason, writable: false },
76
- },
77
- )
60
+ })
61
+ }
62
+
63
+ isSuccess(): false {
64
+ return false
65
+ }
66
+ isFailure(): true {
67
+ return true
68
+ }
69
+ merge<T>(_: (value: S) => T, whenFailure: (reason: F) => T) {
70
+ return whenFailure(this.reason)
71
+ }
72
+ match(_: (value: S) => void, onFailure: (reason: F) => void) {
73
+ return onFailure(this.reason)
74
+ }
75
+ map<S2>() {
76
+ return this as unknown as Result<S2, F>
77
+ }
78
+ flatMap<S2, F2>() {
79
+ return this as unknown as Result<S2, F | F2>
80
+ }
81
+ toJSON() {
82
+ return { $_kind, $_variant: $_variant_Failure, reason: this.reason }
83
+ }
78
84
  }
79
85
 
80
86
  function resultFromMaybe<S>(maybe: Maybe<S>): Result<S, undefined>
@@ -84,8 +90,12 @@ function resultFromMaybe<S, F>(maybe: Maybe<S>, mapNone?: () => F) {
84
90
  }
85
91
 
86
92
  export const Result = {
87
- Success,
88
- Failure,
93
+ Success<S, F>(value: S): Result<S, F> {
94
+ return new Success<S, F>(value)
95
+ },
96
+ Failure<S, F>(reason: F): Result<S, F> {
97
+ return new Failure<S, F>(reason)
98
+ },
89
99
  try<S, F>(tryFn: () => S) {
90
100
  return new TryCatch<S, F>(tryFn)
91
101
  },
@@ -94,14 +104,18 @@ export const Result = {
94
104
  onResolve: (value: S) => S,
95
105
  onReject: (reason: unknown) => F,
96
106
  ): Promise<Result<S, F>> {
97
- return promise.then(compose(onResolve, Success<S, F>), compose(onReject, Failure<S, F>))
107
+ return promise.then(
108
+ compose(onResolve, this.Success<S, F>),
109
+ compose(onReject, this.Failure<S, F>),
110
+ )
98
111
  },
99
112
  fromMaybe: resultFromMaybe,
100
113
  JSONReviver(_key: string, value: any) {
101
114
  if (value?.$_kind == $_kind) {
102
115
  const $_variant = value?.$_variant
103
- if ($_variant == $_variant_Success) return Success<unknown, unknown>(value?.value)
104
- else if ($_variant == $_variant_Failure) return Failure<unknown, unknown>(value?.reason)
116
+ if ($_variant == $_variant_Success) return new Success<unknown, unknown>(value?.value)
117
+ else if ($_variant == $_variant_Failure)
118
+ return new Failure<unknown, unknown>(value?.reason)
105
119
  else return value
106
120
  } else return value
107
121
  },
@@ -123,13 +137,15 @@ class TryCatch<S, F> {
123
137
 
124
138
  exec(finallyFn?: (result: Result<S, F>) => void): Result<S, F> {
125
139
  try {
126
- const result = Success<S, F>(this.tryFn())
140
+ const result = Result.Success<S, F>(this.tryFn())
127
141
  finallyFn?.(result)
128
142
  return result
129
143
  } catch (error) {
130
- const result = Failure<S, F>(this.catchFn(error))
144
+ const result = Result.Failure<S, F>(this.catchFn(error))
131
145
  finallyFn?.(result)
132
146
  return result
133
147
  }
134
148
  }
135
149
  }
150
+
151
+ export type { Success, Failure }
@@ -1,29 +1,45 @@
1
1
  import { Result } from "./result.ts";
2
- interface API<T> {
3
- isSome(this: Maybe<T>): this is Some<T>;
4
- isNone(this: Maybe<T>): this is None<T>;
5
- orDefault(this: Maybe<T>, defaultValue: T): T;
6
- map<U>(this: Maybe<T>, f: (value: T) => U): Maybe<U>;
7
- flatMap<U>(this: Maybe<T>, f: (value: T) => Maybe<U>): Maybe<U>;
8
- toResult(this: Maybe<T>): Result<T, undefined>;
9
- toResult<U>(this: Maybe<T>, mapNone: () => U): Result<T, U>;
10
- toJSON(this: Maybe<T>): Object;
2
+ export interface Maybe<T> {
3
+ isSome(): this is Some<T>;
4
+ isNone(): this is None<T>;
5
+ orDefault(defaultValue: T): T;
6
+ map<U>(f: (value: T) => U): Maybe<U>;
7
+ flatMap<U>(f: (value: T) => Maybe<U>): Maybe<U>;
8
+ toResult(): Result<T, undefined>;
9
+ toResult<U>(mapNone: () => U): Result<T, U>;
10
+ toJSON(): Object;
11
11
  }
12
- export type Some<T> = {
12
+ declare class Some<T> implements Maybe<T> {
13
13
  value: T;
14
- } & API<T>;
15
- export type None<T> = API<T>;
16
- export type Maybe<T> = Some<T> | None<T>;
17
- declare function Some<T>(value: NonNullable<T>): Some<T>;
14
+ constructor(value: T);
15
+ isSome(): true;
16
+ isNone(): false;
17
+ orDefault(): T;
18
+ map<U>(f: (value: T) => U): Some<U>;
19
+ flatMap<U>(f: (value: T) => Maybe<U>): Maybe<U>;
20
+ toResult<U>(): Result<T, U>;
21
+ toJSON(): Object;
22
+ }
23
+ declare class None<T> implements Maybe<T> {
24
+ constructor();
25
+ isSome(): false;
26
+ isNone(): true;
27
+ orDefault(defaultValue: T): T;
28
+ map<U>(): Maybe<U>;
29
+ flatMap<U>(): Maybe<U>;
30
+ toResult(): Result<T, undefined>;
31
+ toResult<U>(mapNone: () => U): Result<T, U>;
32
+ toJSON(): Object;
33
+ }
18
34
  export declare const Maybe: {
19
- Some: typeof Some;
20
- None: None<any>;
21
- from<T>(value: T | null | undefined): Maybe<T>;
35
+ Some<T>(value: T): Maybe<T>;
36
+ None<T_1>(): Maybe<T_1>;
37
+ from<T_2>(value: T_2 | null | undefined): Maybe<T_2>;
22
38
  /**
23
39
  * CAUTION: this method swallows errors and simply returns None!
24
40
  * Use `Result.fromPromise()` if you need error details.
25
41
  */
26
- fromPromise<T_1>(promise: Promise<T_1>, onResolve?: (value: T_1) => T_1): Promise<Maybe<T_1>>;
42
+ fromPromise<T_3>(promise: Promise<T_3>, onResolve?: (value: T_3) => T_3): Promise<Maybe<T_3>>;
27
43
  JSONReviver(_key: string, value: any): any;
28
44
  };
29
- export {};
45
+ export type { Some, None };
@@ -1,29 +1,50 @@
1
1
  import { Maybe } from "./maybe.ts";
2
- interface API<S, F> {
3
- isSuccess(this: Result<S, F>): this is Success<S, F>;
4
- isFailure(this: Result<S, F>): this is Failure<S, F>;
5
- merge<T>(this: Result<S, F>, f: (value: S) => T, g: (reason: F) => T): T;
6
- match(this: Result<S, F>, successFn: (value: S) => void, failureFn: (reason: F) => void): void;
7
- map<S2>(this: Result<S, F>, f: (value: S) => S2): Result<S2, F>;
8
- flatMap<S2, F2>(this: Result<S, F>, f: (value: S) => Result<S2, F | F2>): Result<S2, F | F2>;
9
- toJSON(this: Result<S, F>): Object;
2
+ export interface Result<S, F> {
3
+ isSuccess(): this is Success<S, F>;
4
+ isFailure(): this is Failure<S, F>;
5
+ merge<T>(whenSuccess: (value: S) => T, whenFailure: (reason: F) => T): T;
6
+ match(onSuccess: (value: S) => void, onFailure: (reason: F) => void): void;
7
+ map<S2>(f: (value: S) => S2): Result<S2, F>;
8
+ flatMap<S2, F2>(f: (value: S) => Result<S2, F | F2>): Result<S2, F | F2>;
9
+ toJSON(): Object;
10
10
  }
11
- export type Success<S, F> = {
11
+ declare class Success<S, F> implements Result<S, F> {
12
12
  value: S;
13
- } & API<S, F>;
14
- export type Failure<S, F> = {
13
+ constructor(value: S);
14
+ isSuccess(): true;
15
+ isFailure(): false;
16
+ merge<T>(whenSuccess: (value: S) => T): T;
17
+ match(onSuccess: (value: S) => void): void;
18
+ map<S2>(f: (value: S) => S2): Success<S2, F>;
19
+ flatMap<S2, F2>(f: (value: S) => Result<S2, F2>): Result<S2, F | F2>;
20
+ toJSON(this: Success<S, F>): {
21
+ $_kind: string;
22
+ $_variant: string;
23
+ value: S;
24
+ };
25
+ }
26
+ declare class Failure<S, F> implements Result<S, F> {
15
27
  reason: F;
16
- } & API<S, F>;
17
- export type Result<S, F> = Success<S, F> | Failure<S, F>;
18
- declare function Success<S, F>(value: S): Success<S, F>;
19
- declare function Failure<S, F>(reason: F): Failure<S, F>;
28
+ constructor(reason: F);
29
+ isSuccess(): false;
30
+ isFailure(): true;
31
+ merge<T>(_: (value: S) => T, whenFailure: (reason: F) => T): T;
32
+ match(_: (value: S) => void, onFailure: (reason: F) => void): void;
33
+ map<S2>(): Result<S2, F>;
34
+ flatMap<S2, F2>(): Result<S2, F | F2>;
35
+ toJSON(): {
36
+ $_kind: string;
37
+ $_variant: string;
38
+ reason: F;
39
+ };
40
+ }
20
41
  declare function resultFromMaybe<S>(maybe: Maybe<S>): Result<S, undefined>;
21
42
  declare function resultFromMaybe<S, F>(maybe: Maybe<S>, mapNone: () => F): Result<S, F>;
22
43
  export declare const Result: {
23
- Success: typeof Success;
24
- Failure: typeof Failure;
25
- try<S, F>(tryFn: () => S): TryCatch<S, F>;
26
- fromPromise<S_1, F_1>(promise: Promise<S_1>, onResolve: (value: S_1) => S_1, onReject: (reason: unknown) => F_1): Promise<Result<S_1, F_1>>;
44
+ Success<S, F>(value: S): Result<S, F>;
45
+ Failure<S_1, F_1>(reason: F_1): Result<S_1, F_1>;
46
+ try<S_2, F_2>(tryFn: () => S_2): TryCatch<S_2, F_2>;
47
+ fromPromise<S_3, F_3>(promise: Promise<S_3>, onResolve: (value: S_3) => S_3, onReject: (reason: unknown) => F_3): Promise<Result<S_3, F_3>>;
27
48
  fromMaybe: typeof resultFromMaybe;
28
49
  JSONReviver(_key: string, value: any): any;
29
50
  };
@@ -34,4 +55,4 @@ declare class TryCatch<S, F> {
34
55
  catch(catchFn: (err: unknown) => F): this;
35
56
  exec(finallyFn?: (result: Result<S, F>) => void): Result<S, F>;
36
57
  }
37
- export {};
58
+ export type { Success, Failure };