Skip to content

golyshevd/f2

Repository files navigation

f2 Build Status

fast and powerful string micro templating

Installation

The module available via npm

npm i f2 --save

API

Common

F2 new F2()

class

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.

String f2.format([String f][, * arg[, * arg...]])

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!'

Advanced & sugar

String f2.applyArgs(Array args[, Number offsetL[, Number offsetR]])

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!

String f2.applyArgsTo(String f, Array args[, Number offsetL[, Number offsetR]])

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!'

F2 f2.type(String type, Function formatter)

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

Features

Explicit indexes for positional arguments

f2.format('%2$s %1$s', 'foo', 'bar'); // -> 'bar foo'

Keyword arguments

f2.format('Login to %(user.name)s at %(time)s', {user: {name: 'golyshevd'}, time: 'Tomorrow'});
// -> Login to golyshevd at Tomorrow

Inspecting extra arguments

f2.format('Lorem %s', 'ipsum', 'dolor'); // -> 'Lorem ipsum \'dolor\''

Functional arguments

f2.format('Lorem %s!', function () {
    return 'ipsum';
}); // -> 'Lorem ipsum!'

Substitution options

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

About

fast and powerful string micro templating

Resources

License

Stars

Watchers

Forks

Packages

No packages published