Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ts-pattern

Package Overview
Dependencies
Maintainers
1
Versions
152
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-pattern - npm Package Compare versions

Comparing version 0.1.4 to 0.1.5

index.js

6

package.json
{
"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/"]
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc