Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

pamach

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pamach

Pattern matching utility.

  • 0.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Pamach

Function to match against stuff.

Inspired by:

  • Replacing switch statements with Object literals
  • Rust match syntax

Load the thing:

const match = require('pamach');

Pass some args and spec obj:

const drink = 'coke';

const myDrink = match(drink)({
  coke:    () => { return 'Pssshhh...'; },
  wine:    () => { return 'I like this harvest from summer 1901...'; },
  beer:    () => { return 'After a long day...' },
  default: () => { return 'H20'; },
});

console.log(myDrink); // -> 'Pssshhh...'

Multiple arguments:

const firstComponent = 'coke';
const secondComponent = 'vodka';

const cocktail = match(firstComponent, secondComponent)({
  'coke, vodka': () => { return 'Much better together'; },
  'beer, vodka': () => { return 'Are you sure this is a good idea?'; },
  default:       () => { return 'H20'; }    
});

console.log(cocktail); // -> 'Much better together'

Accepts anything that implements .toString:

const person = {
  firstName: 'Steve',
  lastName: 'Jobs',
  toString: function() { return `${this.firstName} ${this.lastName}` }
};

const collegue = match(person)({
  'Steve Jobs': () => {
    return { firstName: 'Jonny', lastName: 'Ive' }
  },
  'Fox Malder': () => {
    return { firstName: 'Dana', lastName: 'Scully' }
  }
});

console.log(collegue); // -> { firstName: 'Jonny', lastName: 'Ive' }

API:

match([args]) -> Function

Takes any number of args and returns new matcher function

matcher(spec) -> Any

that takes spec object to map args separated by ', ' (coma + space) as key to corresponding function, so value returned by this function becomes the return value of matcher.

If no key found for given set of args, matcher tries to find default key in the spec object and call its value.

If no default key found, undefined is returned.

License - MIT

Keywords

FAQs

Package last updated on 03 Apr 2016

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc