signas
v0.2.0
Published
Type-checking function overloading & currying for Javascript inspired by Elixir and Elm
Downloads
5
Readme
Signas
Type-checking function overloading & currying for Javascript inspired by Elixir and Elm
I honestly don't know what the use case is for this, but I wrote it because I like the function clauses of Elixir and the built-in function currying of Elm.
Usage
Pass the thing an array of objects of the shape javascript{args: [Constructor...], handler: <function>}
, and it will return a function that only accepts types with the given constructors.
import f from 'signas'
const add = f([
{args: [Number, Number], handler: (n1, n2) => n1 + n2},
{args: [String, Number], handler: (s, n) => +s + n},
{args: [Number, String], handler: (n, s) => n + +s},
{args: [String, String], handler: (s1, s2) => +s1 + +s2}
])
add(1, 2) // 3
add('1', 2) // 3
add('1', '2') // 3
add(true, '2') // throws an error
const addOne = add(1) // returns a function that adds 1
addOne(2) // 3
addOne('2') // 3
Other things
Take this and use it how you want. It could probably use more tests. Thanks for looking.