@terrygonguet/utils 0.0.4 → 0.0.6

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,8 +1,8 @@
1
- var g = Object.defineProperty;
2
- var p = (t, e, r) => e in t ? g(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r;
3
- var f = (t, e, r) => (p(t, typeof e != "symbol" ? e + "" : e, r), r);
4
- const a = "@terrygonguet/utils/functional/maybe", _ = "@terrygonguet/utils/functional/maybe/Some", b = "@terrygonguet/utils/functional/maybe/None";
5
- function i(t) {
1
+ var p = Object.defineProperty;
2
+ var S = (t, e, r) => e in t ? p(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r;
3
+ var f = (t, e, r) => (S(t, typeof e != "symbol" ? e + "" : e, r), r);
4
+ const s = "@terrygonguet/utils/functional/maybe", _ = "@terrygonguet/utils/functional/maybe/Some", h = "@terrygonguet/utils/functional/maybe/None";
5
+ function u(t) {
6
6
  return Object.create(
7
7
  {
8
8
  isSome: () => !0,
@@ -11,82 +11,88 @@ function i(t) {
11
11
  return this.value;
12
12
  },
13
13
  map(e) {
14
- return i(e(this.value));
14
+ return u(e(this.value));
15
15
  },
16
16
  flatMap(e) {
17
17
  return e(this.value);
18
18
  },
19
19
  toJSON() {
20
- return { $_kind: a, $_variant: _, value: this.value };
20
+ return { $_kind: s, $_variant: _, value: this.value };
21
21
  }
22
22
  },
23
23
  {
24
- $_kind: { value: a, enumerable: !1, writable: !1 },
24
+ $_kind: { value: s, enumerable: !1, writable: !1 },
25
25
  $_variant: { value: _, enumerable: !1, writable: !1 },
26
26
  value: { value: t, writable: !1 }
27
27
  }
28
28
  );
29
29
  }
30
- const n = Object.create(
30
+ const a = Object.create(
31
31
  {
32
32
  isSome: () => !1,
33
33
  isNone: () => !0,
34
34
  orDefault: $,
35
- map: () => n,
36
- flatMap: () => n,
37
- toJSON: () => ({ $_kind: a, $_variant: b })
35
+ map: () => a,
36
+ flatMap: () => a,
37
+ toJSON: () => ({ $_kind: s, $_variant: h })
38
38
  },
39
39
  {
40
- $_kind: { value: a, enumerable: !1, writable: !1 },
41
- $_variant: { value: b, enumerable: !1, writable: !1 }
40
+ $_kind: { value: s, enumerable: !1, writable: !1 },
41
+ $_variant: { value: h, enumerable: !1, writable: !1 }
42
42
  }
43
- ), k = {
44
- Some: i,
45
- None: n,
43
+ ), y = {
44
+ Some: u,
45
+ None: a,
46
46
  from(t) {
47
47
  switch (t) {
48
48
  case null:
49
49
  case void 0:
50
- return n;
50
+ return a;
51
51
  default:
52
- return i(t);
52
+ return u(t);
53
53
  }
54
54
  },
55
55
  JSONReviver(t, e) {
56
- if ((e == null ? void 0 : e.$_kind) == a) {
56
+ if ((e == null ? void 0 : e.$_kind) == s) {
57
57
  const r = e == null ? void 0 : e.$_variant;
58
- return r == _ ? i(e == null ? void 0 : e.value) : r == b ? n : e;
58
+ return r == _ ? u(e == null ? void 0 : e.value) : r == h ? a : e;
59
59
  } else
60
60
  return e;
61
61
  }
62
- }, s = "@terrygonguet/utils/functional/result", u = "@terrygonguet/utils/functional/result/Success", m = "@terrygonguet/utils/functional/result/Failure";
63
- function l(t) {
62
+ }, i = "@terrygonguet/utils/functional/result", o = "@terrygonguet/utils/functional/result/Success", b = "@terrygonguet/utils/functional/result/Failure";
63
+ function c(t) {
64
64
  return Object.create(
65
65
  {
66
66
  isSuccess: () => !0,
67
67
  isFailure: () => !1,
68
+ merge(e) {
69
+ return e(this.value);
70
+ },
68
71
  map(e) {
69
- return l(e(this.value));
72
+ return c(e(this.value));
70
73
  },
71
74
  flatMap(e) {
72
75
  return e(this.value);
73
76
  },
74
77
  toJSON() {
75
- return { $_kind: s, $_variant: u, value: this.value };
78
+ return { $_kind: i, $_variant: o, value: this.value };
76
79
  }
77
80
  },
78
81
  {
79
- $_kind: { value: s, enumerable: !1, writable: !1 },
80
- $_variant: { value: u, enumerable: !1, writable: !1 },
82
+ $_kind: { value: i, enumerable: !1, writable: !1 },
83
+ $_variant: { value: o, enumerable: !1, writable: !1 },
81
84
  value: { value: t, writable: !1 }
82
85
  }
83
86
  );
84
87
  }
85
- function h(t) {
88
+ function m(t) {
86
89
  return Object.create(
87
90
  {
88
91
  isSuccess: () => !1,
89
92
  isFailure: () => !0,
93
+ merge(e, r) {
94
+ return r(this.reason);
95
+ },
90
96
  map() {
91
97
  return this;
92
98
  },
@@ -94,31 +100,31 @@ function h(t) {
94
100
  return this;
95
101
  },
96
102
  toJSON() {
97
- return { $_kind: s, $_variant: m, reason: this.reason };
103
+ return { $_kind: i, $_variant: b, reason: this.reason };
98
104
  }
99
105
  },
100
106
  {
101
- $_kind: { value: s, enumerable: !1, writable: !1 },
102
- $_variant: { value: u, enumerable: !1, writable: !1 },
107
+ $_kind: { value: i, enumerable: !1, writable: !1 },
108
+ $_variant: { value: o, enumerable: !1, writable: !1 },
103
109
  reason: { value: t, writable: !1 }
104
110
  }
105
111
  );
106
112
  }
107
- const F = {
108
- Success: l,
109
- Failure: h,
113
+ const v = {
114
+ Success: c,
115
+ Failure: m,
110
116
  try(t) {
111
- return new S(t);
117
+ return new d(t);
112
118
  },
113
119
  JSONReviver(t, e) {
114
- if ((e == null ? void 0 : e.$_kind) == s) {
120
+ if ((e == null ? void 0 : e.$_kind) == i) {
115
121
  const r = e == null ? void 0 : e.$_variant;
116
- return r == u ? l(e == null ? void 0 : e.value) : r == m ? h(e == null ? void 0 : e.reason) : e;
122
+ return r == o ? c(e == null ? void 0 : e.value) : r == b ? m(e == null ? void 0 : e.reason) : e;
117
123
  } else
118
124
  return e;
119
125
  }
120
126
  };
121
- class S {
127
+ class d {
122
128
  constructor(e) {
123
129
  f(this, "tryFn");
124
130
  f(this, "catchFn");
@@ -127,38 +133,35 @@ class S {
127
133
  catch(e) {
128
134
  return this.catchFn = e, this;
129
135
  }
130
- exec() {
136
+ exec(e) {
131
137
  try {
132
- return l(this.tryFn());
133
- } catch (e) {
134
- return h(this.catchFn(e));
138
+ const r = c(this.tryFn());
139
+ return e == null || e(r), r;
140
+ } catch (r) {
141
+ const n = m(this.catchFn(r));
142
+ return e == null || e(n), n;
135
143
  }
136
144
  }
137
145
  }
138
- var N = d;
139
- function d(t, e) {
146
+ var N = w;
147
+ function w(t, e) {
140
148
  if (!arguments.length)
141
149
  throw new Error(
142
150
  "expected at least one (and probably more) function arguments"
143
151
  );
144
152
  var r = arguments;
145
153
  return function() {
146
- for (var o = r[0].apply(this, arguments), y = r.length, c = 1; c < y; c++)
147
- o = r[c].call(this, o);
148
- return o;
154
+ for (var n = r[0].apply(this, arguments), g = r.length, l = 1; l < g; l++)
155
+ n = r[l].call(this, n);
156
+ return n;
149
157
  };
150
158
  }
151
159
  function $(t) {
152
160
  return t;
153
161
  }
154
162
  export {
155
- h as Failure,
156
- k as Maybe,
157
- n as None,
158
- F as Result,
159
- i as Some,
160
- l as Success,
161
- S as TryCatch,
163
+ y as Maybe,
164
+ v as Result,
162
165
  N as compose,
163
166
  $ as identity
164
167
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@terrygonguet/utils",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "dev": "vite",
@@ -9,6 +9,7 @@
9
9
  "coverage": "vitest run --coverage"
10
10
  },
11
11
  "devDependencies": {
12
+ "@vitest/coverage-v8": "^0.32.0",
12
13
  "@vitest/ui": "^0.32.0",
13
14
  "prettier": "^2.8.8",
14
15
  "typescript": "^5.0.2",
@@ -17,7 +17,7 @@ const $_kind = "@terrygonguet/utils/functional/maybe"
17
17
  const $_variant_Some = "@terrygonguet/utils/functional/maybe/Some"
18
18
  const $_variant_None = "@terrygonguet/utils/functional/maybe/None"
19
19
 
20
- export function Some<T>(value: NonNullable<T>): Some<T> {
20
+ function Some<T>(value: NonNullable<T>): Some<T> {
21
21
  return Object.create(
22
22
  {
23
23
  isSome: () => true,
@@ -43,7 +43,7 @@ export function Some<T>(value: NonNullable<T>): Some<T> {
43
43
  )
44
44
  }
45
45
 
46
- export const None: None<any> = Object.create(
46
+ const None: None<any> = Object.create(
47
47
  {
48
48
  isSome: () => false,
49
49
  isNone: () => true,
@@ -3,6 +3,7 @@ import { identity } from "./index.ts"
3
3
  interface API<S, F> {
4
4
  isSuccess(this: Result<S, F>): this is Success<S, F>
5
5
  isFailure(this: Result<S, F>): this is Failure<S, F>
6
+ merge<T>(this: Result<S, F>, f: (value: S) => T, g: (reason: F) => T): T
6
7
  map<S2>(this: Result<S, F>, f: (value: S) => S2): Result<S2, F>
7
8
  flatMap<S2, F2>(this: Result<S, F>, f: (value: S) => Result<S2, F | F2>): Result<S2, F | F2>
8
9
  toJSON(this: Result<S, F>): Object
@@ -16,11 +17,14 @@ const $_kind = "@terrygonguet/utils/functional/result"
16
17
  const $_variant_Success = "@terrygonguet/utils/functional/result/Success"
17
18
  const $_variant_Failure = "@terrygonguet/utils/functional/result/Failure"
18
19
 
19
- export function Success<S, F>(value: S): Success<S, F> {
20
+ function Success<S, F>(value: S): Success<S, F> {
20
21
  return Object.create(
21
22
  {
22
23
  isSuccess: () => true,
23
24
  isFailure: () => false,
25
+ merge<T>(this: Success<S, F>, f: (value: S) => T) {
26
+ return f(this.value)
27
+ },
24
28
  map<S2>(this: Success<S, F>, f: (value: S) => S2) {
25
29
  return Success(f(this.value))
26
30
  },
@@ -39,11 +43,14 @@ export function Success<S, F>(value: S): Success<S, F> {
39
43
  )
40
44
  }
41
45
 
42
- export function Failure<S, F>(reason: F): Failure<S, F> {
46
+ function Failure<S, F>(reason: F): Failure<S, F> {
43
47
  return Object.create(
44
48
  {
45
49
  isSuccess: () => false,
46
50
  isFailure: () => true,
51
+ merge<T>(this: Failure<S, F>, _: (value: S) => T, g: (reason: F) => T) {
52
+ return g(this.reason)
53
+ },
47
54
  map<S2>(this: Failure<S, F>) {
48
55
  return this as unknown as Result<S2, F>
49
56
  },
@@ -78,7 +85,7 @@ export const Result = {
78
85
  },
79
86
  }
80
87
 
81
- export class TryCatch<S, F> {
88
+ class TryCatch<S, F> {
82
89
  tryFn: () => S
83
90
  catchFn: (err: unknown) => F
84
91
 
@@ -92,11 +99,15 @@ export class TryCatch<S, F> {
92
99
  return this
93
100
  }
94
101
 
95
- exec(): Result<S, F> {
102
+ exec(finallyFn?: (result: Result<S, F>) => void): Result<S, F> {
96
103
  try {
97
- return Success<S, F>(this.tryFn())
104
+ const result = Success<S, F>(this.tryFn())
105
+ finallyFn?.(result)
106
+ return result
98
107
  } catch (error) {
99
- return Failure<S, F>(this.catchFn(error))
108
+ const result = Failure<S, F>(this.catchFn(error))
109
+ finallyFn?.(result)
110
+ return result
100
111
  }
101
112
  }
102
113
  }
@@ -11,8 +11,7 @@ export type Some<T> = {
11
11
  } & API<T>;
12
12
  export type None<T> = API<T>;
13
13
  export type Maybe<T> = Some<T> | None<T>;
14
- export declare function Some<T>(value: NonNullable<T>): Some<T>;
15
- export declare const None: None<any>;
14
+ declare function Some<T>(value: NonNullable<T>): Some<T>;
16
15
  export declare const Maybe: {
17
16
  Some: typeof Some;
18
17
  None: None<any>;
@@ -1,6 +1,7 @@
1
1
  interface API<S, F> {
2
2
  isSuccess(this: Result<S, F>): this is Success<S, F>;
3
3
  isFailure(this: Result<S, F>): this is Failure<S, F>;
4
+ merge<T>(this: Result<S, F>, f: (value: S) => T, g: (reason: F) => T): T;
4
5
  map<S2>(this: Result<S, F>, f: (value: S) => S2): Result<S2, F>;
5
6
  flatMap<S2, F2>(this: Result<S, F>, f: (value: S) => Result<S2, F | F2>): Result<S2, F | F2>;
6
7
  toJSON(this: Result<S, F>): Object;
@@ -12,19 +13,19 @@ export type Failure<S, F> = {
12
13
  reason: F;
13
14
  } & API<S, F>;
14
15
  export type Result<S, F> = Success<S, F> | Failure<S, F>;
15
- export declare function Success<S, F>(value: S): Success<S, F>;
16
- export declare function Failure<S, F>(reason: F): Failure<S, F>;
16
+ declare function Success<S, F>(value: S): Success<S, F>;
17
+ declare function Failure<S, F>(reason: F): Failure<S, F>;
17
18
  export declare const Result: {
18
19
  Success: typeof Success;
19
20
  Failure: typeof Failure;
20
21
  try<S, F>(tryFn: () => S): TryCatch<S, F>;
21
22
  JSONReviver(_key: string, value: any): any;
22
23
  };
23
- export declare class TryCatch<S, F> {
24
+ declare class TryCatch<S, F> {
24
25
  tryFn: () => S;
25
26
  catchFn: (err: unknown) => F;
26
27
  constructor(tryFn: () => S);
27
28
  catch(catchFn: (err: unknown) => F): this;
28
- exec(): Result<S, F>;
29
+ exec(finallyFn?: (result: Result<S, F>) => void): Result<S, F>;
29
30
  }
30
31
  export {};