Giter VIP home page Giter VIP logo

match-case's Introduction

Functional Matcher for JavaScript

npm version Dependency Status Build Status Coverage Status

Match-case is npm package that add functional pattern matcher.

Install

npm install match-case

Basic Usage

In case of ECMAScript6

import match from 'match-case'

// two function args
const result1 = match(10).
  caseOf(n => n > 0, v => v * v).
  caseOf(n => n < 0, v => v + v).
  caseOfElse(404).
end()

assert(result1 === 100)

// one object arg with function
const result2 = match(-1).
  caseOf({
    when: n => n > 0,
    then: v => v * v
   }).
  caseOfElse({
    then: v => 404
  }).
get()

assert(result2 === 404)

// equal value
const result3 = match(2).
  caseOf(1, 10).
  caseOf(2, 20).
  caseOf(n => n > 0, 30).
  caseOfElse(404).
end()

assert(result3 === 20) // first match case

// match after case construction
const matcher = match().
  caseOf(n => n > 0, 200).
  caseOfNone(404).
  caseOfElse(500)

assert(matcher.get(10) === 200)
assert(matcher.get(-1) === 404)
assert(matcher.get(null) === 500)
assert(matcher.get(undefined) === 500)

In case of ECMAScript5

var match = require('match-case').default

var result = match(10).
  caseOf({
    when: function(n) {return n > 0},
    then: function(v) {return v * v}
  }).
  caseOfElse(404).
end()

assert(result === 100)

Practical Usage

Fizz-Buzz example in case of TypeScript(ES6)

///<reference path='./node_modules/match-case/lib/index.d.ts'/>
import match from 'match-case'
import {List} from 'immutable'

const result = List.of<any>(
  0, 1, "2", 3, ()=>4, {num:5}, new Date(), 7, undefined, 8,
  "9", 10, "Hello", 11, "12", null, 13, 14, "World", 15, 16
).
map<number>(
  v => match<number>(v).
  caseOfNone(0). // prevent runtime error
  caseOf({
    when: n => typeof(n) === "number",
    then: v => v
  }).
  caseOf({
    when: n => typeof(n) === "string" && /^[0-9]+$/.test(n),
    then: v => parseInt(v)
  }).
  caseOf({
    when: n => typeof(n) === "function",
    then: v => v()
  }).
  caseOf({
    when: n => !isNaN(n.num),
    then: v => parseInt(v.num)
  }).
  caseOf({
    when: n => n instanceof Date,
    then: 6
  }).
  end()
 ).
filter(n => (1 <= n&&n <= 15)).
map<string>(
  v => match<number,string>(v).
  caseOf({
    when: n => n%3===0 && n%5===0,
    then: "FizzBuzz"
  }).
  caseOf({
    when: n => n%3===0,
    then: "Fizz"
  }).
  caseOf({
    when: n => n%5===0,
    then: "Buzz"
  }).
  caseOfElse({
    then: v => v.toString()
  }).
  end()
).
join(" ")

assert(
  result === "1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz"
)

More Usage

Please see API spec files.

Development

Get ready

npm install

Build

npm run build

Test

npm test

Test watch

npm run test:watch

License

MIT

match-case's People

Contributors

namikingsoft avatar

Stargazers

Brad Pillow avatar

Watchers

James Cloos avatar  avatar  avatar

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.