Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
ts-pattern
Advanced tools
ts-pattern is a pattern matching library for TypeScript that allows for expressive and type-safe pattern matching. It provides a way to handle complex data structures and control flow in a concise and readable manner.
Basic Pattern Matching
This feature allows you to match a value against multiple patterns and execute corresponding actions. The 'otherwise' method provides a default case.
const { match } = require('ts-pattern');
const value = 2;
const result = match(value)
.with(1, () => 'one')
.with(2, () => 'two')
.with(3, () => 'three')
.otherwise(() => 'unknown');
console.log(result); // Output: 'two'
Nested Pattern Matching
This feature allows you to match nested objects and structures, making it easier to handle complex data.
const { match } = require('ts-pattern');
const data = { type: 'user', user: { name: 'Alice', age: 30 } };
const result = match(data)
.with({ type: 'user', user: { name: 'Alice' } }, () => 'Hello Alice')
.with({ type: 'user', user: { age: 30 } }, () => 'User is 30 years old')
.otherwise(() => 'Unknown user');
console.log(result); // Output: 'Hello Alice'
Guard Functions
Guard functions allow you to use custom logic to determine if a pattern matches, providing greater flexibility.
const { match, when } = require('ts-pattern');
const value = 10;
const result = match(value)
.with(when(x => x > 5), () => 'greater than 5')
.with(when(x => x <= 5), () => '5 or less')
.otherwise(() => 'unknown');
console.log(result); // Output: 'greater than 5'
match-ts is another TypeScript pattern matching library. It offers similar functionality to ts-pattern but with a different API design. It focuses on providing a more functional programming style.
typescript-pattern-matching is a library that provides pattern matching capabilities for TypeScript. It is similar to ts-pattern but may have different performance characteristics and API design choices.
fp-ts is a library for functional programming in TypeScript. While it is not solely focused on pattern matching, it provides utilities for handling data structures and control flow in a functional style, which can be used to achieve similar outcomes.
A complete pattern matching library for typescript.
FAQs
The exhaustive Pattern Matching library for TypeScript.
The npm package ts-pattern receives a total of 1,338,259 weekly downloads. As such, ts-pattern popularity was classified as popular.
We found that ts-pattern demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.