|
1 | 1 | /*! @preserve |
2 | 2 | * numeral.js |
3 | | - * version : 1.5.4 |
| 3 | + * version : 1.5.5 |
4 | 4 | * author : Adam Draper |
5 | 5 | * license : MIT |
6 | 6 | * http://adamwdraper.github.com/Numeral-js/ |
|
13 | 13 | ************************************/ |
14 | 14 |
|
15 | 15 | var numeral, |
16 | | - VERSION = '1.5.4', |
| 16 | + VERSION = '1.5.5', |
17 | 17 | // internal storage for language config files |
18 | 18 | languages = {}, |
19 | 19 | defaults = { |
|
105 | 105 | millionRegExp, |
106 | 106 | billionRegExp, |
107 | 107 | 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'], |
110 | 110 | bytesMultiplier = false, |
111 | 111 | power; |
112 | 112 |
|
|
127 | 127 | trillionRegExp = new RegExp('[^a-zA-Z]' + languages[options.currentLanguage].abbreviations.trillion + '(?:\\)|(\\' + languages[options.currentLanguage].currency.symbol + ')?(?:\\))?)?$'); |
128 | 128 |
|
129 | 129 | // 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 | + } |
134 | 136 | } |
135 | 137 |
|
136 | 138 | // do some math to create our number |
|
245 | 247 | return Number(seconds); |
246 | 248 | } |
247 | 249 |
|
| 250 | + /* format keys: |
| 251 | + * a - abbreviation |
| 252 | + * ib - binary bytes |
| 253 | + * b - decimal bytes |
| 254 | + * o - ordinal |
| 255 | + */ |
248 | 256 | function formatNumber(value, format, roundingFunction) { |
249 | 257 | var negP = false, |
250 | 258 | signed = false, |
|
258 | 266 | bytes = '', |
259 | 267 | ord = '', |
260 | 268 | 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'], |
263 | 271 | min, |
264 | 272 | max, |
265 | 273 | power, |
266 | 274 | w, |
267 | 275 | precision, |
268 | 276 | thousands, |
269 | 277 | d = '', |
270 | | - neg = false; |
| 278 | + neg = false, |
| 279 | + iecBinary = false; |
271 | 280 |
|
272 | 281 | // check if number is zero and a custom zero format has been set |
273 | 282 | if (value === 0 && options.zeroFormat !== null) { |
|
320 | 329 | } |
321 | 330 | } |
322 | 331 |
|
323 | | - // see if we are formatting binary bytes |
| 332 | + // see if we are formatting bytes |
324 | 333 | 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); |
336 | 334 |
|
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; |
344 | 338 | } |
345 | | - } |
346 | 339 |
|
347 | | - // see if we are formatting decimal bytes |
348 | | - if (format.indexOf('d') > -1) { |
349 | 340 | // check for space before |
350 | | - if (format.indexOf(' d') > -1) { |
| 341 | + if (format.indexOf(' b') > -1 || format.indexOf(' ib') > -1) { |
351 | 342 | bytes = ' '; |
352 | | - format = format.replace(' d', ''); |
| 343 | + format = format.replace(' ib', '').replace(' b', ''); |
353 | 344 | } else { |
354 | | - format = format.replace('d', ''); |
| 345 | + format = format.replace('ib', '').replace('b', ''); |
355 | 346 | } |
356 | 347 |
|
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); |
360 | 351 |
|
361 | 352 | if (value >= min && value < max) { |
362 | | - bytes = bytes + decimalSuffixes[power]; |
| 353 | + bytes = bytes + (iecBinary ? iecSuffixes[power] : suffixes[power]); |
363 | 354 | if (min > 0) { |
364 | 355 | value = value / min; |
365 | 356 | } |
|
0 commit comments