GithubHelp home page GithubHelp logo

rickwong / match-with Goto Github PK

View Code? Open in Web Editor NEW
4.0 3.0 0.0 386 KB

🧩 Pattern matching objects

License: BSD 3-Clause "New" or "Revised" License

JavaScript 100.00%
pattern matching object structures truthy fasly exists match deep compare

match-with's Introduction

🧩 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.

match-with's People

Contributors

rickwong avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

match-with's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.