Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RA.template: compound strings with tagged template literals #975

Open
renatorib opened this issue Jul 10, 2019 · 2 comments
Open

RA.template: compound strings with tagged template literals #975

renatorib opened this issue Jul 10, 2019 · 2 comments

Comments

@renatorib
Copy link

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.

const data = {
  profile: {
    firstName: "Renato",
    lastName: "Ribeiro"
  }
}

R.pipe(
  R.prop('profile'),
  R.pick(['firstName', 'lastName']),
  R.values,
  R.join(' '),
)(data) // => "Renato Ribeiro"

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.

const data = {
  address: {
    street: "Hursley Park",
    city: "Winchester",
    postCode: "SO21 2JN"
  }
}

R.pipe(
  R.prop('address'),
  address => `${address.street}, ${address.city} - (${address.postCode})`
)(data) // => "Hursley Park, Winchester - (SO21 2JN)"

RA.template

I see there's room for an elegant solution using Tagged Template Literals so we can do things like this:

const data = {
  address: {
    street: "Hursley Park",
    city: "Winchester",
    postCode: "SO21 2JN"
  }
}

R.pipe(
  R.prop('address'),
  RA.template`${R.prop('street')}, ${R.prop('city')} - (${R.prop('postCode')})`
)(data) // => "Hursley Park, Winchester - (SO21 2JN)"
const data = {
  profile: {
    firstName: "Renato",
    lastName: "Ribeiro"
  }
}

R.pipe(
  R.prop('profile'),
  RA.template`${R.prop('firstName')} ${R.prop('lastName')}`
)(data) // => "Renato Ribeiro"

It works very similar to the styled-components. Each interpolation can act as a function that receives the data, for example:

R.template`${data => ...} some text ${data => ...}`(data)
// if interpolation value isn't a function, it is used as raw value
const greet = "Hello"
const yellGreet = RA.template`${greet}, ${R.toUpper}` 
yellGreet('Renato') // => "Hello, RENATO"

const formatFullName = RA.template`${R.prop('firstName')} ${R.prop('lastName')}`
formatFullName({ firstName: "Renato", lastName: "Ribeiro" })

const strong = RA.template`<strong>${R.identity}</strong>`
strong('More bold than ever') // => "<strong>More bold than ever</strong>"

A dirty implementation (demo purpose) can be seen here.

The problems

  1. As the behavior of a tagged template literals is quite different from a common function, it would not be possible to curry it. But it works as well:
    RA.template`${...}`(data)
  2. The same problem for partial application (I guess).
  3. Signature?
  4. ?

If it sounds good, let me know. I'd love to be able to create my first pull request here.

@char0n
Copy link
Owner

char0n commented Jul 13, 2019

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.

  1. 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.

@char0n
Copy link
Owner

char0n commented Dec 24, 2019

@renatorib have you been able to come up with something better? I’m putting this function out for somebody to implement.

@char0n char0n added the Hacktoberfest Hacktoberfest 2020 label Sep 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants