ts-pattern
Advanced tools
Comparing version 0.1.4 to 0.1.5
{ | ||
"name": "ts-pattern", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"description": "Typescript pattern matching library", | ||
"main": "lib/index.js", | ||
"main": "index.js", | ||
"scripts": { | ||
"clean": "rimraf lib", | ||
"clean": "rimraf index.js index.d.ts", | ||
"build": "tsc -d", | ||
@@ -9,0 +9,0 @@ "prepare": "npm run test && npm run clean && npm run build", |
@@ -5,5 +5,63 @@ # Typescript Pattern | ||
### Example | ||
```ts | ||
import { match, __, when, not } from 'pattern'; | ||
type State = | ||
| { status: 'idle' } | ||
| { status: 'loading' } | ||
| { status: 'success'; data: string } | ||
| { status: 'error'; error: Error }; | ||
type Event = | ||
| { type: 'fetch' } | ||
| { type: 'success'; data: string } | ||
| { type: 'error'; error: Error } | ||
| { type: 'cancel' }; | ||
``` | ||
```ts | ||
import { match, __, not } from 'ts-pattern'; | ||
const initState: State = { | ||
status: 'idle', | ||
}; | ||
const reducer = (state: State, event: Event): State => | ||
// Sometimes you want to match on two values at once. | ||
// Here we pattern match both on the state and the event | ||
// and return a new state. | ||
match<[State, Event], State>([state, event]) | ||
// the first argument is the pattern : the shape of the value | ||
// you expect for this branch. | ||
.with([{ status: 'loading' }, { type: 'success' }], ([, event]) => ({ | ||
status: 'success', | ||
data: event.data, | ||
})) | ||
// The second argument is the function that will be called if | ||
// the data matches the given pattern. | ||
// The type of the data structure is narrowed down to | ||
// what is permitted by the pattern. | ||
.with([{ status: 'loading' }, { type: 'error' }], ([, event]) => ({ | ||
status: 'error', | ||
error: event.error, | ||
})) | ||
.with([{ status: 'loading' }, { type: 'cancel' }], () => initState) | ||
// if you need to exclude a value, you can use | ||
// a `not` pattern. it's a function taking a pattern | ||
// and returning its opposite. | ||
.with([{ status: not('loading') }, { type: 'fetch' }], () => ({ | ||
status: 'loading', | ||
})) | ||
// `__` is a wildcard, it will match any value. | ||
// You can use it at the top level, or inside a data structure. | ||
.with(__, () => state) | ||
// You can also use `otherwise`, which is equivalent to `with(__)`. | ||
.otherwise(() => state) | ||
// `run` execute the match close, and returns the value | ||
.run(); | ||
``` | ||
### type inference | ||
```ts | ||
type Input = { type: string } | string; | ||
@@ -10,0 +68,0 @@ |
@@ -6,5 +6,5 @@ { | ||
"downlevelIteration": true, | ||
"outDir": "lib/" | ||
"outDir": "./" | ||
}, | ||
"exclude": ["tests/"] | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
88
47817
10
1318