@e280/stz 0.2.26 → 0.2.28

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 (49) hide show
  1. package/README.md +40 -0
  2. package/package.json +2 -2
  3. package/s/index.ts +2 -1
  4. package/s/ok/fns/err.ts +8 -0
  5. package/s/ok/fns/grab.ts +10 -0
  6. package/s/ok/fns/need.ts +20 -0
  7. package/s/ok/fns/ok.ts +8 -0
  8. package/s/ok/index.ts +10 -0
  9. package/s/ok/test.ts +47 -0
  10. package/s/ok/types/err.ts +4 -0
  11. package/s/ok/types/ok.ts +4 -0
  12. package/s/ok/types/result.ts +10 -0
  13. package/s/tests.test.ts +2 -1
  14. package/s/thrown.ts +7 -0
  15. package/x/index.d.ts +2 -0
  16. package/x/index.js +2 -0
  17. package/x/index.js.map +1 -1
  18. package/x/ok/fns/err.d.ts +3 -0
  19. package/x/ok/fns/err.js +5 -0
  20. package/x/ok/fns/err.js.map +1 -0
  21. package/x/ok/fns/grab.d.ts +3 -0
  22. package/x/ok/fns/grab.js +7 -0
  23. package/x/ok/fns/grab.js.map +1 -0
  24. package/x/ok/fns/need.d.ts +3 -0
  25. package/x/ok/fns/need.js +13 -0
  26. package/x/ok/fns/need.js.map +1 -0
  27. package/x/ok/fns/ok.d.ts +3 -0
  28. package/x/ok/fns/ok.js +5 -0
  29. package/x/ok/fns/ok.js.map +1 -0
  30. package/x/ok/index.d.ts +7 -0
  31. package/x/ok/index.js +8 -0
  32. package/x/ok/index.js.map +1 -0
  33. package/x/ok/test.d.ts +12 -0
  34. package/x/ok/test.js +39 -0
  35. package/x/ok/test.js.map +1 -0
  36. package/x/ok/types/err.d.ts +5 -0
  37. package/x/ok/types/err.js +2 -0
  38. package/x/ok/types/err.js.map +1 -0
  39. package/x/ok/types/ok.d.ts +5 -0
  40. package/x/ok/types/ok.js +2 -0
  41. package/x/ok/types/ok.js.map +1 -0
  42. package/x/ok/types/result.d.ts +4 -0
  43. package/x/ok/types/result.js +2 -0
  44. package/x/ok/types/result.js.map +1 -0
  45. package/x/tests.test.js +2 -0
  46. package/x/tests.test.js.map +1 -1
  47. package/x/thrown.d.ts +1 -0
  48. package/x/thrown.js +10 -0
  49. package/x/thrown.js.map +1 -0
package/README.md CHANGED
@@ -315,6 +315,46 @@ const stop = cycle(async() => {
315
315
  stop()
316
316
  ```
317
317
 
318
+ ### 🍏 ok
319
+ > tiny result toolkit for saying "this worked" or "this did not work"
320
+
321
+ rust-inspired pattern for explicit error handling instead of the usual js yolo vibes
322
+
323
+ #### result shapes
324
+ - `Ok<Value>` — `{ok: true, value}`
325
+ - `Err<E>` — `{ok: false, error}`
326
+ - `Result<Value, E>` — either of the above
327
+
328
+ #### constructors
329
+ - `ok(value)` — make a successful result
330
+ ```ts
331
+ const result = ok("fusion stable")
332
+ // {ok: true, value: "fusion stable"}
333
+ ```
334
+ - `err(error)` — make a failed result
335
+ ```ts
336
+ const result = err("containment lost")
337
+ // {ok: false, error: "containment lost"}
338
+ ```
339
+
340
+ #### helpers
341
+ - `grab(result)` — get the value, or `undefined`
342
+ ```ts
343
+ grab(ok(123))
344
+ // 123
345
+
346
+ grab(err("nope"))
347
+ // undefined
348
+ ```
349
+ - `need(result)` — get the value, or throw
350
+ ```ts
351
+ need(ok(123))
352
+ // 123
353
+
354
+ need(err("containment lost"))
355
+ // throws Error("containment lost")
356
+ ```
357
+
318
358
 
319
359
 
320
360
  <br/>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e280/stz",
3
- "version": "0.2.26",
3
+ "version": "0.2.28",
4
4
  "description": "everyday ts fns for everything",
5
5
  "license": "MIT",
6
6
  "author": "Chase Moskal <chasemoskal@gmail.com>",
@@ -22,7 +22,7 @@
22
22
  "count": "find s -path '*/_archive' -prune -o -name '*.ts' -exec wc -l {} +"
23
23
  },
24
24
  "devDependencies": {
25
- "@e280/science": "^0.1.8",
25
+ "@e280/science": "^0.1.9",
26
26
  "@types/node": "^25.5.0",
27
27
  "typescript": "^6.0.2"
28
28
  },
package/s/index.ts CHANGED
@@ -23,8 +23,8 @@ export * from "./g/weak-map.js"
23
23
 
24
24
  export * from "./queue/queue.js"
25
25
 
26
+ export * from "./ok/index.js"
26
27
  export * from "./toq/index.js"
27
-
28
28
  export * from "./maybe/index.js"
29
29
 
30
30
  export * from "./all.js"
@@ -53,6 +53,7 @@ export * from "./provide.js"
53
53
  export * from "./pubsub.js"
54
54
  export * from "./scope.js"
55
55
  export * from "./templating.js"
56
+ export * from "./thrown.js"
56
57
  export * from "./time.js"
57
58
  export * from "./trash.js"
58
59
  export * from "./types.js"
@@ -0,0 +1,8 @@
1
+
2
+ import type {Err} from "../types/err.js"
3
+
4
+ /** failure */
5
+ export function err<E = string>(error: E): Err<E> {
6
+ return {ok: false, error}
7
+ }
8
+
@@ -0,0 +1,10 @@
1
+
2
+ import type {Result} from "../types/result.js"
3
+
4
+ /** get value or undefined */
5
+ export function grab<Value>(result: Result<Value, unknown>): Value | undefined {
6
+ return result.ok
7
+ ? result.value
8
+ : undefined
9
+ }
10
+
@@ -0,0 +1,20 @@
1
+
2
+ import type {Result} from "../types/result.js"
3
+
4
+ /** get value or throw error */
5
+ export function need<Value, E>(result: Result<Value, E>): Value {
6
+ if (result.ok)
7
+ return result.value
8
+
9
+ const {error} = result
10
+
11
+ if (error instanceof Error)
12
+ throw error
13
+
14
+ else if (typeof error === "string")
15
+ throw new Error(error)
16
+
17
+ else
18
+ throw new Error("unknown")
19
+ }
20
+
package/s/ok/fns/ok.ts ADDED
@@ -0,0 +1,8 @@
1
+
2
+ import type {Ok} from "../types/ok.js"
3
+
4
+ /** success */
5
+ export function ok<Value>(value: Value): Ok<Value> {
6
+ return {ok: true, value}
7
+ }
8
+
package/s/ok/index.ts ADDED
@@ -0,0 +1,10 @@
1
+
2
+ export * from "./fns/ok.js"
3
+ export * from "./fns/err.js"
4
+ export * from "./fns/grab.js"
5
+ export * from "./fns/need.js"
6
+
7
+ export * from "./types/ok.js"
8
+ export * from "./types/err.js"
9
+ export * from "./types/result.js"
10
+
package/s/ok/test.ts ADDED
@@ -0,0 +1,47 @@
1
+
2
+ import {expect, suite, test} from "@e280/science"
3
+ import {thrown} from "../thrown.js"
4
+ import {ok, err, grab, need} from "./index.js"
5
+
6
+ export default suite({
7
+ "ok": test(async() => {
8
+ expect(ok(123).ok).is(true)
9
+ expect(ok(123).value).is(123)
10
+ expect("error" in ok(123)).is(false)
11
+ }),
12
+
13
+ "err": test(async() => {
14
+ expect(err("nope").ok).is(false)
15
+ expect(err("nope").error).is("nope")
16
+ expect("value" in err("nope")).is(false)
17
+ }),
18
+
19
+ "grab": test(async() => {
20
+ expect(grab(ok(123))).is(123)
21
+ expect(grab(err("nope"))).is(undefined)
22
+ }),
23
+
24
+ "need": test(async() => {
25
+ expect(need(ok(123))).is(123)
26
+ expect(() => need(err("nope"))).throws()
27
+ }),
28
+
29
+ "need error handling": {
30
+ "rethrows errors": test(async() => {
31
+ const error = new TypeError()
32
+ expect(() => need(err(error))).throws(TypeError)
33
+ expect(thrown(() => need(err(error)))).is(error)
34
+ }),
35
+
36
+ "upgrades strings to errors": test(async() => {
37
+ expect(() => need(err("nope"))).throws(Error)
38
+ expect(thrown(() => need(err("nope"))).message).is("nope")
39
+ }),
40
+
41
+ "everything else is unknown": test(async() => {
42
+ expect(() => need(err({code: 234}))).throws(Error)
43
+ expect(thrown(() => need(err({code: 234}))).message).is("unknown")
44
+ }),
45
+ },
46
+ })
47
+
@@ -0,0 +1,4 @@
1
+
2
+ /** failure */
3
+ export type Err<E> = {ok: false, error: E}
4
+
@@ -0,0 +1,4 @@
1
+
2
+ /** success */
3
+ export type Ok<Value> = {ok: true, value: Value}
4
+
@@ -0,0 +1,10 @@
1
+
2
+ import type {Ok} from "./ok.js"
3
+ import type {Err} from "./err.js"
4
+
5
+ /** success or failure */
6
+ export type Result<Value, E = unknown> = (
7
+ | Ok<Value>
8
+ | Err<E>
9
+ )
10
+
package/s/tests.test.ts CHANGED
@@ -8,6 +8,7 @@ import deep from "./deep/index.test.js"
8
8
  import queue from "./queue/queue.test.js"
9
9
  import toq from "./toq/toq.test.js"
10
10
  import maybe from "./maybe/test.js"
11
+ import ok from "./ok/test.js"
11
12
 
12
13
  await Science.run({
13
14
  data,
@@ -17,5 +18,5 @@ await Science.run({
17
18
  queue,
18
19
  toq,
19
20
  maybe,
21
+ ok,
20
22
  })
21
-
package/s/thrown.ts ADDED
@@ -0,0 +1,7 @@
1
+
2
+ export function thrown(fn: () => unknown): any {
3
+ try { fn() }
4
+ catch (error) { return error }
5
+ throw new Error("didn't throw")
6
+ }
7
+
package/x/index.d.ts CHANGED
@@ -17,6 +17,7 @@ export * from "./g/pool.js";
17
17
  export * from "./g/set.js";
18
18
  export * from "./g/weak-map.js";
19
19
  export * from "./queue/queue.js";
20
+ export * from "./ok/index.js";
20
21
  export * from "./toq/index.js";
21
22
  export * from "./maybe/index.js";
22
23
  export * from "./all.js";
@@ -45,6 +46,7 @@ export * from "./provide.js";
45
46
  export * from "./pubsub.js";
46
47
  export * from "./scope.js";
47
48
  export * from "./templating.js";
49
+ export * from "./thrown.js";
48
50
  export * from "./time.js";
49
51
  export * from "./trash.js";
50
52
  export * from "./types.js";
package/x/index.js CHANGED
@@ -17,6 +17,7 @@ export * from "./g/pool.js";
17
17
  export * from "./g/set.js";
18
18
  export * from "./g/weak-map.js";
19
19
  export * from "./queue/queue.js";
20
+ export * from "./ok/index.js";
20
21
  export * from "./toq/index.js";
21
22
  export * from "./maybe/index.js";
22
23
  export * from "./all.js";
@@ -45,6 +46,7 @@ export * from "./provide.js";
45
46
  export * from "./pubsub.js";
46
47
  export * from "./scope.js";
47
48
  export * from "./templating.js";
49
+ export * from "./thrown.js";
48
50
  export * from "./time.js";
49
51
  export * from "./trash.js";
50
52
  export * from "./types.js";
package/x/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../s/index.ts"],"names":[],"mappings":"AACA,cAA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CAAA;AACvB,cAAc,WAAW,CAAA;AACzB,cAAc,WAAW,CAAA;AACzB,cAAc,cAAc,CAAA;AAC5B,cAAc,aAAa,CAAA;AAC3B,cAAc,YAAY,CAAA;AAC1B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,aAAa,CAAA;AAC3B,cAAc,WAAW,CAAA;AACzB,cAAc,YAAY,CAAA;AAC1B,cAAc,YAAY,CAAA;AAC1B,cAAc,YAAY,CAAA"}
@@ -0,0 +1,3 @@
1
+ import type { Err } from "../types/err.js";
2
+ /** failure */
3
+ export declare function err<E = string>(error: E): Err<E>;
@@ -0,0 +1,5 @@
1
+ /** failure */
2
+ export function err(error) {
3
+ return { ok: false, error };
4
+ }
5
+ //# sourceMappingURL=err.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"err.js","sourceRoot":"","sources":["../../../s/ok/fns/err.ts"],"names":[],"mappings":"AAGA,cAAc;AACd,MAAM,UAAU,GAAG,CAAa,KAAQ;IACvC,OAAO,EAAC,EAAE,EAAE,KAAK,EAAE,KAAK,EAAC,CAAA;AAC1B,CAAC"}
@@ -0,0 +1,3 @@
1
+ import type { Result } from "../types/result.js";
2
+ /** get value or undefined */
3
+ export declare function grab<Value>(result: Result<Value, unknown>): Value | undefined;
@@ -0,0 +1,7 @@
1
+ /** get value or undefined */
2
+ export function grab(result) {
3
+ return result.ok
4
+ ? result.value
5
+ : undefined;
6
+ }
7
+ //# sourceMappingURL=grab.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"grab.js","sourceRoot":"","sources":["../../../s/ok/fns/grab.ts"],"names":[],"mappings":"AAGA,6BAA6B;AAC7B,MAAM,UAAU,IAAI,CAAQ,MAA8B;IACzD,OAAO,MAAM,CAAC,EAAE;QACf,CAAC,CAAC,MAAM,CAAC,KAAK;QACd,CAAC,CAAC,SAAS,CAAA;AACb,CAAC"}
@@ -0,0 +1,3 @@
1
+ import type { Result } from "../types/result.js";
2
+ /** get value or throw error */
3
+ export declare function need<Value, E>(result: Result<Value, E>): Value;
@@ -0,0 +1,13 @@
1
+ /** get value or throw error */
2
+ export function need(result) {
3
+ if (result.ok)
4
+ return result.value;
5
+ const { error } = result;
6
+ if (error instanceof Error)
7
+ throw error;
8
+ else if (typeof error === "string")
9
+ throw new Error(error);
10
+ else
11
+ throw new Error("unknown");
12
+ }
13
+ //# sourceMappingURL=need.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"need.js","sourceRoot":"","sources":["../../../s/ok/fns/need.ts"],"names":[],"mappings":"AAGA,+BAA+B;AAC/B,MAAM,UAAU,IAAI,CAAW,MAAwB;IACtD,IAAI,MAAM,CAAC,EAAE;QACZ,OAAO,MAAM,CAAC,KAAK,CAAA;IAEpB,MAAM,EAAC,KAAK,EAAC,GAAG,MAAM,CAAA;IAEtB,IAAI,KAAK,YAAY,KAAK;QACzB,MAAM,KAAK,CAAA;SAEP,IAAI,OAAO,KAAK,KAAK,QAAQ;QACjC,MAAM,IAAI,KAAK,CAAC,KAAK,CAAC,CAAA;;QAGtB,MAAM,IAAI,KAAK,CAAC,SAAS,CAAC,CAAA;AAC5B,CAAC"}
@@ -0,0 +1,3 @@
1
+ import type { Ok } from "../types/ok.js";
2
+ /** success */
3
+ export declare function ok<Value>(value: Value): Ok<Value>;
package/x/ok/fns/ok.js ADDED
@@ -0,0 +1,5 @@
1
+ /** success */
2
+ export function ok(value) {
3
+ return { ok: true, value };
4
+ }
5
+ //# sourceMappingURL=ok.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ok.js","sourceRoot":"","sources":["../../../s/ok/fns/ok.ts"],"names":[],"mappings":"AAGA,cAAc;AACd,MAAM,UAAU,EAAE,CAAQ,KAAY;IACrC,OAAO,EAAC,EAAE,EAAE,IAAI,EAAE,KAAK,EAAC,CAAA;AACzB,CAAC"}
@@ -0,0 +1,7 @@
1
+ export * from "./fns/ok.js";
2
+ export * from "./fns/err.js";
3
+ export * from "./fns/grab.js";
4
+ export * from "./fns/need.js";
5
+ export * from "./types/ok.js";
6
+ export * from "./types/err.js";
7
+ export * from "./types/result.js";
package/x/ok/index.js ADDED
@@ -0,0 +1,8 @@
1
+ export * from "./fns/ok.js";
2
+ export * from "./fns/err.js";
3
+ export * from "./fns/grab.js";
4
+ export * from "./fns/need.js";
5
+ export * from "./types/ok.js";
6
+ export * from "./types/err.js";
7
+ export * from "./types/result.js";
8
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../s/ok/index.ts"],"names":[],"mappings":"AACA,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAC5B,cAAc,eAAe,CAAA;AAC7B,cAAc,eAAe,CAAA;AAE7B,cAAc,eAAe,CAAA;AAC7B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,mBAAmB,CAAA"}
package/x/ok/test.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ declare const _default: {
2
+ ok: import("@e280/science").Test;
3
+ err: import("@e280/science").Test;
4
+ grab: import("@e280/science").Test;
5
+ need: import("@e280/science").Test;
6
+ "need error handling": {
7
+ "rethrows errors": import("@e280/science").Test;
8
+ "upgrades strings to errors": import("@e280/science").Test;
9
+ "everything else is unknown": import("@e280/science").Test;
10
+ };
11
+ };
12
+ export default _default;
package/x/ok/test.js ADDED
@@ -0,0 +1,39 @@
1
+ import { expect, suite, test } from "@e280/science";
2
+ import { thrown } from "../thrown.js";
3
+ import { ok, err, grab, need } from "./index.js";
4
+ export default suite({
5
+ "ok": test(async () => {
6
+ expect(ok(123).ok).is(true);
7
+ expect(ok(123).value).is(123);
8
+ expect("error" in ok(123)).is(false);
9
+ }),
10
+ "err": test(async () => {
11
+ expect(err("nope").ok).is(false);
12
+ expect(err("nope").error).is("nope");
13
+ expect("value" in err("nope")).is(false);
14
+ }),
15
+ "grab": test(async () => {
16
+ expect(grab(ok(123))).is(123);
17
+ expect(grab(err("nope"))).is(undefined);
18
+ }),
19
+ "need": test(async () => {
20
+ expect(need(ok(123))).is(123);
21
+ expect(() => need(err("nope"))).throws();
22
+ }),
23
+ "need error handling": {
24
+ "rethrows errors": test(async () => {
25
+ const error = new TypeError();
26
+ expect(() => need(err(error))).throws(TypeError);
27
+ expect(thrown(() => need(err(error)))).is(error);
28
+ }),
29
+ "upgrades strings to errors": test(async () => {
30
+ expect(() => need(err("nope"))).throws(Error);
31
+ expect(thrown(() => need(err("nope"))).message).is("nope");
32
+ }),
33
+ "everything else is unknown": test(async () => {
34
+ expect(() => need(err({ code: 234 }))).throws(Error);
35
+ expect(thrown(() => need(err({ code: 234 }))).message).is("unknown");
36
+ }),
37
+ },
38
+ });
39
+ //# sourceMappingURL=test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"test.js","sourceRoot":"","sources":["../../s/ok/test.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,MAAM,EAAE,KAAK,EAAE,IAAI,EAAC,MAAM,eAAe,CAAA;AACjD,OAAO,EAAC,MAAM,EAAC,MAAM,cAAc,CAAA;AACnC,OAAO,EAAC,EAAE,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,EAAC,MAAM,YAAY,CAAA;AAE9C,eAAe,KAAK,CAAC;IACpB,IAAI,EAAE,IAAI,CAAC,KAAK,IAAG,EAAE;QACpB,MAAM,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,CAAA;QAC3B,MAAM,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,CAAA;QAC7B,MAAM,CAAC,OAAO,IAAI,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAA;IACrC,CAAC,CAAC;IAEF,KAAK,EAAE,IAAI,CAAC,KAAK,IAAG,EAAE;QACrB,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CA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AAG,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;YAC7C,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,EAAE,CAAC,MAAM,CAAC,CAAA;QAC3D,CAAC,CAAC;QAEF,4BAA4B,EAAE,IAAI,CAAC,KAAK,IAAG,EAAE;YAC5C,MAAM,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,EAAC,IAAI,EAAE,GAAG,EAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;YAClD,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,EAAC,IAAI,EAAE,GAAG,EAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,EAAE,CAAC,SAAS,CAAC,CAAA;QACnE,CAAC,CAAC;KACF;CACD,CAAC,CAAA"}
@@ -0,0 +1,5 @@
1
+ /** failure */
2
+ export type Err<E> = {
3
+ ok: false;
4
+ error: E;
5
+ };
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=err.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"err.js","sourceRoot":"","sources":["../../../s/ok/types/err.ts"],"names":[],"mappings":""}
@@ -0,0 +1,5 @@
1
+ /** success */
2
+ export type Ok<Value> = {
3
+ ok: true;
4
+ value: Value;
5
+ };
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=ok.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ok.js","sourceRoot":"","sources":["../../../s/ok/types/ok.ts"],"names":[],"mappings":""}
@@ -0,0 +1,4 @@
1
+ import type { Ok } from "./ok.js";
2
+ import type { Err } from "./err.js";
3
+ /** success or failure */
4
+ export type Result<Value, E = unknown> = (Ok<Value> | Err<E>);
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=result.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"result.js","sourceRoot":"","sources":["../../../s/ok/types/result.ts"],"names":[],"mappings":""}
package/x/tests.test.js CHANGED
@@ -6,6 +6,7 @@ import deep from "./deep/index.test.js";
6
6
  import queue from "./queue/queue.test.js";
7
7
  import toq from "./toq/toq.test.js";
8
8
  import maybe from "./maybe/test.js";
9
+ import ok from "./ok/test.js";
9
10
  await Science.run({
10
11
  data,
11
12
  debounce,
@@ -14,5 +15,6 @@ await Science.run({
14
15
  queue,
15
16
  toq,
16
17
  maybe,
18
+ ok,
17
19
  });
18
20
  //# sourceMappingURL=tests.test.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"tests.test.js","sourceRoot":"","sources":["../s/tests.test.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,OAAO,EAAC,MAAM,eAAe,CAAA;AAErC,OAAO,IAAI,MAAM,qBAAqB,CAAA;AACtC,OAAO,QAAQ,MAAM,6BAA6B,CAAA;AAClD,OAAO,WAAW,MAAM,gCAAgC,CAAA;AACxD,OAAO,IAAI,MAAM,sBAAsB,CAAA;AACvC,OAAO,KAAK,MAAM,uBAAuB,CAAA;AACzC,OAAO,GAAG,MAAM,mBAAmB,CAAA;AACnC,OAAO,KAAK,MAAM,iBAAiB,CAAA;AAEnC,MAAM,OAAO,CAAC,GAAG,CAAC;IACjB,IAAI;IACJ,QAAQ;IACR,WAAW;IACX,IAAI;IACJ,KAAK;IACL,GAAG;IACH,KAAK;CACL,CAAC,CAAA"}
1
+ {"version":3,"file":"tests.test.js","sourceRoot":"","sources":["../s/tests.test.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,OAAO,EAAC,MAAM,eAAe,CAAA;AAErC,OAAO,IAAI,MAAM,qBAAqB,CAAA;AACtC,OAAO,QAAQ,MAAM,6BAA6B,CAAA;AAClD,OAAO,WAAW,MAAM,gCAAgC,CAAA;AACxD,OAAO,IAAI,MAAM,sBAAsB,CAAA;AACvC,OAAO,KAAK,MAAM,uBAAuB,CAAA;AACzC,OAAO,GAAG,MAAM,mBAAmB,CAAA;AACnC,OAAO,KAAK,MAAM,iBAAiB,CAAA;AACnC,OAAO,EAAE,MAAM,cAAc,CAAA;AAE7B,MAAM,OAAO,CAAC,GAAG,CAAC;IACjB,IAAI;IACJ,QAAQ;IACR,WAAW;IACX,IAAI;IACJ,KAAK;IACL,GAAG;IACH,KAAK;IACL,EAAE;CACF,CAAC,CAAA"}
package/x/thrown.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare function thrown(fn: () => unknown): any;
package/x/thrown.js ADDED
@@ -0,0 +1,10 @@
1
+ export function thrown(fn) {
2
+ try {
3
+ fn();
4
+ }
5
+ catch (error) {
6
+ return error;
7
+ }
8
+ throw new Error("didn't throw");
9
+ }
10
+ //# sourceMappingURL=thrown.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"thrown.js","sourceRoot":"","sources":["../s/thrown.ts"],"names":[],"mappings":"AACA,MAAM,UAAU,MAAM,CAAC,EAAiB;IACvC,IAAI,CAAC;QAAC,EAAE,EAAE,CAAA;IAAC,CAAC;IACZ,OAAO,KAAK,EAAE,CAAC;QAAC,OAAO,KAAK,CAAA;IAAC,CAAC;IAC9B,MAAM,IAAI,KAAK,CAAC,cAAc,CAAC,CAAA;AAChC,CAAC"}