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

Change strman.format parameter format #83

Open
ctaleck opened this issue Jul 3, 2018 · 1 comment
Open

Change strman.format parameter format #83

ctaleck opened this issue Jul 3, 2018 · 1 comment

Comments

@ctaleck
Copy link

ctaleck commented Jul 3, 2018

When I use strman.format, I have to wrap the parameters in an Array as stated, but the documentation shows differently:

const select = `SELECT * FROM CONTACTS WHERE NAME LIKE '%{0}%' AND EMAIL LIKE '%{1}%'`
format(select, 'DANIEL', 'GMAIL')
// => `SELECT * FROM CONTACTS WHERE NAME LIKE '%DANIEL%' AND EMAIL LIKE '%GMAIL%'`

In my tests, I have to use:

format(select, ['DANIEL', 'GMAIL'])

I prefer the documented format (without an Array). Is it possible to also support?

@ctaleck
Copy link
Author

ctaleck commented Jul 3, 2018

Solution using the spread operator on params:

var format = (value, ...params) =>
  replace(value, '{(\\w+)}',
    (match, index) => {
      if (typeof params[index] !== 'undefined') {
        return params[index]
      }
      return match
    },
  )

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

1 participant