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.