diff --git a/README.md b/README.md index be9336e..f6d891d 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,11 @@ wNumb - JavaScript Number & Money formatting Documentation and examples are available on [refreshless.com/wnumb](http://refreshless.com/wnumb/). +# Changelog + +### 1.1.0 (*2017-02-04*) +- Changed: Renamed `postfix` option to the proper `suffix`. `postfix` is remapped internally for backward compatibility; + # License Licensed [WTFPL](http://www.wtfpl.net/about/), so free for personal and commercial use. diff --git a/bower.json b/bower.json index f54b4bc..e223340 100644 --- a/bower.json +++ b/bower.json @@ -1,8 +1,8 @@ { "name": "wnumb", "main": "wNumb.js", - "version": "1.0.4", - "homepage": "http://refreshless.com/wnumb/", + "version": "1.1.0", + "homepage": "https://refreshless.com/wnumb/", "description": "wNumb - JavaScript Number & Money formatting", "keywords": [ "javascript", diff --git a/package.json b/package.json index 13a3f7f..2cdafa6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wnumb", - "version": "1.0.4", + "version": "1.1.0", "description": "wNumb - JavaScript Number & Money formatting", "main": "wNumb.js", "repository": { diff --git a/test.html b/test.html index b04eead..7ddaeb3 100644 --- a/test.html +++ b/test.html @@ -45,10 +45,25 @@ decimals: 0 }); - equal ( Tester.to ( -50.999845 ), '-51' ); + equal ( Tester.to ( -50.999845 ), '-51' ); equal ( Tester.to ( -0.0000001 ), '0' ); }); + test( "Testing rounding", function(){ + + var Tester = wNumb({ + decimals: 2 + }); + + equal ( Tester.to ( 8.905 ), '8.91' ); + equal ( Tester.to ( 8.9049999 ), '8.90' ); + equal ( Tester.to ( 8.9045 ), '8.90' ); + equal ( Tester.to ( 8.9044 ), '8.90' ); + equal ( Tester.to ( 8.9 ), '8.90' ); + equal ( Tester.to ( 1.275 ), '1.28' ); + equal ( Tester.to ( 1.27499 ), '1.27' ); + }); + test( "Testing mark and thousand with longer strings", function(){ var Tester = wNumb({ @@ -65,11 +80,11 @@ }); - test( "Testing prefix and postfix", function(){ + test( "Testing prefix and suffix", function(){ var Tester = wNumb({ prefix: '$', - postfix: ' p.p.', + suffix: ' p.p.', decimals: 5 }); @@ -87,6 +102,13 @@ equal ( Tester.from ( '1500 ' ), 1500 ); equal ( Tester.from ( ' 1500 ' ), 1500 ); equal ( Tester.from ( ' 1.50.0 ' ), false ); + + var Tester2 = wNumb({ + postfix: ' postfix!', + decimals: 1 + }); + + equal ( Tester2.to ( 1 ), '1.0 postfix!' ); }); test( "Testing prefix and with negativeBefore", function(){ diff --git a/wNumb.js b/wNumb.js index 75c66ee..4ab340f 100644 --- a/wNumb.js +++ b/wNumb.js @@ -20,13 +20,12 @@ 'use strict'; -var -/** @const */ FormatOptions = [ +var FormatOptions = [ 'decimals', 'thousand', 'mark', 'prefix', - 'postfix', + 'suffix', 'encoder', 'decoder', 'negativeBefore', @@ -47,7 +46,7 @@ var return input.substring(0, match.length) === match; } - // Check is a string ends in a specified postfix. + // Check is a string ends in a specified suffix. function strEndsWith ( input, match ) { return input.slice(-1 * match.length) === match; } @@ -65,16 +64,19 @@ var } // Provide rounding-accurate toFixed method. - function toFixed ( value, decimals ) { - var scale = Math.pow(10, decimals); - return ( Math.round(value * scale) / scale).toFixed( decimals ); + // Borrowed: http://stackoverflow.com/a/21323330/775265 + function toFixed ( value, exp ) { + value = value.toString().split('e'); + value = Math.round(+(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp))); + value = value.toString().split('e'); + return (+(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp))).toFixed(exp); } // Formatting // Accept a number as input, output formatted string. - function formatTo ( decimals, thousand, mark, prefix, postfix, encoder, decoder, negativeBefore, negative, edit, undo, input ) { + function formatTo ( decimals, thousand, mark, prefix, suffix, encoder, decoder, negativeBefore, negative, edit, undo, input ) { var originalInput = input, inputIsNegative, inputPieces, inputBase, inputDecimals = '', output = ''; @@ -151,9 +153,9 @@ var output += inputBase; output += inputDecimals; - // Apply the postfix. - if ( postfix ) { - output += postfix; + // Apply the suffix. + if ( suffix ) { + output += suffix; } // Run the output through a user-specified post-formatter. @@ -166,7 +168,7 @@ var } // Accept a sting as input, output decoded number. - function formatFrom ( decimals, thousand, mark, prefix, postfix, encoder, decoder, negativeBefore, negative, edit, undo, input ) { + function formatFrom ( decimals, thousand, mark, prefix, suffix, encoder, decoder, negativeBefore, negative, edit, undo, input ) { var originalInput = input, inputIsNegative, output = ''; @@ -198,10 +200,10 @@ var inputIsNegative = true; } - // Remove the postfix. + // Remove the suffix. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice - if ( postfix && strEndsWith(input, postfix) ) { - input = input.slice(0, -1 * postfix.length); + if ( suffix && strEndsWith(input, suffix) ) { + input = input.slice(0, -1 * suffix.length); } // Remove the thousand grouping. @@ -255,6 +257,10 @@ var var i, optionName, optionValue, filteredOptions = {}; + if ( inputOptions['suffix'] === undefined ) { + inputOptions['suffix'] = inputOptions['postfix']; + } + for ( i = 0; i < FormatOptions.length; i+=1 ) { optionName = FormatOptions[i]; @@ -323,7 +329,6 @@ var return method.apply('', args); } - /** @constructor */ function wNumb ( options ) { if ( !(this instanceof wNumb) ) { @@ -347,7 +352,6 @@ var }; } - /** @export */ return wNumb; }));