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

Ignore slots without value provided #24

Open
ericzon opened this issue Nov 2, 2019 · 2 comments
Open

Ignore slots without value provided #24

ericzon opened this issue Nov 2, 2019 · 2 comments

Comments

@ericzon
Copy link

ericzon commented Nov 2, 2019

First of all, thank you for your lib :)

I'm experiencing a possible bug. If I try this example:

const replaceTemplate = require('string-template');

const input = "https://{user}:{password}@{domain}/api/endpoint1?parameter1={parameterValue1}";
const replacements = {
  user: 'Eric',
  password: 'a pass',
  domain: 'any-url.com'
};

const test = replaceTemplate(input, replacements);

The result is like this:

"https://Eric:a [email protected]/api/endpoint1?parameter1=";

parameterValue1 is lost. I should expect an output like this:

"https://Eric:a [email protected]/api/endpoint1?parameter1={parameterValue1}";

To put you in context, I'm developing an API with Loopback 4 using datasource templating and they use {} as delimiters. My idea was to join their bindings with my own thanks to your lib.

Maybe it could also be related with #12 because with custom delimiters, this error could be work-arounded.

@ericzon ericzon changed the title Ignore slots without provided value Ignore slots without value provided Nov 2, 2019
@akashmugu
Copy link

yes. it makes more sense to just substitute passed variables and skip substituting unavailable variables

current code replaces all substitution points with variables available (like user, password, domain) and unavailable (like parameterValue1) with ""

instead, it needs to iterate passed variables and then substitute them one by one. something like this:

const format = (template, vars) =>
  Object.entries(vars).reduce(
    (acc, [key, val]) => acc.replace(`{${key}}`, val),
    template
  )

@Matt-Esch let me know if it makes sense to you too. i'll happily make a PR

@Matt-Esch
Copy link
Owner

Matt-Esch commented Jan 18, 2023

I think the code could be modified to do this but it would be a breaking change if not flagged. The module was designed for use where the named values are supplied every time and this was a performance tradeoff.

I will also mention that your example is dangerous and not a good use case for string template as these values need to be escaped properly to form a valid URL, otherwise it is subject to url rewriting though setting a malicious user variable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants