-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support additional tag property names (#264)
- Loading branch information
1 parent
a43bccb
commit 9f17206
Showing
6 changed files
with
202 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { Alge } from '../src/index.js' | ||
|
||
/** | ||
* You can work with | ||
*/ | ||
|
||
type Fruit = `apple` | `banana` | `orange` | ||
|
||
const fruits = [`apple`, `banana`, `orange`] satisfies [Fruit, ...Fruit[]] | ||
|
||
const fruit = fruits[Math.floor(Math.random() * fruits.length)]! | ||
|
||
const _fruitMatchResult = Alge.match(fruit) | ||
.apple(() => `Do something with apple` as const) | ||
.banana(() => `Do something with banana` as const) | ||
.orange(() => `Do something with orange` as const) | ||
|
||
/** | ||
* You can work with different names for the discriminant property. See docs for all options. Here are a few examples: | ||
*/ | ||
|
||
/** | ||
* Example with `type` | ||
*/ | ||
|
||
type Shape = | ||
| { type: `circle`; radius: number } | ||
| { type: `square`; size: number } | ||
| { type: `rectangle`; width: number; height: number } | ||
|
||
const shapes = [ | ||
{ type: `circle`, radius: 10 }, | ||
{ type: `square`, size: 20 }, | ||
{ type: `rectangle`, width: 30, height: 40 }, | ||
] satisfies [Shape, ...Shape[]] | ||
|
||
const shape = shapes[Math.floor(Math.random() * shapes.length)]! | ||
|
||
const _shapeMatchResult = Alge.match(shape) | ||
.circle(() => `Do something with circle` as const) | ||
.square(() => `Do something with square` as const) | ||
.rectangle(() => `Do something with rectangle` as const) | ||
.done() | ||
|
||
/** | ||
* Example with `kind` | ||
*/ | ||
|
||
type Hero = | ||
| { kind: 'superman' } | ||
| { kind: 'batman' } | ||
| { kind: 'spiderman' } | ||
| { kind: 'wonderWoman' } | ||
| { kind: 'flash' } | ||
|
||
const heroes = [ | ||
{ kind: `superman` }, | ||
{ kind: `batman` }, | ||
{ kind: `spiderman` }, | ||
{ kind: `wonderWoman` }, | ||
{ kind: `flash` }, | ||
] satisfies [Hero, ...Hero[]] | ||
|
||
const hero = heroes[Math.floor(Math.random() * heroes.length)]! | ||
|
||
const _heroMatchResult = Alge.match(hero) | ||
.batman(() => `Do something with batman` as const) | ||
.flash(() => `Do something with flash` as const) | ||
.spiderman(() => `Do something with spiderman` as const) | ||
.superman(() => `Do something with superman` as const) | ||
.wonderWoman(() => `Do something with wonderWoman` as const) | ||
.done() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters