Open renatorib opened 5 years ago
It's an interesting idea for sure. It allows the template literals to become lazy interpolation mechanism, retaining all the semtantics.
1. As the behavior of a tagged template literals is quite different from a common function, it would not be possible to curry it.
I think currying should also be possible. When the function gets called as a template literal tag, it can return function which configuration (currying) can be assumed from the arguments of original function call.
2. The same problem for partial application (I guess).
Same case as 1.)
3. Signature
I'll help with the signature. Shouldn't be that big of a deal.
- Name of the function
I'm not sure the RA.template
is appropriate. I'd image template function to provide some mechanism similar to _.template. This is more about making template strings functional. What do you think ? During writing this comment I wasn't able to come up with better name ;]
Go ahead and create a prototype PR so that we can experiment with the concept.
@renatorib have you been able to come up with something better? I’m putting this function out for somebody to implement.
This is just a suggestion. If it sounds good, let me know. I'd love to be able to create my first pull request here.
The case
I see that composing a string from my data is something that is not very simple yet, despite having quite a few common cases.
This works fine for the case in question, although it is verbose, but it's not very flexible in case I want to build a more complex string.
RA.template
I see there's room for an elegant solution using Tagged Template Literals so we can do things like this:
It works very similar to the styled-components. Each interpolation can act as a function that receives the data, for example:
A dirty implementation (demo purpose) can be seen here.
The problems
RA.template`${...}`(data)