String interpolation in Dojo
by Deepak Anand
HTML
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/dojo/1.8/dijit/themes/claro/claro.css">
JavaScript
require(["dojo/string"], function(string) {
// var updatedL10n = string.substitute("- ${name} output with ${type} data type is not supported!", {"name": "foo", "type": "bar"});
var updatedL10n = subs("- ${0} output with ${1} data type is not supported!", ["foo","bar"]);
console.log(updatedL10n)
});
(()=>{
var p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';
var regex = /dog/gi;
p.replace(regex, function(match, key, format) {
// console.log(key)
});
})();
function interpolate(literals, ...substitutions) {
let interpolation = '';
// loop through based on length of substitutions
// since its shorter by 1
for (let i = 0; i < substitutions.length; i++) {
interpolation += literals[i] + substitutions[i];
}
// add the extra literal to the end
interpolation += literals[literals.length - 1];
return interpolation;
}
let firstName = 'Dee',
lastName = `pak`;
// output: Name: Ilegbodu, Ben
// console.log(interpolate`Name: ${lastName}, ${firstName}`);
function subs(template, map) {
return template.replace(/\$\{([^\s]*)?\}/g,
function(match, key, format){
return map[key];
}); // String
}