variator

Prefix based method dispatch

Usage no npm install needed!

<script type="module">
  import variator from 'https://cdn.skypack.dev/variator';
</script>

README

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 quot; + n * 0.59
})
price.define("Apples", function(fruit, pound) {
  var n = pound || 1
  return "Total price for " + fruit + " is quot; + 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 quot; + n * 3.00
})

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

Install

npm install variator