maths: add pad function

This commit is contained in:
jean-emmanuel 2018-02-16 23:19:19 +01:00
parent 0ef075d3de
commit 58e857da85
2 changed files with 11 additions and 2 deletions

View file

@ -13,13 +13,14 @@ Where FORMULA is a valid [MathJS](http://mathjs.org/docs/expressions/syntax.html
- [available constants](http://mathjs.org/docs/reference/constants.html)
- formulas can be [multiline](http://mathjs.org/docs/expressions/syntax.html#multiline-expressions)
- property inheritance calls (`@{...}`) are always resolved before formulas
- arrays / matrices indexes are **zero-based** (ie `['a','b'][0]` returns `'a'`)
- arrays / matrices indexes are **zero-based** (ie `["a","b"][0]` returns `"a"`)
- strings can be multiline when enclosed in backticks instead of double quotes (``` ` ` ```)
- inner closing brackets (`}`) must be escaped with a backslash (`\}`)
Additionnal functions:
- `unpack`: remove an array's brackets (`unpack([1,2])` returns `'1, 2'`)
- `unpack(x)`: remove an array's brackets (`unpack([1,2])` returns `"1, 2"`)
- `pad(x, padding)`: add leading zeros if the length the integer part of `x` is smaller than `padding` (`pad(1,2)` returns `"01"`)
!!! tip ""
A single widget property can contain multiple formulas. Variables and functions declared in a formula are available to subsequent formulas in the same property definition.

View file

@ -76,6 +76,14 @@ module.exports = {
})
math.import({
unpack: function(a) { return Array.isArray(a) ? a.toString() : a },
pad: function(x, padding) {
var str = String(x),
[int, dec] = str.split('.')
while (int.length < padding) {
int = '0' + int
}
return dec ? int + '.' + dec : int
},
// basic relationnal to keep alphabetical string comparison (v4 change)
equal: function (a, b) { return a == b },
unequal: function (a, b) { return a != b },