Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

44 lines (30 sloc) 1.172 kb

variator

Build Status

Prefix based method dispatch is an extendable alternative to imperative switch statements.

Usage

var variator = require("variator")
var price = variator("price")

price.define("Oranges", function(fruit, pound) {
  var n = pound || 1
  return "Total price for " + fruit + " is $" + n * 0.59
})
price.define("Apples", function(fruit, pound) {
  var n = pound || 1
  return "Total price for " + fruit + " is $" + n * 0.32
})

price("Oranges")      // => "Total price for Oranges is $1"
price("Apples", 5)    // => "Total price for Apples is $1.6"

// Errors are thrown for unknown cases:
price("Cherries", 2)  // => TypeError: Method price#0hq76f not implemented for: Cherries

// Although new cases can be defined
price.define("Cherries", function(fruit, pound) {
  var n = pound || 1
  return "Total price for " + fruit + " is $" + n * 3.00
})

price("Cherries", 2)  // => "Total price for Cherries is $6"

Install

npm install variator
Jump to Line
Something went wrong with that request. Please try again.