@unthrown/pattern 0.3.0 → 1.1.0
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/README.md +6 -6
- package/dist/index.cjs +8 -8
- package/dist/index.d.cts +9 -9
- package/dist/index.d.mts +9 -9
- package/dist/index.mjs +6 -6
- package/dist/index.mjs.map +1 -1
- package/docs/index.md +28 -28
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -19,18 +19,18 @@ import { match } from "ts-pattern";
|
|
|
19
19
|
import * as P from "@unthrown/pattern";
|
|
20
20
|
|
|
21
21
|
match(result)
|
|
22
|
-
.with(P.
|
|
23
|
-
.with(P.
|
|
24
|
-
.with(P.
|
|
25
|
-
.with(P.
|
|
22
|
+
.with(P.Ok(), ({ value }) => `ok: ${value}`)
|
|
23
|
+
.with(P.Err(P.tag("Forbidden")), ({ error }) => `403 ${error.user}`)
|
|
24
|
+
.with(P.Err(), () => "error")
|
|
25
|
+
.with(P.Defect(), () => "bug")
|
|
26
26
|
.exhaustive();
|
|
27
27
|
```
|
|
28
28
|
|
|
29
|
-
- `P.
|
|
29
|
+
- `P.Ok(sub?)` / `P.Err(sub?)` / `P.Defect(sub?)` — match a channel; pass a
|
|
30
30
|
sub-pattern to constrain or select the payload: a literal, or any `ts-pattern`
|
|
31
31
|
pattern (e.g. `ts-pattern`'s own `P.string` / `P.select()`, imported from
|
|
32
32
|
`ts-pattern`). (Or skip the sugar and match `{ tag: "Ok", … }` directly.)
|
|
33
|
-
- `P.tag(t)` — sugar for `{ _tag: t }`; nested in `P.
|
|
33
|
+
- `P.tag(t)` — sugar for `{ _tag: t }`; nested in `P.Err(...)` it narrows to the
|
|
34
34
|
matching `TaggedError` variant, including its payload.
|
|
35
35
|
|
|
36
36
|
For the everyday exhaustive case, `matchTags` in core is simpler. Reach for this
|
package/dist/index.cjs
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
//#region src/index.ts
|
|
3
|
-
function
|
|
3
|
+
function Ok(...args) {
|
|
4
4
|
return args.length === 0 ? { tag: "Ok" } : {
|
|
5
5
|
tag: "Ok",
|
|
6
6
|
value: args[0]
|
|
7
7
|
};
|
|
8
8
|
}
|
|
9
|
-
function
|
|
9
|
+
function Err(...args) {
|
|
10
10
|
return args.length === 0 ? { tag: "Err" } : {
|
|
11
11
|
tag: "Err",
|
|
12
12
|
error: args[0]
|
|
13
13
|
};
|
|
14
14
|
}
|
|
15
|
-
function
|
|
15
|
+
function Defect(...args) {
|
|
16
16
|
return args.length === 0 ? { tag: "Defect" } : {
|
|
17
17
|
tag: "Defect",
|
|
18
18
|
cause: args[0]
|
|
@@ -21,7 +21,7 @@ function defect(...args) {
|
|
|
21
21
|
/**
|
|
22
22
|
* A `ts-pattern` pattern matching any value whose `_tag` equals `value` (e.g. a
|
|
23
23
|
* `TaggedError`). Equivalent to the object pattern `{ _tag: value }`, but reads
|
|
24
|
-
* better nested inside an {@link
|
|
24
|
+
* better nested inside an {@link Err} pattern and narrows to the matching
|
|
25
25
|
* variant — including its payload.
|
|
26
26
|
*
|
|
27
27
|
* @typeParam Tag - the string literal tag to match.
|
|
@@ -29,14 +29,14 @@ function defect(...args) {
|
|
|
29
29
|
*
|
|
30
30
|
* @example
|
|
31
31
|
* ```ts
|
|
32
|
-
* .with(P.
|
|
32
|
+
* .with(P.Err(P.tag("Forbidden")), ({ error }) => error.user)
|
|
33
33
|
* ```
|
|
34
34
|
*/
|
|
35
35
|
function tag(value) {
|
|
36
36
|
return { _tag: value };
|
|
37
37
|
}
|
|
38
38
|
//#endregion
|
|
39
|
-
exports.
|
|
40
|
-
exports.
|
|
41
|
-
exports.
|
|
39
|
+
exports.Defect = Defect;
|
|
40
|
+
exports.Err = Err;
|
|
41
|
+
exports.Ok = Ok;
|
|
42
42
|
exports.tag = tag;
|
package/dist/index.d.cts
CHANGED
|
@@ -7,10 +7,10 @@
|
|
|
7
7
|
*
|
|
8
8
|
* @typeParam V - the sub-pattern matched against the `Ok` value.
|
|
9
9
|
*/
|
|
10
|
-
declare function
|
|
10
|
+
declare function Ok(): {
|
|
11
11
|
tag: "Ok";
|
|
12
12
|
};
|
|
13
|
-
declare function
|
|
13
|
+
declare function Ok<const V>(value: V): {
|
|
14
14
|
tag: "Ok";
|
|
15
15
|
value: V;
|
|
16
16
|
};
|
|
@@ -21,10 +21,10 @@ declare function ok<const V>(value: V): {
|
|
|
21
21
|
*
|
|
22
22
|
* @typeParam V - the sub-pattern matched against the `Err` error.
|
|
23
23
|
*/
|
|
24
|
-
declare function
|
|
24
|
+
declare function Err(): {
|
|
25
25
|
tag: "Err";
|
|
26
26
|
};
|
|
27
|
-
declare function
|
|
27
|
+
declare function Err<const V>(error: V): {
|
|
28
28
|
tag: "Err";
|
|
29
29
|
error: V;
|
|
30
30
|
};
|
|
@@ -35,17 +35,17 @@ declare function err<const V>(error: V): {
|
|
|
35
35
|
*
|
|
36
36
|
* @typeParam V - the sub-pattern matched against the `Defect` cause.
|
|
37
37
|
*/
|
|
38
|
-
declare function
|
|
38
|
+
declare function Defect(): {
|
|
39
39
|
tag: "Defect";
|
|
40
40
|
};
|
|
41
|
-
declare function
|
|
41
|
+
declare function Defect<const V>(cause: V): {
|
|
42
42
|
tag: "Defect";
|
|
43
43
|
cause: V;
|
|
44
44
|
};
|
|
45
45
|
/**
|
|
46
46
|
* A `ts-pattern` pattern matching any value whose `_tag` equals `value` (e.g. a
|
|
47
47
|
* `TaggedError`). Equivalent to the object pattern `{ _tag: value }`, but reads
|
|
48
|
-
* better nested inside an {@link
|
|
48
|
+
* better nested inside an {@link Err} pattern and narrows to the matching
|
|
49
49
|
* variant — including its payload.
|
|
50
50
|
*
|
|
51
51
|
* @typeParam Tag - the string literal tag to match.
|
|
@@ -53,12 +53,12 @@ declare function defect<const V>(cause: V): {
|
|
|
53
53
|
*
|
|
54
54
|
* @example
|
|
55
55
|
* ```ts
|
|
56
|
-
* .with(P.
|
|
56
|
+
* .with(P.Err(P.tag("Forbidden")), ({ error }) => error.user)
|
|
57
57
|
* ```
|
|
58
58
|
*/
|
|
59
59
|
declare function tag<const Tag extends string>(value: Tag): {
|
|
60
60
|
_tag: Tag;
|
|
61
61
|
};
|
|
62
62
|
//#endregion
|
|
63
|
-
export {
|
|
63
|
+
export { Defect, Err, Ok, tag };
|
|
64
64
|
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.mts
CHANGED
|
@@ -7,10 +7,10 @@
|
|
|
7
7
|
*
|
|
8
8
|
* @typeParam V - the sub-pattern matched against the `Ok` value.
|
|
9
9
|
*/
|
|
10
|
-
declare function
|
|
10
|
+
declare function Ok(): {
|
|
11
11
|
tag: "Ok";
|
|
12
12
|
};
|
|
13
|
-
declare function
|
|
13
|
+
declare function Ok<const V>(value: V): {
|
|
14
14
|
tag: "Ok";
|
|
15
15
|
value: V;
|
|
16
16
|
};
|
|
@@ -21,10 +21,10 @@ declare function ok<const V>(value: V): {
|
|
|
21
21
|
*
|
|
22
22
|
* @typeParam V - the sub-pattern matched against the `Err` error.
|
|
23
23
|
*/
|
|
24
|
-
declare function
|
|
24
|
+
declare function Err(): {
|
|
25
25
|
tag: "Err";
|
|
26
26
|
};
|
|
27
|
-
declare function
|
|
27
|
+
declare function Err<const V>(error: V): {
|
|
28
28
|
tag: "Err";
|
|
29
29
|
error: V;
|
|
30
30
|
};
|
|
@@ -35,17 +35,17 @@ declare function err<const V>(error: V): {
|
|
|
35
35
|
*
|
|
36
36
|
* @typeParam V - the sub-pattern matched against the `Defect` cause.
|
|
37
37
|
*/
|
|
38
|
-
declare function
|
|
38
|
+
declare function Defect(): {
|
|
39
39
|
tag: "Defect";
|
|
40
40
|
};
|
|
41
|
-
declare function
|
|
41
|
+
declare function Defect<const V>(cause: V): {
|
|
42
42
|
tag: "Defect";
|
|
43
43
|
cause: V;
|
|
44
44
|
};
|
|
45
45
|
/**
|
|
46
46
|
* A `ts-pattern` pattern matching any value whose `_tag` equals `value` (e.g. a
|
|
47
47
|
* `TaggedError`). Equivalent to the object pattern `{ _tag: value }`, but reads
|
|
48
|
-
* better nested inside an {@link
|
|
48
|
+
* better nested inside an {@link Err} pattern and narrows to the matching
|
|
49
49
|
* variant — including its payload.
|
|
50
50
|
*
|
|
51
51
|
* @typeParam Tag - the string literal tag to match.
|
|
@@ -53,12 +53,12 @@ declare function defect<const V>(cause: V): {
|
|
|
53
53
|
*
|
|
54
54
|
* @example
|
|
55
55
|
* ```ts
|
|
56
|
-
* .with(P.
|
|
56
|
+
* .with(P.Err(P.tag("Forbidden")), ({ error }) => error.user)
|
|
57
57
|
* ```
|
|
58
58
|
*/
|
|
59
59
|
declare function tag<const Tag extends string>(value: Tag): {
|
|
60
60
|
_tag: Tag;
|
|
61
61
|
};
|
|
62
62
|
//#endregion
|
|
63
|
-
export {
|
|
63
|
+
export { Defect, Err, Ok, tag };
|
|
64
64
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.mjs
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
//#region src/index.ts
|
|
2
|
-
function
|
|
2
|
+
function Ok(...args) {
|
|
3
3
|
return args.length === 0 ? { tag: "Ok" } : {
|
|
4
4
|
tag: "Ok",
|
|
5
5
|
value: args[0]
|
|
6
6
|
};
|
|
7
7
|
}
|
|
8
|
-
function
|
|
8
|
+
function Err(...args) {
|
|
9
9
|
return args.length === 0 ? { tag: "Err" } : {
|
|
10
10
|
tag: "Err",
|
|
11
11
|
error: args[0]
|
|
12
12
|
};
|
|
13
13
|
}
|
|
14
|
-
function
|
|
14
|
+
function Defect(...args) {
|
|
15
15
|
return args.length === 0 ? { tag: "Defect" } : {
|
|
16
16
|
tag: "Defect",
|
|
17
17
|
cause: args[0]
|
|
@@ -20,7 +20,7 @@ function defect(...args) {
|
|
|
20
20
|
/**
|
|
21
21
|
* A `ts-pattern` pattern matching any value whose `_tag` equals `value` (e.g. a
|
|
22
22
|
* `TaggedError`). Equivalent to the object pattern `{ _tag: value }`, but reads
|
|
23
|
-
* better nested inside an {@link
|
|
23
|
+
* better nested inside an {@link Err} pattern and narrows to the matching
|
|
24
24
|
* variant — including its payload.
|
|
25
25
|
*
|
|
26
26
|
* @typeParam Tag - the string literal tag to match.
|
|
@@ -28,13 +28,13 @@ function defect(...args) {
|
|
|
28
28
|
*
|
|
29
29
|
* @example
|
|
30
30
|
* ```ts
|
|
31
|
-
* .with(P.
|
|
31
|
+
* .with(P.Err(P.tag("Forbidden")), ({ error }) => error.user)
|
|
32
32
|
* ```
|
|
33
33
|
*/
|
|
34
34
|
function tag(value) {
|
|
35
35
|
return { _tag: value };
|
|
36
36
|
}
|
|
37
37
|
//#endregion
|
|
38
|
-
export {
|
|
38
|
+
export { Defect, Err, Ok, tag };
|
|
39
39
|
|
|
40
40
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["// @unthrown/pattern — native `ts-pattern` interop for `Result`.\n//\n// A `Result` is a discriminated union (`{ tag: \"Ok\" | \"Err\" | \"Defect\" }`), so\n// `ts-pattern` matches it directly — narrowing, selection, and `.exhaustive()`\n// all work out of the box. This package is just sugar: pattern constructors so\n// you can write `P.
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["// @unthrown/pattern — native `ts-pattern` interop for `Result`.\n//\n// A `Result` is a discriminated union (`{ tag: \"Ok\" | \"Err\" | \"Defect\" }`), so\n// `ts-pattern` matches it directly — narrowing, selection, and `.exhaustive()`\n// all work out of the box. This package is just sugar: pattern constructors so\n// you can write `P.Ok(...)` instead of the raw `{ tag: \"Ok\", value: ... }`\n// object pattern, plus `tag` for matching a `TaggedError` by its `_tag`.\n//\n// import { match } from \"ts-pattern\";\n// import * as P from \"@unthrown/pattern\";\n//\n// match(result)\n// .with(P.Ok(), ({ value }) => `ok: ${value}`)\n// .with(P.Err(P.tag(\"NotFound\")), () => \"404\")\n// .with(P.Err(), ({ error }) => `error: ${error}`)\n// .with(P.Defect(), ({ cause }) => `bug: ${String(cause)}`)\n// .exhaustive();\n//\n// Here `P` is THIS package. To also use ts-pattern's own patterns (wildcards,\n// `P.select()`, `P.string`, …), import ts-pattern's `P` under another name:\n//\n// import { match, P as t } from \"ts-pattern\";\n// import * as P from \"@unthrown/pattern\";\n// match(result).with(P.Ok(t.select()), (value) => value).otherwise(() => …);\n\n/**\n * A `ts-pattern` pattern matching the `Ok` variant of a `Result`. With no\n * argument it matches any `Ok`; pass a sub-pattern to constrain or select the\n * `value` — a literal, or any `ts-pattern` pattern (e.g. `ts-pattern`'s own\n * `P.string` / `P.select()`, imported from `ts-pattern`, not this package).\n *\n * @typeParam V - the sub-pattern matched against the `Ok` value.\n */\nexport function Ok(): { tag: \"Ok\" };\nexport function Ok<const V>(value: V): { tag: \"Ok\"; value: V };\nexport function Ok(...args: [] | [unknown]): { tag: \"Ok\"; value?: unknown } {\n return args.length === 0 ? { tag: \"Ok\" } : { tag: \"Ok\", value: args[0] };\n}\n\n/**\n * A `ts-pattern` pattern matching the `Err` variant of a `Result`. With no\n * argument it matches any `Err`; pass a sub-pattern (e.g. {@link tag}) to\n * constrain or select the `error`.\n *\n * @typeParam V - the sub-pattern matched against the `Err` error.\n */\nexport function Err(): { tag: \"Err\" };\nexport function Err<const V>(error: V): { tag: \"Err\"; error: V };\nexport function Err(...args: [] | [unknown]): { tag: \"Err\"; error?: unknown } {\n return args.length === 0 ? { tag: \"Err\" } : { tag: \"Err\", error: args[0] };\n}\n\n/**\n * A `ts-pattern` pattern matching the `Defect` variant of a `Result`. With no\n * argument it matches any `Defect`; pass a sub-pattern to constrain or select\n * the unknown `cause`.\n *\n * @typeParam V - the sub-pattern matched against the `Defect` cause.\n */\nexport function Defect(): { tag: \"Defect\" };\nexport function Defect<const V>(cause: V): { tag: \"Defect\"; cause: V };\nexport function Defect(...args: [] | [unknown]): { tag: \"Defect\"; cause?: unknown } {\n return args.length === 0 ? { tag: \"Defect\" } : { tag: \"Defect\", cause: args[0] };\n}\n\n/**\n * A `ts-pattern` pattern matching any value whose `_tag` equals `value` (e.g. a\n * `TaggedError`). Equivalent to the object pattern `{ _tag: value }`, but reads\n * better nested inside an {@link Err} pattern and narrows to the matching\n * variant — including its payload.\n *\n * @typeParam Tag - the string literal tag to match.\n * @param value - the `_tag` to match.\n *\n * @example\n * ```ts\n * .with(P.Err(P.tag(\"Forbidden\")), ({ error }) => error.user)\n * ```\n */\nexport function tag<const Tag extends string>(value: Tag): { _tag: Tag } {\n return { _tag: value };\n}\n"],"mappings":";AAmCA,SAAgB,GAAG,GAAG,MAAsD;CAC1E,OAAO,KAAK,WAAW,IAAI,EAAE,KAAK,KAAK,IAAI;EAAE,KAAK;EAAM,OAAO,KAAK;CAAG;AACzE;AAWA,SAAgB,IAAI,GAAG,MAAuD;CAC5E,OAAO,KAAK,WAAW,IAAI,EAAE,KAAK,MAAM,IAAI;EAAE,KAAK;EAAO,OAAO,KAAK;CAAG;AAC3E;AAWA,SAAgB,OAAO,GAAG,MAA0D;CAClF,OAAO,KAAK,WAAW,IAAI,EAAE,KAAK,SAAS,IAAI;EAAE,KAAK;EAAU,OAAO,KAAK;CAAG;AACjF;;;;;;;;;;;;;;;AAgBA,SAAgB,IAA8B,OAA2B;CACvE,OAAO,EAAE,MAAM,MAAM;AACvB"}
|
package/docs/index.md
CHANGED
|
@@ -6,15 +6,15 @@
|
|
|
6
6
|
|
|
7
7
|
## Functions
|
|
8
8
|
|
|
9
|
-
###
|
|
9
|
+
### Defect()
|
|
10
10
|
|
|
11
11
|
#### Call Signature
|
|
12
12
|
|
|
13
13
|
```ts
|
|
14
|
-
function
|
|
14
|
+
function Defect(): object;
|
|
15
15
|
```
|
|
16
16
|
|
|
17
|
-
Defined in: [index.ts:60](https://github.com/btravstack/unthrown/blob/
|
|
17
|
+
Defined in: [index.ts:60](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/pattern/src/index.ts#L60)
|
|
18
18
|
|
|
19
19
|
A `ts-pattern` pattern matching the `Defect` variant of a `Result`. With no
|
|
20
20
|
argument it matches any `Defect`; pass a sub-pattern to constrain or select
|
|
@@ -26,15 +26,15 @@ the unknown `cause`.
|
|
|
26
26
|
|
|
27
27
|
| Name | Type | Defined in |
|
|
28
28
|
| ------ | ------ | ------ |
|
|
29
|
-
| `tag` | `"Defect"` | [index.ts:60](https://github.com/btravstack/unthrown/blob/
|
|
29
|
+
| `tag` | `"Defect"` | [index.ts:60](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/pattern/src/index.ts#L60) |
|
|
30
30
|
|
|
31
31
|
#### Call Signature
|
|
32
32
|
|
|
33
33
|
```ts
|
|
34
|
-
function
|
|
34
|
+
function Defect<V>(cause): object;
|
|
35
35
|
```
|
|
36
36
|
|
|
37
|
-
Defined in: [index.ts:61](https://github.com/btravstack/unthrown/blob/
|
|
37
|
+
Defined in: [index.ts:61](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/pattern/src/index.ts#L61)
|
|
38
38
|
|
|
39
39
|
A `ts-pattern` pattern matching the `Defect` variant of a `Result`. With no
|
|
40
40
|
argument it matches any `Defect`; pass a sub-pattern to constrain or select
|
|
@@ -58,20 +58,20 @@ the unknown `cause`.
|
|
|
58
58
|
|
|
59
59
|
| Name | Type | Defined in |
|
|
60
60
|
| ------ | ------ | ------ |
|
|
61
|
-
| `cause` | `V` | [index.ts:61](https://github.com/btravstack/unthrown/blob/
|
|
62
|
-
| `tag` | `"Defect"` | [index.ts:61](https://github.com/btravstack/unthrown/blob/
|
|
61
|
+
| `cause` | `V` | [index.ts:61](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/pattern/src/index.ts#L61) |
|
|
62
|
+
| `tag` | `"Defect"` | [index.ts:61](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/pattern/src/index.ts#L61) |
|
|
63
63
|
|
|
64
64
|
***
|
|
65
65
|
|
|
66
|
-
###
|
|
66
|
+
### Err()
|
|
67
67
|
|
|
68
68
|
#### Call Signature
|
|
69
69
|
|
|
70
70
|
```ts
|
|
71
|
-
function
|
|
71
|
+
function Err(): object;
|
|
72
72
|
```
|
|
73
73
|
|
|
74
|
-
Defined in: [index.ts:47](https://github.com/btravstack/unthrown/blob/
|
|
74
|
+
Defined in: [index.ts:47](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/pattern/src/index.ts#L47)
|
|
75
75
|
|
|
76
76
|
A `ts-pattern` pattern matching the `Err` variant of a `Result`. With no
|
|
77
77
|
argument it matches any `Err`; pass a sub-pattern (e.g. [tag](#tag)) to
|
|
@@ -83,15 +83,15 @@ constrain or select the `error`.
|
|
|
83
83
|
|
|
84
84
|
| Name | Type | Defined in |
|
|
85
85
|
| ------ | ------ | ------ |
|
|
86
|
-
| `tag` | `"Err"` | [index.ts:47](https://github.com/btravstack/unthrown/blob/
|
|
86
|
+
| `tag` | `"Err"` | [index.ts:47](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/pattern/src/index.ts#L47) |
|
|
87
87
|
|
|
88
88
|
#### Call Signature
|
|
89
89
|
|
|
90
90
|
```ts
|
|
91
|
-
function
|
|
91
|
+
function Err<V>(error): object;
|
|
92
92
|
```
|
|
93
93
|
|
|
94
|
-
Defined in: [index.ts:48](https://github.com/btravstack/unthrown/blob/
|
|
94
|
+
Defined in: [index.ts:48](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/pattern/src/index.ts#L48)
|
|
95
95
|
|
|
96
96
|
A `ts-pattern` pattern matching the `Err` variant of a `Result`. With no
|
|
97
97
|
argument it matches any `Err`; pass a sub-pattern (e.g. [tag](#tag)) to
|
|
@@ -115,20 +115,20 @@ constrain or select the `error`.
|
|
|
115
115
|
|
|
116
116
|
| Name | Type | Defined in |
|
|
117
117
|
| ------ | ------ | ------ |
|
|
118
|
-
| `error` | `V` | [index.ts:48](https://github.com/btravstack/unthrown/blob/
|
|
119
|
-
| `tag` | `"Err"` | [index.ts:48](https://github.com/btravstack/unthrown/blob/
|
|
118
|
+
| `error` | `V` | [index.ts:48](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/pattern/src/index.ts#L48) |
|
|
119
|
+
| `tag` | `"Err"` | [index.ts:48](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/pattern/src/index.ts#L48) |
|
|
120
120
|
|
|
121
121
|
***
|
|
122
122
|
|
|
123
|
-
###
|
|
123
|
+
### Ok()
|
|
124
124
|
|
|
125
125
|
#### Call Signature
|
|
126
126
|
|
|
127
127
|
```ts
|
|
128
|
-
function
|
|
128
|
+
function Ok(): object;
|
|
129
129
|
```
|
|
130
130
|
|
|
131
|
-
Defined in: [index.ts:34](https://github.com/btravstack/unthrown/blob/
|
|
131
|
+
Defined in: [index.ts:34](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/pattern/src/index.ts#L34)
|
|
132
132
|
|
|
133
133
|
A `ts-pattern` pattern matching the `Ok` variant of a `Result`. With no
|
|
134
134
|
argument it matches any `Ok`; pass a sub-pattern to constrain or select the
|
|
@@ -141,15 +141,15 @@ argument it matches any `Ok`; pass a sub-pattern to constrain or select the
|
|
|
141
141
|
|
|
142
142
|
| Name | Type | Defined in |
|
|
143
143
|
| ------ | ------ | ------ |
|
|
144
|
-
| `tag` | `"Ok"` | [index.ts:34](https://github.com/btravstack/unthrown/blob/
|
|
144
|
+
| `tag` | `"Ok"` | [index.ts:34](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/pattern/src/index.ts#L34) |
|
|
145
145
|
|
|
146
146
|
#### Call Signature
|
|
147
147
|
|
|
148
148
|
```ts
|
|
149
|
-
function
|
|
149
|
+
function Ok<V>(value): object;
|
|
150
150
|
```
|
|
151
151
|
|
|
152
|
-
Defined in: [index.ts:35](https://github.com/btravstack/unthrown/blob/
|
|
152
|
+
Defined in: [index.ts:35](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/pattern/src/index.ts#L35)
|
|
153
153
|
|
|
154
154
|
A `ts-pattern` pattern matching the `Ok` variant of a `Result`. With no
|
|
155
155
|
argument it matches any `Ok`; pass a sub-pattern to constrain or select the
|
|
@@ -174,8 +174,8 @@ argument it matches any `Ok`; pass a sub-pattern to constrain or select the
|
|
|
174
174
|
|
|
175
175
|
| Name | Type | Defined in |
|
|
176
176
|
| ------ | ------ | ------ |
|
|
177
|
-
| `tag` | `"Ok"` | [index.ts:35](https://github.com/btravstack/unthrown/blob/
|
|
178
|
-
| `value` | `V` | [index.ts:35](https://github.com/btravstack/unthrown/blob/
|
|
177
|
+
| `tag` | `"Ok"` | [index.ts:35](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/pattern/src/index.ts#L35) |
|
|
178
|
+
| `value` | `V` | [index.ts:35](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/pattern/src/index.ts#L35) |
|
|
179
179
|
|
|
180
180
|
***
|
|
181
181
|
|
|
@@ -185,11 +185,11 @@ argument it matches any `Ok`; pass a sub-pattern to constrain or select the
|
|
|
185
185
|
function tag<Tag>(value): object;
|
|
186
186
|
```
|
|
187
187
|
|
|
188
|
-
Defined in: [index.ts:80](https://github.com/btravstack/unthrown/blob/
|
|
188
|
+
Defined in: [index.ts:80](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/pattern/src/index.ts#L80)
|
|
189
189
|
|
|
190
190
|
A `ts-pattern` pattern matching any value whose `_tag` equals `value` (e.g. a
|
|
191
191
|
`TaggedError`). Equivalent to the object pattern `{ _tag: value }`, but reads
|
|
192
|
-
better nested inside an [
|
|
192
|
+
better nested inside an [Err](#err) pattern and narrows to the matching
|
|
193
193
|
variant — including its payload.
|
|
194
194
|
|
|
195
195
|
#### Type Parameters
|
|
@@ -210,10 +210,10 @@ variant — including its payload.
|
|
|
210
210
|
|
|
211
211
|
| Name | Type | Defined in |
|
|
212
212
|
| ------ | ------ | ------ |
|
|
213
|
-
| `_tag` | `Tag` | [index.ts:80](https://github.com/btravstack/unthrown/blob/
|
|
213
|
+
| `_tag` | `Tag` | [index.ts:80](https://github.com/btravstack/unthrown/blob/8424c0f1e5d5b49a3cb6853d52fb8d5085bd4701/packages/pattern/src/index.ts#L80) |
|
|
214
214
|
|
|
215
215
|
#### Example
|
|
216
216
|
|
|
217
217
|
```ts
|
|
218
|
-
.with(P.
|
|
218
|
+
.with(P.Err(P.tag("Forbidden")), ({ error }) => error.user)
|
|
219
219
|
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unthrown/pattern",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "ts-pattern integration for unthrown",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"errors-as-values",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"./package.json": "./package.json"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"unthrown": "
|
|
46
|
+
"unthrown": "1.1.0"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
49
|
"@types/node": "24.13.2",
|