Terminal Value Resolver - power string interpolation
Your #1 choice for string interpolation.
A powerful tool to have at the core of any HTML template rendering engine, or when you just want to spit out a message string without having to do concatenation. Dig deep - the surface-level simplicity may belie the underlying power of this module.
_Documentation coming soon_
NodeJS:
``js`
var tvr = require('tvr')(opt);
/*
opt = optional parameters
log: Console logger object
env: Shared public variables object
NS: Namespace reference to simple-guts based namespace
*/`
Examples:js`
var guts = require('simple-guts')();
var tvr = require('tvr')({NS: guts});
...
console.log(tvr.$s('Message: ${msg}', {msg: 'Hello world!'}));
For convenience and reduced typing, it is suggested to define (local or global) references to a few of the primary methods:
`js`
var tvr = require('tvr');
$r = tvr.$r;
$v = tvr.$v;
$s = tvr.$s;
$o = tvr.$o;
Powerful expression evaluator for computing dynamic values.
$r – Resolves raw value
$v – Resolves variable expression to terminal value
$s – Resolves expression to string value
$r(string, context) - Resolve raw value
`js`
var person = {
fname: 'John',
lname: 'Smith',
age: 27,
married: false,
hobbies: ['flying', 'scuba']
};
var name = $r('fname', person); // 'John'
var age = $r('age', person); // 27
var married = $r('married', person); // false
var hobbies = $r('hobbies', person); // ['flying', 'scuba']
$v(val, context) - Resolves variable expression to terminal value
`js`
var person = {
fname: 'John',
lname: 'Smith',
age: 27,
fullname: '${fname} ${lname}',
capsname: function() {
return this.fname.toUpperCase();
},
mixname: '${capsname} ${lname}',
getname: function() { return this.fullname; }
};
var name = $v('${fname}', person); // 'John'
name = $v('${fullname}', person); // 'John Smith'
name = $v('${capsname}', person); // 'JOHN'
name = $v('${mixname}', person); // 'JOHN Smith'
name = $v('Name: ${getname}', person); // 'Name: John Smith'
name = $v('Hey!', person); // 'Hey!'
var age = $v('Age: ${age}', person); // 'Age: 27'
age = $v('${age}', person); // 27
TODO Show Boolean, number, null, undefined, NaN, object
$s(val, context) - Resolves variable expression to string value
`js``
var age = $s('${age}', person); // '27'
var married = $s('${married}', person); // 'false'
var hobbies = $s('${hobbies}', person); // 'flying,scuba'
TODO Show Boolean, number, null, undefined, NaN, object
TODO Statics:
|Static|Description|
|:--|:--|
|{NS}*|Reference to top-level namespace ({NS} will be whatever is defined in $env.namespace, or "NS" by default - see simple-guts package)|
|$env|Reference to global environment block|
|$not(val)|Inverts a truthy/falsey/Boolean value|
|$eq(val1, val2)|Compares two values for equality (==)|
|$neq(val1, val2)|Compares two values for inequality (!=)|
|$and(val...)|Tests all parameters to determine if all are truthy (implicit Boolean conversion)|
|$or(val...)|Tests all parameters to determine if at least one is truthy (implicit Boolean conversion)|
|$xor(val...)|Tests all parameters to see if not all are the same|
|$nand(val...)|Tests all parameters to determine if at lease one is falsey (implicit Boolean conversion)|
|$nor(val...)|Tests all parameters to determine if all are falsey (implicit Boolean conversion)|
|$bit(val, num)|Checks a specified bit in a value to determine if that bit is set (num = bit number)|
|$bits(val, mask)|Selects bits based on specified bit mask to determine if any are set (returns actual bits for truthy/falsey check)|
|$nobit(val, num)|Checks a specified bit in a value to determine if that bit is clear (not set)|
|$nobits(val, mask)|Selects bits based on specified bit mask to determine if all are clear (not set)|
|$bool(val)|Resolve truthy/falsey value to Boolean|
|$plural(label, n, suffix)|Returns the plural version of a label (English assumed) if n != 1 by appending suffix (if "ies", it will replace "y" if found at the end of label)|
|$fixed(num, decimals)|Returns a numeric value as a string, rounded to the nearest specified decimal places|
Configuration properties are picked up from $env.
- $env.fixValues - DEPRECATED - Akin to noTerminal in old code
- $env.noTerminal - If true, values like "true", "false" and numeric strings are not converted to their
respective data types (they remain as strings). So $v('${x}', {x: 'false'}) would be 'false' not false.
Resolver configuration can be changed, programmatically, via:
- Kjs.resolver.cfg.fixValues - See above
- Kjs.resolver.cfg.noTerminal - See above
- Kjs.resolver.cfg.deadTag - Key value to match to remove a property in a call to derive().