amadsen / trike

A simple try/catch wrapper that returns an error-first array
MIT License
2 stars 1 forks source link

Allow wrapping a function, and all functions in a module #1

Open vjpr opened 6 years ago

vjpr commented 6 years ago
// a. Alternative syntax for immediate invocation.
const [e, msg] = trike(getMessage)(buffer)

// b. Allow wrapping before invocation.
const getMessageT = trike(getMessage)
const [e, msg] = getMessageT(buffer)

// c. Allow wrapping all
const foo = {foo() {}, bar() {}}
const fooT = trike(foo)
const [e, msg] = fooT.foo()

Inspiration from https://github.com/sindresorhus/pify#usage.

amadsen commented 6 years ago

I very much like this idea - particularly the curried / partially applied syntax, but also the object syntax. Counterbalancing this, I'd like to keep this module ridiculously simple. I think it may be best to split this request in two:

  1. support single-argument (curried) syntax in the main module - this is fairly easily accomplished and is very useful.
  2. provide the "trikify" ability to to apply trike to an object full of functions as a distinct module. This avoids potentially tricky type detection (modules that are functions AND have function properties - like express - would be hard) and provides cleaner code splitting for those who don't need the object syntax.