This library is created to provide formatter declaration that can be transported through network and executed securely. The formatter is written in JSON and it's highly configurable. This formatter declaration is called "array expression".
Expression array syntax is specified below:
[expression_name, arg0, arg1, ...]Expression is categorized into 4 different types:
- Type
- Math
- String
- Decision
Converts value into boolean.
['boolean', value]: booleanConverts value into string.
['string', value]: stringPerforms addition and return the resulting number.
['+', number1, number2]: numberPerforms subtraction and return the resulting number.
['-', number1, number2]: numberPerforms multiplication and return the resulting number.
['*', number1, number2]: numberPerforms division and return the resulting number.
['/', number1, number2]: numberPerforms remainder operation and return the resulting number.
['%', number1, number2]: numberReturns number1 to the power of number2.
['^', number1, number2]: numberReturns the square root of a number.
['sqrt', number]: numberReturns the absolute value of a number.
['abs', number]: numberReturns the sine of a number.
['sin', number]: numberReturns the cosine of a number.
['cos', number]: numberReturns the tangent of a number.
['tan', number]: numberReturns natural logarithm (base e) of a number.
['log', number]: numberReturns the greatest integer less than or equal to a value.
['floor', number]: numberReturns the smallest integer greater than or equal to a value.
['ceil', number]: numberRounds a number to the nearest integer and return the resulting number.
['round', number]: numberReturns the lowest number in the arguments.
['min', number1, number2, ...]: numberReturns the highest number in the arguments.
['max', number1, number2, ...]: numberConcatenate the arguments into one single string.
['concat', value1, value2, ...]: stringConverts all the alphabetic characters in a string to lowercase and return the resulting string.
['downcase', string]: stringConverts all the alphabetic characters in a string to uppercase and return the resulting string.
['upcase', string]: stringFormat a number by specifying the minimum and maximum fraction digits.
['number-format', number, minimum_fraction_digits, [maximum_fraction_digits]]: stringNegates a value and return the resulting boolean.
['!', value]: booleanPerforms strict equality operation and return the resulting boolean.
['==', value]: booleanPerforms strict inequality operation and return the resulting boolean.
['!=', value]: booleanReturns true if number1 or string1 is less than number2 or string2, otherwise return false.
['<', number1 | string1, number2 | string2]: booleanReturns true if number1 or string1 is less than or equal to number2 or string2, otherwise return false.
['<=', number1 | string1, number2 | string2]: booleanReturns true if number1 or string1 is greater than number2 or string2, otherwise return false.
['>', number1 | string1, number2 | string2]: booleanReturns true if number1 or string1 is greater than or equal to number2 or string2, otherwise return false.
['>=', number1 | string1, number2 | string2]: booleanReturns true if all the values evaluate to true.
['all', value1, value2, ...]: booleanReturns true if any of the values evaluate to true.
['any', value1, value2, ...]: booleanPerform if else operation.
['if', condition1, output1, condition2, output2, ..., fallback_output]: booleanPlease see the example below:
// Returns 'equal to two'
exp(['if', ['==', 5, 1], 'equal to one', ['==', 2, 2], 'equal to two', 'failure']);By specifying a variable using wildcard, we can inject data into the expression.
Please see the example below:
const expression: Expression = [
'if',
['==', '$animal', 'cat'],
'meow',
['==', '$animal', 'dog'],
'woof',
'Wa-pa-pa-pa-pa-pa-pow!',
];
// Returns 'meow'
exp(expression, { animal: 'cat' });
// Returns 'woof'
exp(expression, { animal: 'dog' });
// Returns 'Wa-pa-pa-pa-pa-pa-pow!'
exp(expression, { animal: 'fox' });Below, we will convert unit from liter per minute into US gallon per hour.
const data: Data = {
// value contains liter per minute
value: 5,
};
// Returns '79.3 gallon/hour'
exp(
['concat', ['number-format', ['*', ['*', '$value', 0.2641722], 60], 0, 1], ' ', 'gallon/hour'],
data
);array-expression is MIT licensed.