@unthrown/pattern 0.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/LICENSE +21 -0
- package/README.md +43 -0
- package/dist/index.cjs +42 -0
- package/dist/index.d.cts +64 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +64 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +40 -0
- package/dist/index.mjs.map +1 -0
- package/docs/index.md +219 -0
- package/package.json +73 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Benoit Travers
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# @unthrown/pattern
|
|
2
|
+
|
|
3
|
+
> Thin [ts-pattern](https://github.com/gvergnaud/ts-pattern) integration for
|
|
4
|
+
> [unthrown](https://github.com/btravstack/unthrown)'s `Result`.
|
|
5
|
+
|
|
6
|
+
๐ **[Documentation](https://btravstack.github.io/unthrown/guide/pattern-matching)** ยท
|
|
7
|
+
[API Reference](https://btravstack.github.io/unthrown/api/pattern/)
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
pnpm add @unthrown/pattern ts-pattern
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
A `Result` is a discriminated union (`{ tag: "Ok" | "Err" | "Defect" }`), so
|
|
14
|
+
ts-pattern matches it **natively** โ narrowing, selection, and `.exhaustive()`
|
|
15
|
+
all work. This package is just pattern-constructor sugar.
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { match } from "ts-pattern";
|
|
19
|
+
import * as P from "@unthrown/pattern";
|
|
20
|
+
|
|
21
|
+
match(result)
|
|
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
|
+
.exhaustive();
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
- `P.ok(sub?)` / `P.err(sub?)` / `P.defect(sub?)` โ match a channel; pass a
|
|
30
|
+
sub-pattern to constrain or select the payload: a literal, or any `ts-pattern`
|
|
31
|
+
pattern (e.g. `ts-pattern`'s own `P.string` / `P.select()`, imported from
|
|
32
|
+
`ts-pattern`). (Or skip the sugar and match `{ tag: "Ok", โฆ }` directly.)
|
|
33
|
+
- `P.tag(t)` โ sugar for `{ _tag: t }`; nested in `P.err(...)` it narrows to the
|
|
34
|
+
matching `TaggedError` variant, including its payload.
|
|
35
|
+
|
|
36
|
+
For the everyday exhaustive case, `matchTags` in core is simpler. Reach for this
|
|
37
|
+
when you need ts-pattern's guards, nested patterns, or wildcards.
|
|
38
|
+
|
|
39
|
+
`ts-pattern` is a peer dependency.
|
|
40
|
+
|
|
41
|
+
## License
|
|
42
|
+
|
|
43
|
+
[MIT](../../LICENSE) ยฉ Benoit TRAVERS
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/index.ts
|
|
3
|
+
function ok(...args) {
|
|
4
|
+
return args.length === 0 ? { tag: "Ok" } : {
|
|
5
|
+
tag: "Ok",
|
|
6
|
+
value: args[0]
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
function err(...args) {
|
|
10
|
+
return args.length === 0 ? { tag: "Err" } : {
|
|
11
|
+
tag: "Err",
|
|
12
|
+
error: args[0]
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
function defect(...args) {
|
|
16
|
+
return args.length === 0 ? { tag: "Defect" } : {
|
|
17
|
+
tag: "Defect",
|
|
18
|
+
cause: args[0]
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* A `ts-pattern` pattern matching any value whose `_tag` equals `value` (e.g. a
|
|
23
|
+
* `TaggedError`). Equivalent to the object pattern `{ _tag: value }`, but reads
|
|
24
|
+
* better nested inside an {@link err} pattern and narrows to the matching
|
|
25
|
+
* variant โ including its payload.
|
|
26
|
+
*
|
|
27
|
+
* @typeParam Tag - the string literal tag to match.
|
|
28
|
+
* @param value - the `_tag` to match.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* .with(P.err(P.tag("Forbidden")), ({ error }) => error.user)
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
function tag(value) {
|
|
36
|
+
return { _tag: value };
|
|
37
|
+
}
|
|
38
|
+
//#endregion
|
|
39
|
+
exports.defect = defect;
|
|
40
|
+
exports.err = err;
|
|
41
|
+
exports.ok = ok;
|
|
42
|
+
exports.tag = tag;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
//#region src/index.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* A `ts-pattern` pattern matching the `Ok` variant of a `Result`. With no
|
|
4
|
+
* argument it matches any `Ok`; pass a sub-pattern to constrain or select the
|
|
5
|
+
* `value` โ a literal, or any `ts-pattern` pattern (e.g. `ts-pattern`'s own
|
|
6
|
+
* `P.string` / `P.select()`, imported from `ts-pattern`, not this package).
|
|
7
|
+
*
|
|
8
|
+
* @typeParam V - the sub-pattern matched against the `Ok` value.
|
|
9
|
+
*/
|
|
10
|
+
declare function ok(): {
|
|
11
|
+
tag: "Ok";
|
|
12
|
+
};
|
|
13
|
+
declare function ok<const V>(value: V): {
|
|
14
|
+
tag: "Ok";
|
|
15
|
+
value: V;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* A `ts-pattern` pattern matching the `Err` variant of a `Result`. With no
|
|
19
|
+
* argument it matches any `Err`; pass a sub-pattern (e.g. {@link tag}) to
|
|
20
|
+
* constrain or select the `error`.
|
|
21
|
+
*
|
|
22
|
+
* @typeParam V - the sub-pattern matched against the `Err` error.
|
|
23
|
+
*/
|
|
24
|
+
declare function err(): {
|
|
25
|
+
tag: "Err";
|
|
26
|
+
};
|
|
27
|
+
declare function err<const V>(error: V): {
|
|
28
|
+
tag: "Err";
|
|
29
|
+
error: V;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* A `ts-pattern` pattern matching the `Defect` variant of a `Result`. With no
|
|
33
|
+
* argument it matches any `Defect`; pass a sub-pattern to constrain or select
|
|
34
|
+
* the unknown `cause`.
|
|
35
|
+
*
|
|
36
|
+
* @typeParam V - the sub-pattern matched against the `Defect` cause.
|
|
37
|
+
*/
|
|
38
|
+
declare function defect(): {
|
|
39
|
+
tag: "Defect";
|
|
40
|
+
};
|
|
41
|
+
declare function defect<const V>(cause: V): {
|
|
42
|
+
tag: "Defect";
|
|
43
|
+
cause: V;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* A `ts-pattern` pattern matching any value whose `_tag` equals `value` (e.g. a
|
|
47
|
+
* `TaggedError`). Equivalent to the object pattern `{ _tag: value }`, but reads
|
|
48
|
+
* better nested inside an {@link err} pattern and narrows to the matching
|
|
49
|
+
* variant โ including its payload.
|
|
50
|
+
*
|
|
51
|
+
* @typeParam Tag - the string literal tag to match.
|
|
52
|
+
* @param value - the `_tag` to match.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* .with(P.err(P.tag("Forbidden")), ({ error }) => error.user)
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
declare function tag<const Tag extends string>(value: Tag): {
|
|
60
|
+
_tag: Tag;
|
|
61
|
+
};
|
|
62
|
+
//#endregion
|
|
63
|
+
export { defect, err, ok, tag };
|
|
64
|
+
//# sourceMappingURL=index.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/index.ts"],"mappings":";;AAiCA;;;;AAA2B;AAC3B;;iBADgB,EAAA;EAAQ,GAAG;AAAA;AAAA,iBACX,EAAA,UAAY,KAAA,EAAO,CAAA;EAAM,GAAA;EAAW,KAAA,EAAO,CAAC;AAAA;;;AAAA;AAY5D;;;;iBAAgB,GAAA;EAAS,GAAG;AAAA;AAAA,iBACZ,GAAA,UAAa,KAAA,EAAO,CAAA;EAAM,GAAA;EAAY,KAAA,EAAO,CAAC;AAAA;;;;;;AAAA;AAY9D;iBAAgB,MAAA;EAAY,GAAG;AAAA;AAAA,iBACf,MAAA,UAAgB,KAAA,EAAO,CAAA;EAAM,GAAA;EAAe,KAAA,EAAO,CAAC;AAAA;;;;;;;;;AAAA;AAmBpE;;;;;iBAAgB,GAAA,2BAA8B,KAAA,EAAO,GAAA;EAAQ,IAAA,EAAM,GAAG;AAAA"}
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
//#region src/index.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* A `ts-pattern` pattern matching the `Ok` variant of a `Result`. With no
|
|
4
|
+
* argument it matches any `Ok`; pass a sub-pattern to constrain or select the
|
|
5
|
+
* `value` โ a literal, or any `ts-pattern` pattern (e.g. `ts-pattern`'s own
|
|
6
|
+
* `P.string` / `P.select()`, imported from `ts-pattern`, not this package).
|
|
7
|
+
*
|
|
8
|
+
* @typeParam V - the sub-pattern matched against the `Ok` value.
|
|
9
|
+
*/
|
|
10
|
+
declare function ok(): {
|
|
11
|
+
tag: "Ok";
|
|
12
|
+
};
|
|
13
|
+
declare function ok<const V>(value: V): {
|
|
14
|
+
tag: "Ok";
|
|
15
|
+
value: V;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* A `ts-pattern` pattern matching the `Err` variant of a `Result`. With no
|
|
19
|
+
* argument it matches any `Err`; pass a sub-pattern (e.g. {@link tag}) to
|
|
20
|
+
* constrain or select the `error`.
|
|
21
|
+
*
|
|
22
|
+
* @typeParam V - the sub-pattern matched against the `Err` error.
|
|
23
|
+
*/
|
|
24
|
+
declare function err(): {
|
|
25
|
+
tag: "Err";
|
|
26
|
+
};
|
|
27
|
+
declare function err<const V>(error: V): {
|
|
28
|
+
tag: "Err";
|
|
29
|
+
error: V;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* A `ts-pattern` pattern matching the `Defect` variant of a `Result`. With no
|
|
33
|
+
* argument it matches any `Defect`; pass a sub-pattern to constrain or select
|
|
34
|
+
* the unknown `cause`.
|
|
35
|
+
*
|
|
36
|
+
* @typeParam V - the sub-pattern matched against the `Defect` cause.
|
|
37
|
+
*/
|
|
38
|
+
declare function defect(): {
|
|
39
|
+
tag: "Defect";
|
|
40
|
+
};
|
|
41
|
+
declare function defect<const V>(cause: V): {
|
|
42
|
+
tag: "Defect";
|
|
43
|
+
cause: V;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* A `ts-pattern` pattern matching any value whose `_tag` equals `value` (e.g. a
|
|
47
|
+
* `TaggedError`). Equivalent to the object pattern `{ _tag: value }`, but reads
|
|
48
|
+
* better nested inside an {@link err} pattern and narrows to the matching
|
|
49
|
+
* variant โ including its payload.
|
|
50
|
+
*
|
|
51
|
+
* @typeParam Tag - the string literal tag to match.
|
|
52
|
+
* @param value - the `_tag` to match.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* .with(P.err(P.tag("Forbidden")), ({ error }) => error.user)
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
declare function tag<const Tag extends string>(value: Tag): {
|
|
60
|
+
_tag: Tag;
|
|
61
|
+
};
|
|
62
|
+
//#endregion
|
|
63
|
+
export { defect, err, ok, tag };
|
|
64
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"mappings":";;AAiCA;;;;AAA2B;AAC3B;;iBADgB,EAAA;EAAQ,GAAG;AAAA;AAAA,iBACX,EAAA,UAAY,KAAA,EAAO,CAAA;EAAM,GAAA;EAAW,KAAA,EAAO,CAAC;AAAA;;;AAAA;AAY5D;;;;iBAAgB,GAAA;EAAS,GAAG;AAAA;AAAA,iBACZ,GAAA,UAAa,KAAA,EAAO,CAAA;EAAM,GAAA;EAAY,KAAA,EAAO,CAAC;AAAA;;;;;;AAAA;AAY9D;iBAAgB,MAAA;EAAY,GAAG;AAAA;AAAA,iBACf,MAAA,UAAgB,KAAA,EAAO,CAAA;EAAM,GAAA;EAAe,KAAA,EAAO,CAAC;AAAA;;;;;;;;;AAAA;AAmBpE;;;;;iBAAgB,GAAA,2BAA8B,KAAA,EAAO,GAAA;EAAQ,IAAA,EAAM,GAAG;AAAA"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
//#region src/index.ts
|
|
2
|
+
function ok(...args) {
|
|
3
|
+
return args.length === 0 ? { tag: "Ok" } : {
|
|
4
|
+
tag: "Ok",
|
|
5
|
+
value: args[0]
|
|
6
|
+
};
|
|
7
|
+
}
|
|
8
|
+
function err(...args) {
|
|
9
|
+
return args.length === 0 ? { tag: "Err" } : {
|
|
10
|
+
tag: "Err",
|
|
11
|
+
error: args[0]
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
function defect(...args) {
|
|
15
|
+
return args.length === 0 ? { tag: "Defect" } : {
|
|
16
|
+
tag: "Defect",
|
|
17
|
+
cause: args[0]
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* A `ts-pattern` pattern matching any value whose `_tag` equals `value` (e.g. a
|
|
22
|
+
* `TaggedError`). Equivalent to the object pattern `{ _tag: value }`, but reads
|
|
23
|
+
* better nested inside an {@link err} pattern and narrows to the matching
|
|
24
|
+
* variant โ including its payload.
|
|
25
|
+
*
|
|
26
|
+
* @typeParam Tag - the string literal tag to match.
|
|
27
|
+
* @param value - the `_tag` to match.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* .with(P.err(P.tag("Forbidden")), ({ error }) => error.user)
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
function tag(value) {
|
|
35
|
+
return { _tag: value };
|
|
36
|
+
}
|
|
37
|
+
//#endregion
|
|
38
|
+
export { defect, err, ok, tag };
|
|
39
|
+
|
|
40
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +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.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
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
**@unthrown/pattern**
|
|
2
|
+
|
|
3
|
+
***
|
|
4
|
+
|
|
5
|
+
# @unthrown/pattern
|
|
6
|
+
|
|
7
|
+
## Functions
|
|
8
|
+
|
|
9
|
+
### defect()
|
|
10
|
+
|
|
11
|
+
#### Call Signature
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
function defect(): object;
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Defined in: [index.ts:60](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/pattern/src/index.ts#L60)
|
|
18
|
+
|
|
19
|
+
A `ts-pattern` pattern matching the `Defect` variant of a `Result`. With no
|
|
20
|
+
argument it matches any `Defect`; pass a sub-pattern to constrain or select
|
|
21
|
+
the unknown `cause`.
|
|
22
|
+
|
|
23
|
+
##### Returns
|
|
24
|
+
|
|
25
|
+
`object`
|
|
26
|
+
|
|
27
|
+
| Name | Type | Defined in |
|
|
28
|
+
| ------ | ------ | ------ |
|
|
29
|
+
| `tag` | `"Defect"` | [index.ts:60](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/pattern/src/index.ts#L60) |
|
|
30
|
+
|
|
31
|
+
#### Call Signature
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
function defect<V>(cause): object;
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Defined in: [index.ts:61](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/pattern/src/index.ts#L61)
|
|
38
|
+
|
|
39
|
+
A `ts-pattern` pattern matching the `Defect` variant of a `Result`. With no
|
|
40
|
+
argument it matches any `Defect`; pass a sub-pattern to constrain or select
|
|
41
|
+
the unknown `cause`.
|
|
42
|
+
|
|
43
|
+
##### Type Parameters
|
|
44
|
+
|
|
45
|
+
| Type Parameter | Description |
|
|
46
|
+
| ------ | ------ |
|
|
47
|
+
| `V` | the sub-pattern matched against the `Defect` cause. |
|
|
48
|
+
|
|
49
|
+
##### Parameters
|
|
50
|
+
|
|
51
|
+
| Parameter | Type |
|
|
52
|
+
| ------ | ------ |
|
|
53
|
+
| `cause` | `V` |
|
|
54
|
+
|
|
55
|
+
##### Returns
|
|
56
|
+
|
|
57
|
+
`object`
|
|
58
|
+
|
|
59
|
+
| Name | Type | Defined in |
|
|
60
|
+
| ------ | ------ | ------ |
|
|
61
|
+
| `cause` | `V` | [index.ts:61](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/pattern/src/index.ts#L61) |
|
|
62
|
+
| `tag` | `"Defect"` | [index.ts:61](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/pattern/src/index.ts#L61) |
|
|
63
|
+
|
|
64
|
+
***
|
|
65
|
+
|
|
66
|
+
### err()
|
|
67
|
+
|
|
68
|
+
#### Call Signature
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
function err(): object;
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Defined in: [index.ts:47](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/pattern/src/index.ts#L47)
|
|
75
|
+
|
|
76
|
+
A `ts-pattern` pattern matching the `Err` variant of a `Result`. With no
|
|
77
|
+
argument it matches any `Err`; pass a sub-pattern (e.g. [tag](#tag)) to
|
|
78
|
+
constrain or select the `error`.
|
|
79
|
+
|
|
80
|
+
##### Returns
|
|
81
|
+
|
|
82
|
+
`object`
|
|
83
|
+
|
|
84
|
+
| Name | Type | Defined in |
|
|
85
|
+
| ------ | ------ | ------ |
|
|
86
|
+
| `tag` | `"Err"` | [index.ts:47](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/pattern/src/index.ts#L47) |
|
|
87
|
+
|
|
88
|
+
#### Call Signature
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
function err<V>(error): object;
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Defined in: [index.ts:48](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/pattern/src/index.ts#L48)
|
|
95
|
+
|
|
96
|
+
A `ts-pattern` pattern matching the `Err` variant of a `Result`. With no
|
|
97
|
+
argument it matches any `Err`; pass a sub-pattern (e.g. [tag](#tag)) to
|
|
98
|
+
constrain or select the `error`.
|
|
99
|
+
|
|
100
|
+
##### Type Parameters
|
|
101
|
+
|
|
102
|
+
| Type Parameter | Description |
|
|
103
|
+
| ------ | ------ |
|
|
104
|
+
| `V` | the sub-pattern matched against the `Err` error. |
|
|
105
|
+
|
|
106
|
+
##### Parameters
|
|
107
|
+
|
|
108
|
+
| Parameter | Type |
|
|
109
|
+
| ------ | ------ |
|
|
110
|
+
| `error` | `V` |
|
|
111
|
+
|
|
112
|
+
##### Returns
|
|
113
|
+
|
|
114
|
+
`object`
|
|
115
|
+
|
|
116
|
+
| Name | Type | Defined in |
|
|
117
|
+
| ------ | ------ | ------ |
|
|
118
|
+
| `error` | `V` | [index.ts:48](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/pattern/src/index.ts#L48) |
|
|
119
|
+
| `tag` | `"Err"` | [index.ts:48](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/pattern/src/index.ts#L48) |
|
|
120
|
+
|
|
121
|
+
***
|
|
122
|
+
|
|
123
|
+
### ok()
|
|
124
|
+
|
|
125
|
+
#### Call Signature
|
|
126
|
+
|
|
127
|
+
```ts
|
|
128
|
+
function ok(): object;
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Defined in: [index.ts:34](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/pattern/src/index.ts#L34)
|
|
132
|
+
|
|
133
|
+
A `ts-pattern` pattern matching the `Ok` variant of a `Result`. With no
|
|
134
|
+
argument it matches any `Ok`; pass a sub-pattern to constrain or select the
|
|
135
|
+
`value` โ a literal, or any `ts-pattern` pattern (e.g. `ts-pattern`'s own
|
|
136
|
+
`P.string` / `P.select()`, imported from `ts-pattern`, not this package).
|
|
137
|
+
|
|
138
|
+
##### Returns
|
|
139
|
+
|
|
140
|
+
`object`
|
|
141
|
+
|
|
142
|
+
| Name | Type | Defined in |
|
|
143
|
+
| ------ | ------ | ------ |
|
|
144
|
+
| `tag` | `"Ok"` | [index.ts:34](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/pattern/src/index.ts#L34) |
|
|
145
|
+
|
|
146
|
+
#### Call Signature
|
|
147
|
+
|
|
148
|
+
```ts
|
|
149
|
+
function ok<V>(value): object;
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Defined in: [index.ts:35](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/pattern/src/index.ts#L35)
|
|
153
|
+
|
|
154
|
+
A `ts-pattern` pattern matching the `Ok` variant of a `Result`. With no
|
|
155
|
+
argument it matches any `Ok`; pass a sub-pattern to constrain or select the
|
|
156
|
+
`value` โ a literal, or any `ts-pattern` pattern (e.g. `ts-pattern`'s own
|
|
157
|
+
`P.string` / `P.select()`, imported from `ts-pattern`, not this package).
|
|
158
|
+
|
|
159
|
+
##### Type Parameters
|
|
160
|
+
|
|
161
|
+
| Type Parameter | Description |
|
|
162
|
+
| ------ | ------ |
|
|
163
|
+
| `V` | the sub-pattern matched against the `Ok` value. |
|
|
164
|
+
|
|
165
|
+
##### Parameters
|
|
166
|
+
|
|
167
|
+
| Parameter | Type |
|
|
168
|
+
| ------ | ------ |
|
|
169
|
+
| `value` | `V` |
|
|
170
|
+
|
|
171
|
+
##### Returns
|
|
172
|
+
|
|
173
|
+
`object`
|
|
174
|
+
|
|
175
|
+
| Name | Type | Defined in |
|
|
176
|
+
| ------ | ------ | ------ |
|
|
177
|
+
| `tag` | `"Ok"` | [index.ts:35](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/pattern/src/index.ts#L35) |
|
|
178
|
+
| `value` | `V` | [index.ts:35](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/pattern/src/index.ts#L35) |
|
|
179
|
+
|
|
180
|
+
***
|
|
181
|
+
|
|
182
|
+
### tag()
|
|
183
|
+
|
|
184
|
+
```ts
|
|
185
|
+
function tag<Tag>(value): object;
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
Defined in: [index.ts:80](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/pattern/src/index.ts#L80)
|
|
189
|
+
|
|
190
|
+
A `ts-pattern` pattern matching any value whose `_tag` equals `value` (e.g. a
|
|
191
|
+
`TaggedError`). Equivalent to the object pattern `{ _tag: value }`, but reads
|
|
192
|
+
better nested inside an [err](#err) pattern and narrows to the matching
|
|
193
|
+
variant โ including its payload.
|
|
194
|
+
|
|
195
|
+
#### Type Parameters
|
|
196
|
+
|
|
197
|
+
| Type Parameter | Description |
|
|
198
|
+
| ------ | ------ |
|
|
199
|
+
| `Tag` *extends* `string` | the string literal tag to match. |
|
|
200
|
+
|
|
201
|
+
#### Parameters
|
|
202
|
+
|
|
203
|
+
| Parameter | Type | Description |
|
|
204
|
+
| ------ | ------ | ------ |
|
|
205
|
+
| `value` | `Tag` | the `_tag` to match. |
|
|
206
|
+
|
|
207
|
+
#### Returns
|
|
208
|
+
|
|
209
|
+
`object`
|
|
210
|
+
|
|
211
|
+
| Name | Type | Defined in |
|
|
212
|
+
| ------ | ------ | ------ |
|
|
213
|
+
| `_tag` | `Tag` | [index.ts:80](https://github.com/btravstack/unthrown/blob/4553631adb8280ef7c869dfae5bdb13be762095c/packages/pattern/src/index.ts#L80) |
|
|
214
|
+
|
|
215
|
+
#### Example
|
|
216
|
+
|
|
217
|
+
```ts
|
|
218
|
+
.with(P.err(P.tag("Forbidden")), ({ error }) => error.user)
|
|
219
|
+
```
|
package/package.json
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@unthrown/pattern",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "ts-pattern integration for unthrown",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"errors-as-values",
|
|
7
|
+
"pattern-matching",
|
|
8
|
+
"result",
|
|
9
|
+
"ts-pattern",
|
|
10
|
+
"typescript",
|
|
11
|
+
"unthrown"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://github.com/btravstack/unthrown#readme",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/btravstack/unthrown/issues"
|
|
16
|
+
},
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"author": "Benoit TRAVERS <benoit.travers.fr@gmail.com>",
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "https://github.com/btravstack/unthrown.git",
|
|
22
|
+
"directory": "packages/pattern"
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist",
|
|
26
|
+
"docs"
|
|
27
|
+
],
|
|
28
|
+
"type": "module",
|
|
29
|
+
"main": "./dist/index.cjs",
|
|
30
|
+
"module": "./dist/index.mjs",
|
|
31
|
+
"types": "./dist/index.d.mts",
|
|
32
|
+
"exports": {
|
|
33
|
+
".": {
|
|
34
|
+
"import": {
|
|
35
|
+
"types": "./dist/index.d.mts",
|
|
36
|
+
"default": "./dist/index.mjs"
|
|
37
|
+
},
|
|
38
|
+
"require": {
|
|
39
|
+
"types": "./dist/index.d.cts",
|
|
40
|
+
"default": "./dist/index.cjs"
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
"./package.json": "./package.json"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"unthrown": "0.1.0"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@types/node": "24.13.2",
|
|
50
|
+
"@vitest/coverage-v8": "4.1.8",
|
|
51
|
+
"ts-pattern": "5.9.0",
|
|
52
|
+
"tsdown": "0.22.2",
|
|
53
|
+
"typedoc": "0.28.19",
|
|
54
|
+
"typedoc-plugin-markdown": "4.12.0",
|
|
55
|
+
"typescript": "6.0.3",
|
|
56
|
+
"vitest": "4.1.8",
|
|
57
|
+
"@unthrown/tsconfig": "0.1.0",
|
|
58
|
+
"@unthrown/typedoc": "0.1.0"
|
|
59
|
+
},
|
|
60
|
+
"peerDependencies": {
|
|
61
|
+
"ts-pattern": "^5"
|
|
62
|
+
},
|
|
63
|
+
"engines": {
|
|
64
|
+
"node": ">=22.19"
|
|
65
|
+
},
|
|
66
|
+
"scripts": {
|
|
67
|
+
"build": "tsdown src/index.ts --format cjs,esm --dts --clean",
|
|
68
|
+
"build:docs": "typedoc",
|
|
69
|
+
"dev": "tsdown src/index.ts --format cjs,esm --dts --watch",
|
|
70
|
+
"test": "vitest run",
|
|
71
|
+
"typecheck": "tsc --noEmit"
|
|
72
|
+
}
|
|
73
|
+
}
|