// Import relevant functions:
import {interpolateFactory, addSymbolicStringExtensions} from "../Bundle/index.min.js";
// ↑ ↑ function to add symbolic extensions to String.prototype
// ↑ the interpolateFactory to create an interpolate function
// create an interpolator function with a default replacement value
// (replaces tokens with empty values with the default replacement value)
const splatES = interpolateFactory("¡no value!");
// ↑ missing values are replaced with this
const templateStringEx = " hello {wrld} {univrs}\n";
// use splatES function
splatES(
templateStringEx,
{wrld: "WORLD", univrs: null},
{wrld: null, univrs: "UNIVERSE"},
{wrld: "WORLD", univrs: "AND UNIVERSE"} );
/* result ↓
hello WORLD ¡no value!
hello ¡no value! UNIVERSE
hello WORLD AND UNIVERSE */
// On importing interpolateFactory String.prototype
// was extended using 2 Symbols. Here we
// assign them to variables.
const [splat, splat$] = addSymbolicStringExtensions();
// now we can use the 'symbolic extensions' (named splat/splat$)
// [splat]: keep tokens with empty values intact
templateStringEx[splat](
{wrld: "WORLD"},
{wrld: "WORLD", univrs: null},
{wrld: null, univrs: "UNIVERSE"},
{wrld: "WORLD", univrs: "AND UNIVERSE"} );
/* result ↓
hello WORLD (Note: missing tokens are ignored) {univrs}
hello WORLD {univrs}
hello {wrld} UNIVERSE
hello WORLD AND UNIVERSE */
// [splat$]: remove tokens without values (replace with empty string)
templateStringEx[splat$](
{univrs: "UNIVERSE"},
{wrld: "WORLD", univrs: null},
{wrld: null, univrs: "UNIVERSE"},
{wrld: "WORLD", univrs: "AND UNIVERSE"} );
/* result ↓
hello UNIVERSE (Note: missing tokens are cleared)
hello WORLD
hello UNIVERSE
hello WORLD AND UNIVERSE */
// escaped "{" and/or "}" ("\{", "\}") and non existing token values are ignored
const replacement = { blah: "FOOBLAH", bar: "BARRED" };
const someStr = "Blah [{blah}] and blah and {foo}, but then again [\{bar\} | {bar}]";
// ↑ ↑ escaped/ignored
// ↑ not in [replacement]/ignored
someStr[splat](replacement); // => "Blah [FOOBLAH] and blah and {foo}, but then again [\{bar\} | BARRED]"
someStr[splat$](replacement); // => "Blah [FOOBLAH] and blah and | BARRED]"
// the templates for the following tables
// table
const tableTemplate = `
<table>
<caption>{caption}</caption>
<thead>
<tr>
<th>#</th>
<th>prename</th>
<th>surname</th>
</tr>
</thead>
<tbody>{rows}</tbody>
</table>`;
// row
const tableRowTemplate = `
<tr>
<td>{index}</td>
<td>{pre}</td>
<td>{last}</td>
</tr>`
/* ↑ Note:
adding {index} in the template string is optional.
It will be automa(t/g)ically replaced with the index
(starting with 1) of the splat insertion */;
/* the tokens used for tableRowTemplate */
const theNames = [
{pre: "Mary", last: "Peterson"},
{pre: "Muhammed", last: "Ali"},
{pre: "Missy", last: "Johnson"},
{pre: "Hillary", last: "Clinton"},
{pre: "Foo", last: "Bar"},
{pre: "Bar", last: "Foo"},
{pre: "小平", last: "邓"},
{pre: "Володимир", last: "Зеленський"},
{pre: "zero (0)", last: 0},
{pre: "Row", last: 10},
// ignored/cleared values ↓
{pre: "replacement-is-empty-string", last: ''}, // ᐊ Empty string value IS replaced
{pre: "replacement-Is-array", last: [1, 2, 3]}, // ᐊ Array value IS NOT replaced/
{pre: "replacement-Is-null", last: null}, // ᐊ null value IS NOT replaced
{pre: "replacement-Is-object", last: {}}, // ᐊ Object value IS NOT replaced
{pre: "replacement-Is-undefined", last: undefined}, // ᐊ undefined value IS NOT replaced
{last: "key-pre-does-not-exist"}, // ᐊ undefined value IS NOT replaced
{pre: "key-last-does-not-exist"}, // ᐊ incomplete object, what exists is replaced
{some: "nothing-to-replace", name: "nothing"}, // ᐊ non existing keys, tokens ignored
{}, // ᐊ empty object, tokens ignored
["nothing", "nada", "zip", "没有什么"] // ᐊ replacement not an Object, tokens ignored
];
// table creation
const table1 = tableTemplate[splat]({
caption: `tableRowTemplate[splat] using theNames`,
rows: tableRowTemplate[splat](...theNames)
});
const table2 = tableTemplate[splat$]({
caption: `tableRowTemplate[splat$] (empty/invalid values => empty string)`,
rows: tableRowTemplate[splat$](...theNamesClear)
});
// to fill the next table, a single object with array values is used
const theNamesTokensAsArrays = {
pre: [ "Mary", "Muhammed", "Missy", "Hillary", "Foo", "Bar",
"小平", "Володимир", "zero (0)", "Row", null, "missing value" ],
last: [ "Peterson", "Ali", "Johnson", "Clinton", "Bar", "Foo",
"邓", "Зеленський", "0", "10", "missing value", null ]
};
// table creation
tableTemplate[splat]({
caption: `tableRowTemplate[splat] token values are arrays`,
rows: tableRowTemplate[splat](theNamesTokensAsArrays)
});
{backLink}
The module exports
- the factory itself
interpolateFactory),
- the
interpolate function (default),
- the
interpolateClear function (which clears empty placeholders) and
- the
addSymbolicStringExtensions) function to extend
String.prototype with symbolic splatting methods.
Syntax by example