String.substitute

Edwin Reynoso eorroe at gmail.com
Wed Aug 12 14:55:33 UTC 2015


With `String.substitute()` you'd still be able to evaluate expressions,
it's kind of like changing scope, or passing an object to use for scoped
variables, which is not possible:

```JS
substitute({ year: 2015 }, 'Last year was year ${year-1}'); // Will throw
an error
```

You can't evaluate with your function

With `String.substitute()` you would be able to:

```JS
String.substitute({ year: 2015}, `Last year was ${year-1}`);
```

You also miss **New Lines** which is another thing template literals
solved, so yes you could just add `\n` but that's the point of why template
literals take care of that for you

On Wed, Aug 12, 2015 at 10:49 AM, Elie Rotenberg <elie at rotenberg.io> wrote:

> ```
> function substitute(obj, str) {
>   return str.replace(/\${([^}]*)}/g, (m, p) => obj[p]);
> }
>
> substitute({ year: 2015 }, 'This is year ${year}'); // === 'This is year
> 2015'
> ```
>
>
> On Wed, Aug 12, 2015 at 4:42 PM, Nathaniel Higgins <nat at nath.is> wrote:
>
>> Am I being naive or could this just be written in user space?
>>
>> Sent from my iPhone
>>
>> On 12 Aug 2015, at 15:31, Edwin Reynoso <eorroe at gmail.com> wrote:
>>
>> Yes of course, still requires 1.Destructuring, and making a function for
>> each type of string to return. Defeats the purpose.
>>
>> I'd have to make different functions for each template:
>>
>> ```JS
>> const yearTemplate = ({ year }) => `This year is ${year}`;
>>
>> const ageTemplate = ({ age}) => `I'm ${age} yrs old`;
>> ```
>>
>> Compare to:
>>
>> ```JS
>> let yearSentence = String.substitute({ year: 2015}, `This year is
>> ${year}`);
>> let ageSentence = String.substitute({ age:100 }, `I'm ${age} yrs old`);
>> ```
>>
>> On Wed, Aug 12, 2015 at 10:19 AM, Claude Pache <claude.pache at gmail.com>
>> wrote:
>>
>>>
>>> > Le 12 août 2015 à 15:41, Edwin Reynoso <eorroe at gmail.com> a écrit :
>>> >
>>> > Could we make the following possible, I can't seem to think of a way
>>> to do it, since template literals are evaluated with the current scope,
>>> also tried with `eval` but shouldn't even use that at all:
>>> >
>>> > ```JS
>>> > String.substitute( { year: 2015 }, `This year is ${year}` ); //
>>> Returns "This year is 2015"
>>> > ```
>>> >
>>> > Yes I'm aware I could do the following:
>>> >
>>> > ```JS
>>> > var obj = { year:2015 }
>>> >
>>> > `This year is ${obj.year}`
>>> > ```
>>> >
>>> > The point is to get rid of the part where I reference `obj` all the
>>> time.
>>> >
>>> > I could just use destructuring:
>>> >
>>> > ```JS
>>> > var { year } = { year: 2015 }
>>> >
>>> > `This year is ${year}`
>>> > ```
>>> >
>>> > So yes destructuring takes care of most of this just fine, but the
>>> point is to have a template literal evaluate as a pass reference and not
>>> right away.
>>> >
>>> > You can't make your own function and pass in a template literal that's
>>> not evaluated when passed as a reference, I'm not sure if anyone will
>>> actually want this but me. Please let me know. Thanks
>>>
>>> There is a general trick for deferring evaluation (of a template literal
>>> or of anything else): enclosing it in a function.
>>>
>>> ```js
>>> const myTemplate = ({ year }) => `This year is ${year}`
>>>
>>> myTemplate({ year: 2015 })
>>> ```
>>>
>>> —Claude
>>>
>>>
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>>
>>
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150812/d2b4abe9/attachment.html>


More information about the es-discuss mailing list