Skip to content

Commit b50f4c9

Browse files
authored
Merge pull request #378 from adamwdraper/fix-bytes-for-legacy
Fix bytes for legacy
2 parents 0b027ac + 63ecec9 commit b50f4c9

File tree

8 files changed

+52
-57
lines changed

8 files changed

+52
-57
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,15 @@ See [the english unit tests](https://github.com/adamwdraper/Numeral-js/blob/mast
4747

4848
# Changelog
4949

50+
### 1.5.5
51+
52+
Bug fix: Switch bytes back to `b` and change iecBinary to `ib`, and calculate both using 1024 for backwards compatibility
53+
5054
### 1.5.4
5155

5256
Tests: Changed all tests to use Mocha and Chai
5357

54-
Tests: Added browser tests for Chrome, Firefox, and IE using saucelabs
58+
Tests: Added browser tests for Chrome, Firefox, and IE using saucelabs
5559

5660
Added reset function to reset numeral to default options
5761

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "numeral",
33
"repo": "adamwdraper/Numeral-js",
4-
"version": "1.5.4",
4+
"version": "1.5.5",
55
"description": "Format and manipulate numbers.",
66
"keywords": [
77
"numeral",

component.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "numeral",
33
"repo": "adamwdraper/Numeral-js",
4-
"version": "1.5.4",
4+
"version": "1.5.5",
55
"description": "Format and manipulate numbers.",
66
"keywords": [
77
"numeral",

min/numeral.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

numeral.js

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*! @preserve
22
* numeral.js
3-
* version : 1.5.4
3+
* version : 1.5.5
44
* author : Adam Draper
55
* license : MIT
66
* http://adamwdraper.github.com/Numeral-js/
@@ -13,7 +13,7 @@
1313
************************************/
1414

1515
var numeral,
16-
VERSION = '1.5.4',
16+
VERSION = '1.5.5',
1717
// internal storage for language config files
1818
languages = {},
1919
defaults = {
@@ -105,8 +105,8 @@
105105
millionRegExp,
106106
billionRegExp,
107107
trillionRegExp,
108-
binarySuffixes = ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'],
109-
decimalSuffixes = ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
108+
suffixes = ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
109+
iecSuffixes = ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'],
110110
bytesMultiplier = false,
111111
power;
112112

@@ -127,10 +127,12 @@
127127
trillionRegExp = new RegExp('[^a-zA-Z]' + languages[options.currentLanguage].abbreviations.trillion + '(?:\\)|(\\' + languages[options.currentLanguage].currency.symbol + ')?(?:\\))?)?$');
128128

129129
// see if bytes are there so that we can multiply to the correct number
130-
for (power = 0; power <= binarySuffixes.length && !bytesMultiplier; power++) {
131-
if (string.indexOf(binarySuffixes[power]) > -1) { bytesMultiplier = Math.pow(1024, power + 1); }
132-
else if (string.indexOf(decimalSuffixes[power]) > -1) { bytesMultiplier = Math.pow(1000, power + 1); }
133-
else { bytesMultiplier = false; }
130+
for (power = 0; power <= suffixes.length; power++) {
131+
bytesMultiplier = ((string.indexOf(suffixes[power]) > -1) || (string.indexOf(iecSuffixes[power]) > -1))? Math.pow(1024, power + 1) : false;
132+
133+
if (bytesMultiplier) {
134+
break;
135+
}
134136
}
135137

136138
// do some math to create our number
@@ -245,6 +247,12 @@
245247
return Number(seconds);
246248
}
247249

250+
/* format keys:
251+
* a - abbreviation
252+
* ib - binary bytes
253+
* b - decimal bytes
254+
* o - ordinal
255+
*/
248256
function formatNumber(value, format, roundingFunction) {
249257
var negP = false,
250258
signed = false,
@@ -258,16 +266,17 @@
258266
bytes = '',
259267
ord = '',
260268
abs = Math.abs(value),
261-
binarySuffixes = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'],
262-
decimalSuffixes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
269+
iecSuffixes = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'],
270+
suffixes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
263271
min,
264272
max,
265273
power,
266274
w,
267275
precision,
268276
thousands,
269277
d = '',
270-
neg = false;
278+
neg = false,
279+
iecBinary = false;
271280

272281
// check if number is zero and a custom zero format has been set
273282
if (value === 0 && options.zeroFormat !== null) {
@@ -320,46 +329,28 @@
320329
}
321330
}
322331

323-
// see if we are formatting binary bytes
332+
// see if we are formatting bytes
324333
if (format.indexOf('b') > -1) {
325-
// check for space before
326-
if (format.indexOf(' b') > -1) {
327-
bytes = ' ';
328-
format = format.replace(' b', '');
329-
} else {
330-
format = format.replace('b', '');
331-
}
332-
333-
for (power = 0; power <= binarySuffixes.length; power++) {
334-
min = Math.pow(1024, power);
335-
max = Math.pow(1024, power + 1);
336334

337-
if (value >= min && value < max) {
338-
bytes = bytes + binarySuffixes[power];
339-
if (min > 0) {
340-
value = value / min;
341-
}
342-
break;
343-
}
335+
// check for IEC Binary byte notation
336+
if (format.indexOf('ib') > -1) {
337+
iecBinary = true;
344338
}
345-
}
346339

347-
// see if we are formatting decimal bytes
348-
if (format.indexOf('d') > -1) {
349340
// check for space before
350-
if (format.indexOf(' d') > -1) {
341+
if (format.indexOf(' b') > -1 || format.indexOf(' ib') > -1) {
351342
bytes = ' ';
352-
format = format.replace(' d', '');
343+
format = format.replace(' ib', '').replace(' b', '');
353344
} else {
354-
format = format.replace('d', '');
345+
format = format.replace('ib', '').replace('b', '');
355346
}
356347

357-
for (power = 0; power <= decimalSuffixes.length; power++) {
358-
min = Math.pow(1000, power);
359-
max = Math.pow(1000, power+1);
348+
for (power = 0; power <= suffixes.length; power++) {
349+
min = Math.pow(1024, power);
350+
max = Math.pow(1024, power+1);
360351

361352
if (value >= min && value < max) {
362-
bytes = bytes + decimalSuffixes[power];
353+
bytes = bytes + (iecBinary ? iecSuffixes[power] : suffixes[power]);
363354
if (min > 0) {
364355
value = value / min;
365356
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "numeral",
3-
"version": "1.5.4",
3+
"version": "1.5.5",
44
"description": "Format and manipulate numbers.",
55
"homepage": "http://numeraljs.com",
66
"author": {

tests/numeral/format.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,16 @@ describe('Format', function() {
133133
it('should format to bytes', function() {
134134
var tests = [
135135
[100,'0b','100B'],
136-
[1024*2,'0 b','2 KiB'],
137-
[1024*1024*5,'0b','5MiB'],
138-
[1024*1024*1024*7.343,'0.[0] b','7.3 GiB'],
139-
[1024*1024*1024*1024*3.1536544,'0.000b','3.154TiB'],
140-
[1024*1024*1024*1024*1024*2.953454534534,'0b','3PiB'],
141-
[1000*2,'0 d','2 KB'],
142-
[1000*1000*5,'0d','5MB'],
143-
[1000*1000*1000*7.343,'0.[0] d','7.3 GB'],
144-
[1000*1000*1000*1000*3.1536544,'0.000d','3.154TB'],
145-
[1000*1000*1000*1000*1000*2.953454534534,'0d','3PB']
136+
[1024*2,'0 ib','2 KiB'],
137+
[1024*1024*5,'0ib','5MiB'],
138+
[1024*1024*1024*7.343,'0.[0] ib','7.3 GiB'],
139+
[1024*1024*1024*1024*3.1536544,'0.000ib','3.154TiB'],
140+
[1024*1024*1024*1024*1024*2.953454534534,'0ib','3PiB'],
141+
[1024*2,'0 b','2 KB'],
142+
[1024*1024*5,'0b','5MB'],
143+
[1024*1024*1024*7.343,'0.[0] b','7.3 GB'],
144+
[1024*1024*1024*1024*3.1536544,'0.000b','3.154TB'],
145+
[1024*1024*1024*1024*1024*2.953454534534,'0b','3PB']
146146
],
147147
i;
148148

tests/numeral/unformat.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ describe('Unformat', function() {
6060
var tests = [
6161
['100B', 100],
6262
['3.154 TiB', 3467859674006],
63-
['3.154 TB', 3154000000000]
63+
['3.154 TB', 3467859674006]
6464
];
6565

6666
for (var i = 0; i < tests.length; i++) {

0 commit comments

Comments
 (0)