From 58e857da85ce471705aced5e8b2e94296a747c77 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Fri, 16 Feb 2018 23:19:19 +0100 Subject: [PATCH] maths: add pad function --- resources/docs/extras/property-maths.md | 5 +++-- src/browser/js/app/widgets/utils.js | 8 ++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/resources/docs/extras/property-maths.md b/resources/docs/extras/property-maths.md index 604b234f..1a67f711 100644 --- a/resources/docs/extras/property-maths.md +++ b/resources/docs/extras/property-maths.md @@ -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. diff --git a/src/browser/js/app/widgets/utils.js b/src/browser/js/app/widgets/utils.js index cceeb7bb..d650946b 100644 --- a/src/browser/js/app/widgets/utils.js +++ b/src/browser/js/app/widgets/utils.js @@ -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 },