YAML, extended to support inline variables.
Replacing strings is now as simple as fill a hole.
Template string, files, and even YAML.
I love YAML. In many ways. But sometimes, we need "just a little bit more". Tampax is the more, by providing a way to parse recursively the internal structure of YAML to use variables inside of it.
If you don't know how YAML works, you can read this introduction to YAML.
npm install tampax --save
This example show how you can use both object-related variables included in your YAML structure and give an object to feed your object. This is optional to give an object to this function.
const tampax = require('../index.js');
const yamlString = `
dude:
name: Arthur
weapon:
favorite: Excalibur
useless: knife
sentence: "{{dude.name}} use {{weapon.favorite}}. The goal is {{goal}}."`;
tampax.yamlParseString(yamlString, {goal: 'to kill Mordred'}, (err, data) => {
if (err) {
throw err;
}
console.log(data);
/* => { dude: { name: 'Arthur' },
weapon: { favorite: 'Excalibur', useless: 'knife' },
sentence: 'Arthur use Excalibur. The goal is to kill Mordred.' }
*/
});
There is 2 ways to do this.
First, by passing an object with the equivalent name :
let tampax = require("tampax")
let result
// Format using an object hash with keys matching [0-9a-zA-Z.]+
result = tampax("Hello {{name}}, you have {{count}} unread messages", {
name: "Robert",
count: 12
})
console.log(result);
// result -> "Hello Robert, you have 12 unread messages"
Or by using an old-fashion-way to do, by passing an array, and using numbers.
// Format using a number indexed array
result = tampax("Hello {{0}}, you have {{1}} unread messages", ["Robert", 12])
console.log(result);
// result -> "Hello Robert, you have 12 unread messages"
find all occurences of {{mystring}} in the string, and replace it.
- string
required
{YAML string} A YAML string to parse. - args
required
{Object|Array} Variables to find. If an array is given, you need to use numbers instead of words. - return {string} Return a string with everything replaced.
Find all occurences of {{mystring}}, and a correspondance in the YAML file itself, and the optional args
parameter.
- string
required
{YAML string} A YAML string to parse. - args
optional
{Object|Array} Variables to find. If an array is given, you need to use numbers instead of words. - opts
optional
{Object} Options to call to yaml.safeLoad(). - callback {function}
- err {string} Error and why.
- data {Object} Return an object with everything replaced.
Same as tampax.yamlParseString()
, but take a file instead of a string.
- string
required
{string} A File to parse. - args
optional
{Object|Array} Variables to find. If an array is given, you need to use numbers instead of words. - opts
optional
{Object} Options to call to yaml.safeLoad(). - callback {function}
- err {string} Error and why.
- data {Object} Return an object with everything replaced.
Escape {{}} pairs by using triple {{}}}
var text = tampax("{{{0}}}")
// text -> "{{0}}"
Arthur Lacoste