npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

match-case

v0.1.3

Published

Functional pattern matcher

Downloads

13

Readme

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