%PDF- %PDF-
Direktori : /home/opti/workflow/node_modules/numeral/src/formats/ |
Current File : /home/opti/workflow/node_modules/numeral/src/formats/bytes.js |
// numeral.js format configuration // format : bytes // author : Adam Draper : https://github.com/adamwdraper (function (global, factory) { if (typeof define === 'function' && define.amd) { define(['../numeral'], factory); } else if (typeof module === 'object' && module.exports) { factory(require('../numeral')); } else { factory(global.numeral); } }(this, function (numeral) { var decimal = { base: 1000, suffixes: ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] }, binary = { base: 1024, suffixes: ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'] }; var allSuffixes = decimal.suffixes.concat(binary.suffixes.filter(function (item) { return decimal.suffixes.indexOf(item) < 0; })); var unformatRegex = allSuffixes.join('|'); // Allow support for BPS (http://www.investopedia.com/terms/b/basispoint.asp) unformatRegex = '(' + unformatRegex.replace('B', 'B(?!PS)') + ')'; numeral.register('format', 'bytes', { regexps: { format: /([0\s]i?b)/, unformat: new RegExp(unformatRegex) }, format: function(value, format, roundingFunction) { var output, bytes = numeral._.includes(format, 'ib') ? binary : decimal, suffix = numeral._.includes(format, ' b') || numeral._.includes(format, ' ib') ? ' ' : '', power, min, max; // check for space before format = format.replace(/\s?i?b/, ''); for (power = 0; power <= bytes.suffixes.length; power++) { min = Math.pow(bytes.base, power); max = Math.pow(bytes.base, power + 1); if (value === null || value === 0 || value >= min && value < max) { suffix += bytes.suffixes[power]; if (min > 0) { value = value / min; } break; } } output = numeral._.numberToFormat(value, format, roundingFunction); return output + suffix; }, unformat: function(string) { var value = numeral._.stringToNumber(string), power, bytesMultiplier; if (value) { for (power = decimal.suffixes.length - 1; power >= 0; power--) { if (numeral._.includes(string, decimal.suffixes[power])) { bytesMultiplier = Math.pow(decimal.base, power); break; } if (numeral._.includes(string, binary.suffixes[power])) { bytesMultiplier = Math.pow(binary.base, power); break; } } value *= (bytesMultiplier || 1); } return value; } }); }));