Socket
Socket
Sign inDemoInstall

ts-pattern

Package Overview
Dependencies
Maintainers
1
Versions
151
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.2.2 to 0.3.0

2

lib/index.js

@@ -80,3 +80,3 @@ "use strict";

},
]));
])).run();
},

@@ -83,0 +83,0 @@ run: function () {

@@ -66,3 +66,3 @@ import { Pattern, SelectPattern, GuardValue } from './Pattern';

**/
otherwise: <c>(handler: () => PickReturnValue<b, c>) => Match<a, PickReturnValue<b, c>>;
otherwise: <c>(handler: () => PickReturnValue<b, c>) => PickReturnValue<b, c>;
/**

@@ -69,0 +69,0 @@ * ### Match.run

{
"name": "ts-pattern",
"version": "0.2.2",
"version": "0.3.0",
"description": "Typescript pattern matching library",

@@ -20,3 +20,3 @@ "main": "lib/index.js",

"type": "git",
"url": "git+ssh://git@github.com/gvergnaud/match.git"
"url": "git+ssh://git@github.com/gvergnaud/ts-pattern.git"
},

@@ -36,5 +36,5 @@ "keywords": [

"bugs": {
"url": "https://github.com/gvergnaud/match/issues"
"url": "https://github.com/gvergnaud/ts-pattern/issues"
},
"homepage": "https://github.com/gvergnaud/match#readme",
"homepage": "https://github.com/gvergnaud/ts-pattern#readme",
"devDependencies": {

@@ -41,0 +41,0 @@ "@types/jest": "^25.2.3",

@@ -1,10 +0,27 @@

# Typescript Pattern
# TS Pattern
A complete pattern matching library for typescript.
The Pattern Matching library for TypeScript you have been missing.
### Example
## What is Pattern Matching?
Pattern Matching is a technique coming from Functional Programming languages to declaratively write conditional code branches based on the structure of one or several values. It is a well proven technique much more powerful and much less verbose than imperative alternatives (if/else/switch statements) especially when branching on several values.
Pattern Matching is implemented in Elixir, Rust, Haskell, Swift and many other languages. There is [a tc39 proposal](https://github.com/tc39/proposal-pattern-matching) to add Pattern Matching to the EcmaScript specification, but it is still in stage 1 and isn't likely to land before several years (if ever). Lukily, pattern matching can be implemented in userland. `ts-pattern` Provides a typesafe pattern matching implementation that you can start using today.
## Features
- Supports every data structure you use: objects, arrays, tuples, Sets, Maps, and all primitive types.
- Typesafe, with great type inference.
- Catch all (`__`) and type specific wild cards support.
- Supports `when(predicate)` and `not(pattern)` patterns for complexe cases.
- Supports properties selection, via the `select(name)` function.
- Tiny bundle footprint (1kb).
## Gist
Sometimes you want to match on two values at once. Here we pattern match
on both the current state and the event (or action) and return a new state.
```ts
type State =
| { status: 'idle' }
| { status: 'loading' }

@@ -17,4 +34,3 @@ | { status: 'success'; data: string }

| { type: 'success'; data: string }
| { type: 'error'; error: Error }
| { type: 'cancel' };
| { type: 'error'; error: Error };
```

@@ -30,5 +46,3 @@

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])

@@ -49,3 +63,2 @@ // the first argument is the pattern : the shape of the value

}))
.with([{ status: 'loading' }, { type: 'cancel' }], () => initState)
// if you need to exclude a value, you can use

@@ -60,8 +73,33 @@ // a `not` pattern. it's a function taking a pattern

.with(__, () => state)
// You can also use `otherwise`, which is equivalent to `with(__)`.
.otherwise(() => state)
// `run` execute the match close, and returns the value
.run();
// .run();
// You can also use `otherwise`, which take an handler returning
// a default value. It is equivalent to `with(__, handler).run()`.
.otherwise(() => state);
```
## Code Sandbox Examples
- Simple example
- [Reducer Demo (in React)](https://codesandbox.io/s/ts-pattern-reducer-example-c4yuq?file=/src/App.tsx)
- [`when` guards Demo](https://codesandbox.io/s/ts-pattern-when-guard-example-0s6d8?file=/src/index.ts)
- Polymorphic input
- Untyped input (e.g. an API response)
- `not` patterns
- `select` pattern
## Documentation
- Installation
- Patterns
- Literals
- Objects and arrays
- Sets and Maps
- `__` and other wild cards
- `when` guards
- `not` patterns
- `select` pattern
## Pattern matching
### type inference

@@ -68,0 +106,0 @@

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