string-template icon indicating copy to clipboard operation
string-template copied to clipboard

Properties with underscore not working in compile mode

Open ksmithut opened this issue 8 years ago • 1 comments

Using version 1.0.0 on node v6.9.4:

const format = require("string-template")
const compile = require("string-template/compile")

const vars = {
  hostName: 'my.only.host.com',
  host_tenant: 'only',
  host_lane: 'prd',
  subdomain: 'my.only',
}

console.log(format('http://{hostName}-{host_tenant}-{host_lane}-{subdomain}', vars))
// Logs out "http://my.only.host.com-only-prd-my.only"

const template = compile('http://{hostName}-{host_tenant}-{host_lane}-{subdomain}')
console.log(template(vars))
// Logs out "http://my.only.host.com-{host_tenant}-{host_lane}-my.only"

Can be reproduced here: https://runkit.com/58827adb4ddfcf0014890d06/58827adb4ddfcf0014890d07

ksmithut avatar Jan 20 '17 21:01 ksmithut

I believe this is fixed by changing this line: https://github.com/Matt-Esch/string-template/blob/master/compile.js#L4 to allow an _ in its charset, but you may want to support other valid characters in variable names such as $

ksmithut avatar Jan 20 '17 21:01 ksmithut