matchexpr 0.0.3
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 +67 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +1 -0
- package/package.json +42 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License Copyright (c) 2026 tijn
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of
|
|
4
|
+
charge, to any person obtaining a copy of this software and associated
|
|
5
|
+
documentation files (the "Software"), to deal in the Software without
|
|
6
|
+
restriction, including without limitation the rights to use, copy, modify, merge,
|
|
7
|
+
publish, distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to the
|
|
9
|
+
following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice
|
|
12
|
+
(including the next paragraph) shall be included in all copies or substantial
|
|
13
|
+
portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
|
|
16
|
+
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
17
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
|
|
18
|
+
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
19
|
+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
20
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
<h1>matchexpr</h1>
|
|
2
|
+
|
|
3
|
+
<p>
|
|
4
|
+
typesafe & exhaustive pattern matching for discriminated unions and objects
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
```tsx
|
|
8
|
+
import { match } from 'matchexpr'
|
|
9
|
+
|
|
10
|
+
type Data = { type: 'text', content: string } | { type: 'img', src: string }
|
|
11
|
+
|
|
12
|
+
type Result = { type: 'ok', data: Data } | { type: 'error', error: Error }
|
|
13
|
+
|
|
14
|
+
declare const result: Result
|
|
15
|
+
|
|
16
|
+
const html = match(result, 'type', {
|
|
17
|
+
error: () => <p>Oops! An error occurred</p>,
|
|
18
|
+
ok: ({ data }) =>
|
|
19
|
+
match(data, 'type', {
|
|
20
|
+
text: ({ content }) => <p>{content}</p>,
|
|
21
|
+
img: ({ src }) => <img src={src} />,
|
|
22
|
+
}),
|
|
23
|
+
})
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## features
|
|
27
|
+
|
|
28
|
+
- very small bundle size (less than 200 bytes)
|
|
29
|
+
- concise syntax, no method chaining
|
|
30
|
+
|
|
31
|
+
## usage
|
|
32
|
+
|
|
33
|
+
```sh
|
|
34
|
+
pnpm add matchexpr
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
import { match } from 'matchexpr'
|
|
39
|
+
|
|
40
|
+
declare const color: 'red' | 'green' | 'blue'
|
|
41
|
+
|
|
42
|
+
match(color, {
|
|
43
|
+
red: () => 1,
|
|
44
|
+
green: () => 2,
|
|
45
|
+
blue: () => 3,
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
match(color, {
|
|
49
|
+
red: () => 1,
|
|
50
|
+
green: () => 2,
|
|
51
|
+
// ERROR: Property 'blue' is missing
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
match(color, {
|
|
55
|
+
red: () => 1,
|
|
56
|
+
green: () => 2,
|
|
57
|
+
blue: () => 3,
|
|
58
|
+
yellow: () => 4,
|
|
59
|
+
// ERROR: Property 'yellow' is not a valid case
|
|
60
|
+
})
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## inspired by
|
|
64
|
+
|
|
65
|
+
- [gvergnaud/ts-pattern](https://github.com/gvergnaud/ts-pattern)
|
|
66
|
+
- rust `match`
|
|
67
|
+
- [suchipi/match-discriminated-union](https://github.com/suchipi/match-discriminated-union)
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
type MatchableValue<O, K extends keyof O> = Extract<O[K], PropertyKey>;
|
|
3
|
+
type MatchObjectCases<O extends object, K extends keyof O> = Partial<{ [V in MatchableValue<O, K>]: (obj: Extract<O, Record<K, V>>) => unknown }> & {
|
|
4
|
+
_?: (obj: O) => unknown;
|
|
5
|
+
};
|
|
6
|
+
//#endregion
|
|
7
|
+
//#region src/match.d.ts
|
|
8
|
+
/**
|
|
9
|
+
* typesafe exhaustive matching for discriminated unions and objects
|
|
10
|
+
*
|
|
11
|
+
* @throws {Error} if no pattern matches the value
|
|
12
|
+
*/
|
|
13
|
+
declare function match<TValue extends PropertyKey, THandlers>(value: TValue, handlers: THandlers & { [K in keyof THandlers]: THandlers[K] extends (() => unknown) ? unknown : never } & ('_' extends keyof THandlers ? Record<Exclude<keyof THandlers, TValue | '_'>, never> : Record<Exclude<keyof THandlers, TValue>, never> & Record<Exclude<TValue, keyof THandlers>, never>), _arg2?: never): THandlers[keyof THandlers] extends (() => infer R) ? R : never;
|
|
14
|
+
declare function match<TValue extends object, TKey extends keyof TValue, const THandlers extends MatchObjectCases<TValue, TKey>>(value: TValue, key: TKey, handlers: THandlers & ('_' extends keyof THandlers ? Record<Exclude<keyof THandlers, MatchableValue<TValue, TKey> | '_'>, never> : Record<Exclude<keyof THandlers, MatchableValue<TValue, TKey>>, never> & Record<Exclude<MatchableValue<TValue, TKey>, keyof THandlers>, never>)): { [K in keyof THandlers]: THandlers[K] extends ((...args: any[]) => infer R) ? R : never }[keyof THandlers];
|
|
15
|
+
//#endregion
|
|
16
|
+
export { match };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function e(e,t,n){let r=n!==void 0,[i,a,o]=r?[e[t],n,e]:[e,t],s=Object.hasOwn(a,i)?a[i]:Object.hasOwn(a,`_`)?a._:void 0;if(s===void 0)throw Error(i);return r?s(o):s()}export{e as match};
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "matchexpr",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.3",
|
|
5
|
+
"description": "typesafe exhaustive pattern matching for discriminated unions and objects",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/tijnjh/match.git"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"typescript",
|
|
13
|
+
"pattern-matching",
|
|
14
|
+
"match",
|
|
15
|
+
"exhaustive",
|
|
16
|
+
"discriminated-union"
|
|
17
|
+
],
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"import": "./dist/index.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "tsdown",
|
|
29
|
+
"lint": "eslint",
|
|
30
|
+
"typecheck": "tsc --noEmit",
|
|
31
|
+
"check": "npm run typecheck && npm run lint && npm run build"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@antfu/eslint-config": "^9.0.0",
|
|
35
|
+
"eslint": "^10.3.0",
|
|
36
|
+
"tsdown": "^0.22.3",
|
|
37
|
+
"typescript": "^6.0.3"
|
|
38
|
+
},
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
}
|
|
42
|
+
}
|