2

Thank you for answering my question, I think this is not interpolation so I change the title and before you mark this as duplicate as string interpolation in JavaScript please read the question carefully, because I already read the other interpolation question for JavaScript but not one of them is have the same way that I want for my code (I tell the reason on the question), and I don't want to use Plugin.

Hi all, first I want you to know about my purpose in this code, you can tell this main reason is to build Query Binding for Express with MySQL, but I will use this code for other reason also.

I want to know about string interpolation in Javascript / Typescript that will work like Query Binding in code in Code Igniter source

// Code 1
let person = 'ujang'
let age = 23

console.log("Hello, %s. You're age is %d years old.", person, age)
// Hello, ujang. You're age is 23 years old.

// The function is similiar to this code
// $sql = "insert into tbl_user (name, age, groupname) values (?, ?, ?)";
// $this->db->query($sql,array('codeigniter, 35, 'Group 1'));

As can you see in above code I use console.log and it's working as I want it, but because the console.log is void and not returning any value I can't use it in real condition.

// Code 2
const str = 'helow, %s. and you want %d piece of cake.?'
const name = 'ujang'
const want = 13

const myFunction = (value, ...optionalParams) => {

    // The function I want is similiar with Query Binding in Code Igniter
    // And it can handle dynamicly params
    // This only sample
    value = value.replace('%s', optionalParams[0])
    value = value.replace('%d', optionalParams[1])
    return value
}

myFunction(str, name, want)
// helow, ujang. and you want 13 piece of cake.?

In Code 2 I'll try making a function, that working as expected, but only for static params.

// Code 3
const names = 'ujang'
const arg1 = 'good' 
const argN = 'better'

const dontWantFunction = (value, arg1, argN) => {

    return `helow, ${value}, this function is ${arg1} but any ${argN} solution.?`
}

dontWantFunction(names, arg1, argN)
// helow, ujang, this function is good but any better solution.?

In Code 3 is function that I don't really want, because is hard to manage and have more hardcode text inside the function.

Is anyone know how to fill myFunction in Code 2.?

or anyone working on similar code.?

or know some documentation / article that will lead me to this solution.?

I'am waiting for your response that will help me a lot, Thank you for attention.

Sae
  • 502
  • 1
  • 4
  • 20
  • no, that will look like Code 3 in my question. Thank you for your attention. – Sae Oct 29 '19 at 16:18
  • There are 18 answers on that question. All 18 will look like Code 3? – Heretic Monkey Oct 29 '19 at 16:26
  • oh sorry @HereticMonkey, previously i read only the corrent answer, but yeah some of it use `${}` and 'str' + 'str', and the other using plugin Kiwi and sprintf. that not really what i want for this code. – Sae Oct 29 '19 at 16:33
  • The answer with the check mark is the *accepted* answer, which just means it helped the original asker the most. It does not mean that it is only correct answer, or even correct at all. – Heretic Monkey Oct 29 '19 at 16:36
  • i don't understand what you mean, but from the 18 answer i don't see similar answer that i want, the nearest is the Kiwi plugin and the prototype way, but that not the way i want for my code. and I already have the answer below. but thank you for your attention. – Sae Oct 29 '19 at 16:47

1 Answers1

1

You can try something like this, where we take out the values from optionalParams in sequential manner and replace on matching value

const str = 'helow, {{value}}. and you want {{value}} piece of cake.?'
const name = 'ujang'
const want = 13

const myFunction = (value, ...optionalParams) => {
  return value.replace(/\{\{value\}\}/g, (m) => optionalParams.shift() || m)
}

console.log(myFunction(str, name, want))
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
  • Well. thank you so much for your fast response @CodeManiac. can you costum {{value}} for more shorter than {{v}} .? – Sae Oct 29 '19 at 16:10
  • @Sae yes you can use whatever you want instead of `{{value}}`, you can replace `{{value}}` by `{{v}}` and in replace change the pattern to `\{\{v\}\}` – Code Maniac Oct 29 '19 at 16:11
  • 1
    I got it, change to {{value}} with ? and regex with /\?/. – Sae Oct 29 '19 at 16:13