Skip to content
This repository was archived by the owner on Jul 26, 2023. It is now read-only.

Commit

Permalink
Add support for omitting abbreviation or % sign
Browse files Browse the repository at this point in the history
When formatting numbers, bytes, or percents, adding ~ to the end of the format
will omit the abbreviation symbol or the % sign.

Closes #1
  • Loading branch information
Phil Nachum committed Oct 12, 2015
1 parent 20149c2 commit 8d18ad6
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 31 deletions.
56 changes: 36 additions & 20 deletions numeral.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,9 @@
function formatPercentage (n, format, roundingFunction) {
var space = '',
output,
value = n._value * 100;
value = n._value * 100,
omitPercent = false,
percentSymbol = '%';

// check for space before %
if (format.indexOf(' %') > -1) {
Expand All @@ -199,14 +201,22 @@
format = format.replace('%', '');
}

omitPercent = format.indexOf('~') > -1;
format = format.replace('~', '');
if (omitPercent) {
space = '';
percentSymbol = '';
} else {
percentSymbol = '%';
}
output = formatNumber(value, format, roundingFunction);

if (output.indexOf(')') > -1 ) {
output = output.split('');
output.splice(-1, 0, space + '%');
output.splice(-1, 0, space + percentSymbol);
output = output.join('');
} else {
output = output + space + '%';
output = output + space + percentSymbol;
}

return output;
Expand Down Expand Up @@ -249,6 +259,7 @@
abbrB = false, // force abbreviation to billions
abbrT = false, // force abbreviation to trillions
abbrForce = false, // force abbreviation
omitAbbr = false,
bytes = '',
ord = '',
abs = Math.abs(value),
Expand Down Expand Up @@ -290,7 +301,9 @@
abbr = ' ';
}

format = format.replace(new RegExp(abbr + 'a[KMBT]?'), '');
omitAbbr = format.indexOf('~') > -1;

format = format.replace(new RegExp(abbr + 'a[KMBT]?~?'), '');

if (abs >= Math.pow(10, 12) && !abbrForce || abbrT) {
// trillion
Expand Down Expand Up @@ -321,6 +334,10 @@
format = format.replace('b', '');
}

omitAbbr = format.indexOf('~') > -1;

format = format.replace(new RegExp('~'), '');

for (power = 0; power <= suffixes.length; power++) {
min = Math.pow(1024, power);
max = Math.pow(1024, power+1);
Expand Down Expand Up @@ -394,8 +411,7 @@
if (format.indexOf('.') === 0) {
w = '';
}

return ((negP && neg) ? '(' : '') + ((!negP && neg) ? '-' : '') + ((!neg && signed) ? '+' : '') + w + d + ((ord) ? ord : '') + ((abbr) ? abbr : '') + ((bytes) ? bytes : '') + ((negP && neg) ? ')' : '');
return ((negP && neg) ? '(' : '') + ((!negP && neg) ? '-' : '') + ((!neg && signed) ? '+' : '') + w + d + ((ord) ? ord : '') + ((abbr && !omitAbbr) ? abbr : '') + ((bytes && !omitAbbr) ? bytes : '') + ((negP && neg) ? ')' : '');
}
}

Expand Down Expand Up @@ -444,19 +460,19 @@

return numeral;
};

// This function provides access to the loaded language data. If
// no arguments are passed in, it will simply return the current
// global language object.
numeral.languageData = function (key) {
if (!key) {
return languages[currentLanguage];
}

if (!languages[key]) {
throw new Error('Unknown language : ' + key);
}

return languages[key];
};

Expand Down Expand Up @@ -513,14 +529,14 @@
if ('function' !== typeof Array.prototype.reduce) {
Array.prototype.reduce = function (callback, opt_initialValue) {
'use strict';

if (null === this || 'undefined' === typeof this) {
// At the moment all modern browsers, that support strict mode, have
// native implementation of Array.prototype.reduce. For instance, IE8
// does not support strict mode, so this check is actually useless.
throw new TypeError('Array.prototype.reduce called on null or undefined');
}

if ('function' !== typeof callback) {
throw new TypeError(callback + ' is not a function');
}
Expand Down Expand Up @@ -554,7 +570,7 @@
};
}


/**
* Computes the multiplier necessary to make x >= 1,
* effectively eliminating miscalculations caused by
Expand All @@ -580,7 +596,7 @@
mn = multiplier(next);
return mp > mn ? mp : mn;
}, -Infinity);
}
}


/************************************
Expand All @@ -595,15 +611,15 @@
},

format : function (inputString, roundingFunction) {
return formatNumeral(this,
inputString ? inputString : defaultFormat,
return formatNumeral(this,
inputString ? inputString : defaultFormat,
(roundingFunction !== undefined) ? roundingFunction : Math.round
);
},

unformat : function (inputString) {
if (Object.prototype.toString.call(inputString) === '[object Number]') {
return inputString;
if (Object.prototype.toString.call(inputString) === '[object Number]') {
return inputString;
}
return unformatNumeral(this, inputString ? inputString : defaultFormat);
},
Expand Down Expand Up @@ -635,7 +651,7 @@
function cback(accum, curr, currI, O) {
return accum - corrFactor * curr;
}
this._value = [value].reduce(cback, this._value * corrFactor) / corrFactor;
this._value = [value].reduce(cback, this._value * corrFactor) / corrFactor;
return this;
},

Expand All @@ -654,7 +670,7 @@
var corrFactor = correctionFactor(accum, curr);
return (accum * corrFactor) / (curr * corrFactor);
}
this._value = [this._value, value].reduce(cback);
this._value = [this._value, value].reduce(cback);
return this;
},

Expand Down
49 changes: 38 additions & 11 deletions tests/numeral/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ exports.format = {
i;

test.expect(test.length);

for (i = 0; i < tests.length; i++) {
format = n.format(test[i]);
test.strictEqual(n.value(), value, 'value unchanged after format' + test[i]);
Expand Down Expand Up @@ -75,6 +75,18 @@ exports.format = {
[-5444333222111, '0,0 aB', '-5,444 b'],
[-5444333222111, '0,0 aT', '-5 t'],
[123456, '0.0[0] aK', '123.46 k'],

// omit abbreviations
[2000000000,'0.0a~','2.0'],
[1230974,'0.0a~','1.2'],
[1460,'0a~','1'],
[-104000,'0 a~','-104'],
[-5444333222111, '0,0 aK~', '-5,444,333,222'],
[-5444333222111, '0,0 aM~', '-5,444,333'],
[-5444333222111, '0,0 aB~', '-5,444'],
[-5444333222111, '0,0 aT~', '-5'],
[123456, '0.0[0] aK~', '123.46'],

],
i;

Expand All @@ -96,6 +108,7 @@ exports.format = {
[-1000.234,'(0,0$)','(1,000$)'],
[-1000.234,'$0.00','-$1000.23'],
[1230974,'($0.00 a)','$1.23 m'],
[1230974,'($0.00 a~)','$1.23'],

// test symbol position before negative sign / open parens
[-1000.234,'$ (0,0)','$ (1,000)'],
Expand Down Expand Up @@ -132,7 +145,15 @@ exports.format = {
[1024*1024*5,'0b','5MB'],
[1024*1024*1024*7.343,'0.[0] b','7.3 GB'],
[1024*1024*1024*1024*3.1536544,'0.000b','3.154TB'],
[1024*1024*1024*1024*1024*2.953454534534,'0b','3PB']
[1024*1024*1024*1024*1024*2.953454534534,'0b','3PB'],

// omit abbreviations
[100,'0b~','100'],
[1024*2,'0 b~','2'],
[1024*1024*5,'0b~','5'],
[1024*1024*1024*7.343,'0.[0] b~','7.3'],
[1024*1024*1024*1024*3.1536544,'0.000b~','3.154'],
[1024*1024*1024*1024*1024*2.953454534534,'0b~','3']
],
i;

Expand All @@ -150,7 +171,13 @@ exports.format = {
[1,'0%','100%'],
[0.974878234,'0.000%','97.488%'],
[-0.43,'0 %','-43 %'],
[0.43,'(0.00[0]%)','43.00%']
[0.43,'(0.00[0]%)','43.00%'],

// omit symbol
[1,'0%~','100'],
[0.974878234,'0.000%~','97.488'],
[-0.43,'0 %~','-43'],
[0.43,'(0.00[0]%~)','43.00']
],
i;

Expand Down Expand Up @@ -179,7 +206,7 @@ exports.format = {

test.done();
},

rounding: function (test) {
var tests = [
// value, format string, expected w/ floor, expected w/ ceil
Expand All @@ -190,19 +217,19 @@ exports.format = {
[-0.433,'0 %','-44 %', '-43 %']
],
i;

test.expect(tests.length * 2);

for (i = 0; i < tests.length; i++) {
// floor
test.strictEqual(numeral(tests[i][0]).format(tests[i][1], Math.floor), tests[i][2], tests[i][1] + ", floor");

// ceil
test.strictEqual(numeral(tests[i][0]).format(tests[i][1], Math.ceil), tests[i][3], tests[i][1] + ", ceil");
test.strictEqual(numeral(tests[i][0]).format(tests[i][1], Math.ceil), tests[i][3], tests[i][1] + ", ceil");

}

test.done();

},
};

0 comments on commit 8d18ad6

Please sign in to comment.