@terrygonguet/utils 0.0.3 → 0.0.5

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/async.js ADDED
@@ -0,0 +1,6 @@
1
+ function i(e) {
2
+ return new Promise((t) => setTimeout(t, e));
3
+ }
4
+ export {
5
+ i as wait
6
+ };
@@ -0,0 +1,165 @@
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", h = "@terrygonguet/utils/functional/maybe/None";
5
+ function i(t) {
6
+ return Object.create(
7
+ {
8
+ isSome: () => !0,
9
+ isNone: () => !1,
10
+ orDefault() {
11
+ return this.value;
12
+ },
13
+ map(e) {
14
+ return i(e(this.value));
15
+ },
16
+ flatMap(e) {
17
+ return e(this.value);
18
+ },
19
+ toJSON() {
20
+ return { $_kind: a, $_variant: _, value: this.value };
21
+ }
22
+ },
23
+ {
24
+ $_kind: { value: a, enumerable: !1, writable: !1 },
25
+ $_variant: { value: _, enumerable: !1, writable: !1 },
26
+ value: { value: t, writable: !1 }
27
+ }
28
+ );
29
+ }
30
+ const n = Object.create(
31
+ {
32
+ isSome: () => !1,
33
+ isNone: () => !0,
34
+ orDefault: $,
35
+ map: () => n,
36
+ flatMap: () => n,
37
+ toJSON: () => ({ $_kind: a, $_variant: h })
38
+ },
39
+ {
40
+ $_kind: { value: a, enumerable: !1, writable: !1 },
41
+ $_variant: { value: h, enumerable: !1, writable: !1 }
42
+ }
43
+ ), k = {
44
+ Some: i,
45
+ None: n,
46
+ from(t) {
47
+ switch (t) {
48
+ case null:
49
+ case void 0:
50
+ return n;
51
+ default:
52
+ return i(t);
53
+ }
54
+ },
55
+ JSONReviver(t, e) {
56
+ if ((e == null ? void 0 : e.$_kind) == a) {
57
+ const r = e == null ? void 0 : e.$_variant;
58
+ return r == _ ? i(e == null ? void 0 : e.value) : r == h ? n : e;
59
+ } else
60
+ return e;
61
+ }
62
+ }, s = "@terrygonguet/utils/functional/result", u = "@terrygonguet/utils/functional/result/Success", b = "@terrygonguet/utils/functional/result/Failure";
63
+ function l(t) {
64
+ return Object.create(
65
+ {
66
+ isSuccess: () => !0,
67
+ isFailure: () => !1,
68
+ merge(e) {
69
+ return e(this.value);
70
+ },
71
+ map(e) {
72
+ return l(e(this.value));
73
+ },
74
+ flatMap(e) {
75
+ return e(this.value);
76
+ },
77
+ toJSON() {
78
+ return { $_kind: s, $_variant: u, value: this.value };
79
+ }
80
+ },
81
+ {
82
+ $_kind: { value: s, enumerable: !1, writable: !1 },
83
+ $_variant: { value: u, enumerable: !1, writable: !1 },
84
+ value: { value: t, writable: !1 }
85
+ }
86
+ );
87
+ }
88
+ function m(t) {
89
+ return Object.create(
90
+ {
91
+ isSuccess: () => !1,
92
+ isFailure: () => !0,
93
+ merge(e, r) {
94
+ return r(this.reason);
95
+ },
96
+ map() {
97
+ return this;
98
+ },
99
+ flatMap() {
100
+ return this;
101
+ },
102
+ toJSON() {
103
+ return { $_kind: s, $_variant: b, reason: this.reason };
104
+ }
105
+ },
106
+ {
107
+ $_kind: { value: s, enumerable: !1, writable: !1 },
108
+ $_variant: { value: u, enumerable: !1, writable: !1 },
109
+ reason: { value: t, writable: !1 }
110
+ }
111
+ );
112
+ }
113
+ const F = {
114
+ Success: l,
115
+ Failure: m,
116
+ try(t) {
117
+ return new S(t);
118
+ },
119
+ JSONReviver(t, e) {
120
+ if ((e == null ? void 0 : e.$_kind) == s) {
121
+ const r = e == null ? void 0 : e.$_variant;
122
+ return r == u ? l(e == null ? void 0 : e.value) : r == b ? m(e == null ? void 0 : e.reason) : e;
123
+ } else
124
+ return e;
125
+ }
126
+ };
127
+ class S {
128
+ constructor(e) {
129
+ f(this, "tryFn");
130
+ f(this, "catchFn");
131
+ this.tryFn = e, this.catchFn = $;
132
+ }
133
+ catch(e) {
134
+ return this.catchFn = e, this;
135
+ }
136
+ exec() {
137
+ try {
138
+ return l(this.tryFn());
139
+ } catch (e) {
140
+ return m(this.catchFn(e));
141
+ }
142
+ }
143
+ }
144
+ var v = d;
145
+ function d(t, e) {
146
+ if (!arguments.length)
147
+ throw new Error(
148
+ "expected at least one (and probably more) function arguments"
149
+ );
150
+ var r = arguments;
151
+ return function() {
152
+ for (var o = r[0].apply(this, arguments), y = r.length, c = 1; c < y; c++)
153
+ o = r[c].call(this, o);
154
+ return o;
155
+ };
156
+ }
157
+ function $(t) {
158
+ return t;
159
+ }
160
+ export {
161
+ k as Maybe,
162
+ F as Result,
163
+ v as compose,
164
+ $ as identity
165
+ };
package/dist/main.js ADDED
@@ -0,0 +1,22 @@
1
+ function o(n, t, r) {
2
+ return Math.min(Math.max(n, t), r);
3
+ }
4
+ function c(n, t, r) {
5
+ try {
6
+ return JSON.parse(n, r);
7
+ } catch {
8
+ return t;
9
+ }
10
+ }
11
+ function f(...n) {
12
+ return function(t, r) {
13
+ for (const e of n)
14
+ r = e(t, r);
15
+ return r;
16
+ };
17
+ }
18
+ export {
19
+ o as clamp,
20
+ f as composeJSONRevivers,
21
+ c as safeParse
22
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@terrygonguet/utils",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "dev": "vite",
@@ -9,12 +9,18 @@
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",
15
16
  "vite": "^4.3.9",
16
17
  "vitest": "^0.32.0"
17
18
  },
19
+ "files": [
20
+ "dist/**/*.js",
21
+ "src/**/!(*.test).ts",
22
+ "types/**/!(*.test).d.ts"
23
+ ],
18
24
  "exports": {
19
25
  ".": {
20
26
  "types": "./types/main.js",
@@ -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
 
@@ -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,15 +13,15 @@ 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);
package/index.html DELETED
@@ -1,13 +0,0 @@
1
- <!DOCTYPE html>
2
- <html lang="en">
3
- <head>
4
- <meta charset="UTF-8" />
5
- <link rel="icon" type="image/svg+xml" href="/vite.svg" />
6
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7
- <title>Vite + TS</title>
8
- </head>
9
- <body>
10
- <div id="app"></div>
11
- <script type="module" src="/src/main.ts"></script>
12
- </body>
13
- </html>
package/tsconfig.json DELETED
@@ -1,25 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2020",
4
- "useDefineForClassFields": true,
5
- "module": "ESNext",
6
- "lib": ["ES2020", "DOM", "DOM.Iterable"],
7
- "skipLibCheck": true,
8
-
9
- /* Bundler mode */
10
- "moduleResolution": "node16",
11
- "allowImportingTsExtensions": true,
12
- "allowSyntheticDefaultImports": true,
13
- "resolveJsonModule": true,
14
- "isolatedModules": true,
15
- "declaration": true,
16
- "emitDeclarationOnly": true,
17
- "outDir": "./types",
18
-
19
- /* Linting */
20
- "strict": true,
21
- "noUnusedLocals": true,
22
- "noUnusedParameters": true
23
- },
24
- "include": ["src"]
25
- }
package/vite.config.js DELETED
@@ -1,15 +0,0 @@
1
- import { resolve } from "path"
2
- import { defineConfig } from "vite"
3
-
4
- export default defineConfig({
5
- build: {
6
- lib: {
7
- formats: ["es"],
8
- entry: {
9
- main: resolve(__dirname, "src/main.ts"),
10
- async: resolve(__dirname, "src/async.ts"),
11
- functional: resolve(__dirname, "src/functional/index.ts"),
12
- },
13
- },
14
- },
15
- })