Skip to content
master
Switch branches/tags
Go to file
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 

tampax

YAML, extended to support inline variables.

NPM Version XO code style build status

Replacing strings is now as simple as fill a hole.

Template string, files, and even YAML.

Introduction

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.

Installation

npm install tampax --save

Example

Using YAML variables

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.' }
  */
});

Templating string without YAML

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"

Doc

tampax(string, args)

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.

tampax.yamlParseString(string[, args, opts], callback)

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.

tampax.yamlParseFile(string[, args, opts], callback)

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.

Escaping

Escape {{}} pairs by using triple {{}}}

var text = tampax("{{{0}}}")
// text -> "{{0}}"

Related projects

Licence

CC-BY-NC-SA-4.0

Arthur Lacoste

About

πŸ’Š Replacing strings is now as simple as fill a hole.

Resources

Releases

No releases published

Packages

No packages published