Skip to content

Commit

Permalink
postfix -> suffix, number rounding
Browse files Browse the repository at this point in the history
  • Loading branch information
leongersen committed Feb 4, 2017
1 parent 352dc0e commit 725bc8e
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 23 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
4 changes: 2 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wnumb",
"version": "1.0.4",
"version": "1.1.0",
"description": "wNumb - JavaScript Number & Money formatting",
"main": "wNumb.js",
"repository": {
Expand Down
28 changes: 25 additions & 3 deletions test.html
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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
});

Expand All @@ -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(){
Expand Down
38 changes: 21 additions & 17 deletions wNumb.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@

'use strict';

var
/** @const */ FormatOptions = [
var FormatOptions = [
'decimals',
'thousand',
'mark',
'prefix',
'postfix',
'suffix',
'encoder',
'decoder',
'negativeBefore',
Expand All @@ -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;
}
Expand All @@ -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 = '';

Expand Down Expand Up @@ -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.
Expand All @@ -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 = '';

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -323,7 +329,6 @@ var
return method.apply('', args);
}

/** @constructor */
function wNumb ( options ) {

if ( !(this instanceof wNumb) ) {
Expand All @@ -347,7 +352,6 @@ var
};
}

/** @export */
return wNumb;

}));

0 comments on commit 725bc8e

Please # to comment.