fast and powerful string micro templating
The module available via npm
npm i f2 --saveclass
var F2 = require('f2/f2');
var f2 = new F2();The module provides a complete instance with built-in substitution formatters.
var f2 = require('f2');A class reference needed only for advanced usage.
method
Formats pattern with passed arguments
var f2 = require('f2');
f2.format('Lorem %s!', 'ipsum'); // -> 'Lorem ipsum!'Common method. See the full templates features below. The method is bound to instance, it can be used as static function
var f = require('f2').format;
f('Lorem %s!', 'ipsum'); // -> 'Lorem ipsum!'Also the method is represented as separate module
var f = require('f2/format');
f('Lorem %s!', 'ipsum'); // -> 'Lorem ipsum!'method
Do something like f2.format.apply(f2, ['Lorem %s!', 'ipsum']), but supports args array offsets from left and from right.
var f2 = require('f2');
f2.applyArgs(['Lorem %s!', 'ipsum']); // -> 'Lorem ipsum!'
f2.applyArgs(['foobar', 'Lorem %s!', 'ipsum'], 1); // -> 'Lorem ipsum!'
f2.applyArgs(['foobar', 'Lorem %s!', 'ipsum'], 1, 1); // -> 'Lorem undefined!' // oops!method
Like f2.applyArgs, but first argument is template
var f2 = require('f2');
f2.applyArgsTo('Lorem %s!', ['ipsum']); // -> 'Lorem ipsum!'
f2.applyArgsTo('Lorem %s!', ['foobar', 'ipsum'], 1); // -> 'Lorem ipsum!'method
Adds new substitution type
var F2 = require('f2/f2');
var f2 = new F2();
f2.type('b', function (v) {
return '[' + v + ']';
});
f2.format('Lorem %b!', 'ipsum'); // -> 'Lorem [ipsum]!'There are available s, d and j formatters in bundled instance
f2.format('%2$s %1$s', 'foo', 'bar'); // -> 'bar foo'f2.format('Login to %(user.name)s at %(time)s', {user: {name: 'golyshevd'}, time: 'Tomorrow'});
// -> Login to golyshevd at Tomorrowf2.format('Lorem %s', 'ipsum', 'dolor'); // -> 'Lorem ipsum \'dolor\''f2.format('Lorem %s!', function () {
return 'ipsum';
}); // -> 'Lorem ipsum!'Any substitution type can support options that can be passed in template between % and <type>
f2.format('Lorem %-?:5.3s!', 'ipsum'); // 'Lorem ips??!'There are fill, sign, width and precision. Every formatter can interpret parameters handling in their own way. All parameters are available in substitution formatter function body.
f2.type('b', function (value, fill, sign, width, precision) {
return value; // Ignore any parameters, why not?
});LICENSE MIT