
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Rematch is a pattern matching library built for JavaScript and TypeScript. Pattern matching helps you write safer, more readable, and more concise code.
npm install rematch
Or download a release and pick up lib/rematch.js
.
function dangerLevel(mutant) {
return Rematch(mutant, [
Rematch.Type(Villain, () => 5),
Rematch.Value(Mutants.Flash, () => 0),
Rematch.Value(Mutants.Superman, () => 2)
]);
};
console.log([
dangerLevel(Mutants.Flash), // 0
dangerLevel(Mutants.Superman), // 2
dangerLevel(Mutants.Joker) // 5
])
console.log(dangerLevel(Mutants.Batman)) // Error: Rematch.MatchError
Play with it live on RunKit.
The Rematch
function takes in an argument and a group of cases to test the argument against.
There are 4 types of cases:
If no cases are valid, a Rematch.MatchError
is thrown. There are no 'fall-throughs' like in switch statements.
By default, equality checks use lodash.isEqual
, which does a structural comparison. You can override this by setting
Rematch.isEqual
to a custom value.
For the large majority of code that isn't performance-sensitive, there are a lot of great reasons why you'd want to use pattern matching over if/else:
Let's do an example! We're building a webapp, and we need to authenticate our users and update them on their status. Here's a straightforward solution:
if(user instanceof BlacklistedUser) {
warnBlacklistMonitor();
return;
}
else if(user.password === enteredPassword) {
login();
alert("You're logged in!");
}
else {
onUserFailedLogin();
alert("Mistyped your password? Try again or do a password reset.");
}
This code works. Let's see how a pattern matching solution stacks up:
Rematch(user, [
Rematch.Type(BlacklistedUser, () => warnBlacklistMonitor()),
Rematch.Else(() => {
var statusMessage = Rematch(enteredPassword, [
Rematch.Value(user.password, () => {
login();
return "You're logged in!";
}),
Rematch.Else(() => {
onUserFailedLogin();
return "Entered password is invalid";
})
]);
alert(statusMessage);
})
])
It's immediately clear that there are 3 return points, and that 2 of them are dependent on the other one.
We've identified a common variable statusMessage
, which'll make debugging / testing easier down the line.
And lastly, all the return points consistently return nothing.
FAQs
Pattern matching for JavaScript/TypeScript
The npm package rematch receives a total of 0 weekly downloads. As such, rematch popularity was classified as not popular.
We found that rematch demonstrated a not healthy version release cadence and project activity because the last version was released 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
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.