Deschtex / html2react

A utility for turning raw HTML into React elements.
MIT License
43 stars 5 forks source link

What do you think of having a "curried" API? #10

Closed xaviervia closed 8 years ago

xaviervia commented 8 years ago

It could look like this:

const reactifier = html2react({ a: Link })

const reactComponents = reactifier(content)

for the regular API it would imply only reverting the order of the arguments:

const reactComponents = html2react({
  a: Link
}, content)

This way it's easy to provide a common wrapping when makes sense to use the same setup in multiple places.

Deschtex commented 8 years ago

Interesting... I'm curious about what the implementation would look like, as there would be three different signatures to support:

  1. Content only: HTML2React(content)
  2. Overrides and content: HTML2React({ a: Link }, content)
  3. Curried overrides and then content: HTML2React({ a: Link })(content)

Any suggestion?

xaviervia commented 8 years ago

As I see it, internally it would always be partially applied and take content from a passed content or from a default, and then the externally exposed one decides according to the arguments. So in pseudo JS it would be something like:


const defaultElementOverrides = { a: <a> , ... }

const html2react = (elementOverrides = defaultElementOverrides) => (content) => {
  // do stuff
}

export default function (...args) {
  if (args.length == 2) {
    const elementOverrides = args[0]
    const content = args[1]
    return html2react(elementOverrides)(content)
  } else if (typeof args[0] === 'string') { // called with only content
    return html2react()(args[0])
  } else { // called with only elementOverrides returns partially applied
    return html2react(args[1])
  }
}
Deschtex commented 8 years ago

Cool, I like it. I would prefer keeping the content as the first argument, though, so that it's not a breaking update. The only change would be to switch the arguments to html2react in the if (args.length === 2) { ... } block. What do you think?

xaviervia commented 8 years ago

Sure!