From 3a5e1bb5d5993b8129e4f9f530c776156526d955 Mon Sep 17 00:00:00 2001 From: Leon Gersen Date: Sat, 14 Jun 2014 15:53:19 +0200 Subject: [PATCH] Fixed `decimals: 0` --- test.html | 13 +++++++++++-- wNumb.js | 10 ++++++++-- wNumb.min.js | 8 ++++---- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/test.html b/test.html index 7c1db79..4d1ad50 100644 --- a/test.html +++ b/test.html @@ -39,6 +39,15 @@ equal ( Tester.from ( '-79?900_041' ), -79900.041 ); }); + test( "Testing 0 decimals", function(){ + + var Tester = wNumb({ + decimals: 0 + }); + + equal ( Tester.to ( -50.999845 ), '-51' ); + }); + test( "Testing mark and thousand with longer strings", function(){ var Tester = wNumb({ @@ -131,7 +140,7 @@ test( "Testing edit, undo", function(){ var Tester = wNumb({ - edit: function( value, originalValue ){ + edit: function( value, originalValue ){ if ( originalValue > 100000 ) { return value + 'm'; } else if ( originalValue > 1000 ) { @@ -147,7 +156,7 @@ equal ( Tester.to ( 15000 ), '15000k' ); equal ( Tester.from ( '15000k' ), 15000 ); - + equal ( Tester.to ( 180 ), '180' ); equal ( Tester.from ( '180' ), 180 ); diff --git a/wNumb.js b/wNumb.js index 89902a7..8ecfdd9 100644 --- a/wNumb.js +++ b/wNumb.js @@ -85,7 +85,7 @@ var } // Reduce the number of decimals to the specified option. - if ( decimals ) { + if ( decimals !== false ) { input = toFixed( input, decimals ); } @@ -248,14 +248,18 @@ var // Floating points in JS are stable up to 7 decimals. } else if ( optionName === 'decimals' ) { - if ( optionValue > 0 && optionValue < 8 ) { + if ( optionValue >= 0 && optionValue < 8 ) { filteredOptions[optionName] = optionValue; + } else { + throw new Error(optionName); } // These options, when provided, must be functions. } else if ( optionName === 'encoder' || optionName === 'decoder' || optionName === 'edit' || optionName === 'undo' ) { if ( typeof optionValue === 'function' ) { filteredOptions[optionName] = optionValue; + } else { + throw new Error(optionName); } // Other options are strings. @@ -263,6 +267,8 @@ var if ( typeof optionValue === 'string' ) { filteredOptions[optionName] = optionValue; + } else { + throw new Error(optionName); } } } diff --git a/wNumb.min.js b/wNumb.min.js index 1108bf2..90fed33 100644 --- a/wNumb.min.js +++ b/wNumb.min.js @@ -1,4 +1,4 @@ -(function(){function r(b){return b.split("").reverse().join("")}function s(b,f,c){if((b[f]||b[c])&&b[f]===b[c])throw Error(f);}function v(b,f,c,d,e,p,q,k,l,h,n,a){q=a;var m,g=n="";p&&(a=p(a));if("number"!==typeof a||!isFinite(a))return!1;b&&0===parseFloat(a.toFixed(b))&&(a=0);0>a&&(m=!0,a=Math.abs(a));b&&(p=Math.pow(10,b),a=(Math.round(a*p)/p).toFixed(b));a=a.toString();-1!==a.indexOf(".")&&(b=a.split("."),a=b[0],c&&(n=c+b[1]));f&&(a=r(a).match(/.{1,3}/g),a=r(a.join(r(f))));m&&k&&(g+=k);d&&(g+=d); -m&&l&&(g+=l);g=g+a+n;e&&(g+=e);h&&(g=h(g,q));return g}function w(b,f,c,d,e,h,q,k,l,r,n,a){var m;b="";n&&(a=n(a));if(!a||"string"!==typeof a)return!1;k&&a.substring(0,k.length)===k&&(a=a.replace(k,""),m=!0);d&&a.substring(0,d.length)===d&&(a=a.replace(d,""));l&&a.substring(0,l.length)===l&&(a=a.replace(l,""),m=!0);e&&a.slice(-1*e.length)===e&&(a=a.slice(0,-1*e.length));f&&(a=a.split(f).join(""));c&&(a=a.replace(c,"."));m&&(b+="-");b=Number((b+a).replace(/[^0-9\.\-.]/g,""));q&&(b=q(b));return"number"=== -typeof b&&isFinite(b)?b:!1}function x(b){var f,c,d,e={};for(f=0;fd&&(e[c]=d):"encoder"===c||"decoder"===c||"edit"===c||"undo"===c?"function"===typeof d&&(e[c]=d):"string"===typeof d&&(e[c]=d);s(e,"mark","thousand");s(e,"prefix","negative");s(e,"prefix","negativeBefore");return e}function u(b,f,c){var d,e=[];for(d=0;da&&(m=!0,a=Math.abs(a));!1!==b&&(p=Math.pow(10,b),a=(Math.round(a*p)/p).toFixed(b));a=a.toString();-1!==a.indexOf(".")&&(b=a.split("."),a=b[0],c&&(n=c+b[1]));f&&(a=r(a).match(/.{1,3}/g),a=r(a.join(r(f))));m&&k&&(g+=k);d&&(g+= +d);m&&l&&(g+=l);g=g+a+n;e&&(g+=e);h&&(g=h(g,q));return g}function w(b,f,c,d,e,h,q,k,l,r,n,a){var m;b="";n&&(a=n(a));if(!a||"string"!==typeof a)return!1;k&&a.substring(0,k.length)===k&&(a=a.replace(k,""),m=!0);d&&a.substring(0,d.length)===d&&(a=a.replace(d,""));l&&a.substring(0,l.length)===l&&(a=a.replace(l,""),m=!0);e&&a.slice(-1*e.length)===e&&(a=a.slice(0,-1*e.length));f&&(a=a.split(f).join(""));c&&(a=a.replace(c,"."));m&&(b+="-");b=Number((b+a).replace(/[^0-9\.\-.]/g,""));q&&(b=q(b));return"number"=== +typeof b&&isFinite(b)?b:!1}function x(b){var f,c,d,e={};for(f=0;fd)e[c]=d;else throw Error(c);else if("encoder"===c||"decoder"===c||"edit"===c||"undo"===c)if("function"===typeof d)e[c]=d;else throw Error(c);else if("string"===typeof d)e[c]=d;else throw Error(c);s(e,"mark","thousand");s(e,"prefix","negative");s(e,"prefix","negativeBefore");return e} +function u(b,f,c){var d,e=[];for(d=0;d