var jslt = require("jslt");
jslt.transform({
firstName: "Chandler",
lastName: "Bing",
married: true,
}, {
fullName: "{{lastName}}, {{firstName}}",
status: {
$fetch: "{{married}}",
$translate: [
{ from: true, to: "Married" },
{ default: "Single" }
]
}
}
);
-
Transformation basics
-
Placeholder replacement
-
Update rules
-
Update operators:
-
Query operators:
When transforming an object, the properties in the template are recursivly traversed and their output values are determined using the following rules:
- If the template value is a string containing placeholders, they are replaced according to the placeholder replacement rules.
- If the template value is an object that at least one of its properties starts with a dollar sign, the object is processed using the update rules.
- Otherwise the template value is copied to the output.
Placeholders are defined using double curly braces ({{name}}
). The text inside is interpreted as the name of a property in the object being transformed. The result of the placeholder is the value of the same named property in the object being transformed. Nested properties can be accessed using a dot notation (e.g. {{prop1.prop2}}
).
If a string contains a single placeholder without any other character, the returned value will be of the same type as the original value. Otherwise, it will be converted into a string.
Operators are processed in the order they are defined in the object. The output of the first operator becomes the input for the second operator and so forth. If an operator needs to be repeated more than once in the same object, a number can be added to its name (e.g. $translate2
).
Input
NoneParameters
<Array> The array to joinOutput
<String>
Example:
var res = jslt.transform({}, { $join: [ "Hello", " ", "world" ] });
console.log(res); // => "Hello world"
Merges two or more arrays.
Input
NoneParameters
<Array> An array of arrays to concatOutput
<Array>
Example:
var res = jslt.transform({}, { $concat: [ [ 1, 2 ], [ 3, 4 ] ] });
console.log(res); // => [ 1, 2, 3, 4 ]
Returns a string with a language sensitive representation of the input date. See Date.prototype.toLocaleString() documentation for more information.
Input
<Date> | <Number> | <String> If not a date object, value should be in a format supported by the Date constructorParameters
locales
<String> Optionaloptions
<Object> Optional
Output
<String>
Returns a string with a language sensitive representation of the input number. See Number.prototype.toLocaleString() documentation for more information.
Input
<Number> The number to formatParameters
locales
<String> Optionaloptions
<Object> Optional
Output
<String>
Example:
var res = jslt.transform(1000, { $formatNumber: { locales: "en-US" options: { minimumFractionDigits: 2 } } });
console.log(res); // => "1,000.00"
Parses the input string and returns a number.
Input
<String> The string to parseParameters
groupSymbol
<String> OptionaldecimalSymbol
<String> Optional
Output
<Number>
Example:
var res = jslt.transform("1,000", { $parseNumber: { groupSymbol: "," } });
console.log(res); // => 1000
Parses the input string and returns a date object.
Input
<String>Parameters
format
<String>timezone
<String>
Output
<Date>
Returns a new string with some or all matches of a pattern replaced by a new string.
Input
<String>Parameters
substr
<String> A string that is to be replaced by newSubStrregexp
<RegExp>newSubstr
<String> The string that replaces the substring specified by the regexp or substr parameters
Output
<String>
Example:
var res = jslt.transform("Hello world", { $replace: { substr: "world", newSubstr: "world!!" });
console.log(res); // => "Hello world!!"
Transforms the input data using the supplied sub-template and returns the result. Typically used as a starting point for other operators
Input
NoneParameters
<Any> The sub-templateOutput
<Any>
Example:
var res = jslt.transform({ someField: "test" }, { $fetch: "{{someField}}" });
console.log(res); // => "test"
With another operator:
var res = jslt.transform({ arrayField: [ 2, 4, 6 ] }, {
$fetch: "{{arrayField}}",
$sum : {}
});
console.log(res); // => 12
Selects a value by applying from-to rules on the input value and returns it. If none of the rules match, returns the input value.
Rules can contain query operators for complex translation logic.
Input
<Any>Parameters
<Array> And array of from-to rules (see examples)Output
<Any>
Example:
var res = jslt.transform(true, {
$translate: [
{ from: true, to: "Yes" },
{ from: false, to: "No" }
}
);
console.log(res); // => true
With query operators:
var res = jslt.transform(10, {
$translate: [
{ from: { $lt: 8 }, to: "Low" },
{ from: { $gte: 8 }, to: "High" }
]
});
console.log(res); // => "High"
The above example with a default value:
var res = jslt.transform(10, {
$translate: [
{ from: { $lt : 8 }, to: "Low" },
{ default: "High" }
]
});
console.log(res); // => "High"
Input
<Array>Parameters
<Any>Output
<Array>
Input
<Array>Parameters
<Any>Output
<Array>
Adds an element to the end of an array.
Input
<Array>Parameters
<Any> The element to addOutput
<Array>
Example:
var res = jslt.transform([ 1, 2 ], { $push: 3 });
console.log(res); // => [ 1, 2, 3 ]
Adds an element to the beginning of an array.
Input
<Array>Parameters
<Any> The element to addOutput
<Array>
Example:
var res = jslt.transform([ 1, 2 ], { $unshift: 0 });
console.log(res); // => [ 0, 1, 2 ]
Reverses an array.
Input
<Array>Parameters
NoneOutput
<Array>
Example:
var res = jslt.transform([ 1, 2, 3 ], { $reverse: {} });
console.log(res); // => [ 3, 2, 1 ]
Returns the sum of all the elements in an array.
Input
<Array>Parameters
NoneOutput
<Number>
var res = jslt.transform([ 1, 2, 3 ], { $sum: {} });
console.log(res); // => 6