diff options
author | Jo-Philipp Wich <jow@openwrt.org> | 2016-02-15 12:45:39 +0100 |
---|---|---|
committer | Jo-Philipp Wich <jow@openwrt.org> | 2016-02-15 12:45:42 +0100 |
commit | a860de860ad8c15d1717f317c91c2e9168260a4f (patch) | |
tree | 5e8aaf3714f4789107facf96a3eb5f8fabed4d45 | |
parent | 5cfad4338f4749aee0a8d81e3b3727ec6630485d (diff) |
luci-base: cbi.js: string formatting fixes
* Fix left and right justify/padding in formats
* Do not emit decimal numbers for small values in %m format
Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
-rw-r--r-- | modules/luci-base/htdocs/luci-static/resources/cbi.js | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/modules/luci-base/htdocs/luci-static/resources/cbi.js b/modules/luci-base/htdocs/luci-static/resources/cbi.js index f58306954e..c084709f92 100644 --- a/modules/luci-base/htdocs/luci-static/resources/cbi.js +++ b/modules/luci-base/htdocs/luci-static/resources/cbi.js @@ -1309,7 +1309,7 @@ String.prototype.format = function() var re = /^(([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X|q|h|j|t|m))/; var a = b = [], numSubstitutions = 0, numMatches = 0; - while( a = re.exec(str) ) + while (a = re.exec(str)) { var m = a[1]; var leftpart = a[2], pPad = a[3], pJustify = a[4], pMinLength = a[5]; @@ -1332,6 +1332,8 @@ String.prototype.format = function() pad = leftpart.substr(1,1); else if (pPad) pad = pPad; + else + pad = ' '; var justifyRight = true; if (pJustify && pJustify === "-") @@ -1432,17 +1434,27 @@ String.prototype.format = function() var i = 0; var val = (+param || 0); - var units = [ '', 'K', 'M', 'G', 'T', 'P', 'E' ]; + var units = [ ' ', ' K', ' M', ' G', ' T', ' P', ' E' ]; for (i = 0; (i < units.length) && (val > mf); i++) val /= mf; - subst = val.toFixed(pr) + ' ' + units[i]; + subst = (i ? val.toFixed(pr) : val) + units[i]; + pMinLength = null; break; } } } + if (pMinLength) { + subst = subst.toString(); + for (var i = subst.length; i < pMinLength; i++) + if (pJustify == '-') + subst = subst + ' '; + else + subst = pad + subst; + } + out += leftpart + subst; str = str.substr(m.length); } |