luci-base: cbi.js: fix number rounding in string.format()

Ensure that patterns like %d, %x, %o or %b properly truncate their
operands to whole integers.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
This commit is contained in:
Jo-Philipp Wich 2019-06-06 21:06:25 +02:00
parent 0539e7f6b9
commit 0bcb9f9f1d

View file

@ -566,7 +566,7 @@ String.prototype.format = function()
switch(pType) { switch(pType) {
case 'b': case 'b':
subst = (+param || 0).toString(2); subst = (~~param || 0).toString(2);
break; break;
case 'c': case 'c':
@ -574,7 +574,7 @@ String.prototype.format = function()
break; break;
case 'd': case 'd':
subst = ~~(+param || 0); subst = (~~param || 0);
break; break;
case 'u': case 'u':
@ -588,7 +588,7 @@ String.prototype.format = function()
break; break;
case 'o': case 'o':
subst = (+param || 0).toString(8); subst = (~~param || 0).toString(8);
break; break;
case 's': case 's':
@ -596,11 +596,11 @@ String.prototype.format = function()
break; break;
case 'x': case 'x':
subst = ('' + (+param || 0).toString(16)).toLowerCase(); subst = ('' + (~~param || 0).toString(16)).toLowerCase();
break; break;
case 'X': case 'X':
subst = ('' + (+param || 0).toString(16)).toUpperCase(); subst = ('' + (~~param || 0).toString(16)).toUpperCase();
break; break;
case 'h': case 'h':