Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

RickWong/match-with

Repository files navigation

🧩 match-with

Pattern matching object structures. Semi-related to tc39/proposal-pattern-matching.

Installation

yarn add match-with

Usage

Basic syntax:

import { match } from "match-with";
match(subject: any)
 .with(pattern1: any, callback1: (subject, pattern1) => result: any)
 .with(pattern2: any, callback2: (subject, pattern2) => result: any)
 .default(callback3: (subject) => result: any)
 .result;

Examples:

...
const subject = {
 one: 1,
 two: undefined,
 three: "3",
};
match(subject)
 .with({ one: 1, two: undefined }, () => {
 return "match";
 })
 .result; // "match";
match(subject)
 .with({ one: 2 }, () => {
 // Skipped.
 })
 .default(() => {
 return 1;
 });
match(subject)
 .with({ one: n => n < 2 }, () => {
 console.log(true);
 });
match(subject)
 .with({ four: match.EXISTS }, () => {
 // Skipped.
 })
 .with({ four: undefined }, () => {
 return 4;
 });

Community

Let me know what you think. After you β˜… this project, follow me @Rygu on Twitter.

License

BSD 3-Clause license. Copyright Β© 2019, Rick Wong. All rights reserved.

Releases

No releases published

Packages

No packages published

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /