Polyfun allows you to write functions which supports single dispatch on type. It means you can write polymorphic functions or extend third-party functions for specific types.
var f = fun()
Add support for specific type:
f.define(Number, function() {
return 'number'
})
f(12) // => 'number'
f("12") // => throw new TypeError("Type is not supported")
Type dispatching works on:
- null
- undefined
- native types
- custom types
f.supportedTypes() // => {Number: fun...}
f.isSupported(String)
var indentity = fun(function(val) {
return val;
})
identity(1) // => 1
f.hasDefault()