aboutsummaryrefslogtreecommitdiff
path: root/node_modules/bytes/index.js
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2018-09-20 02:56:13 +0200
committerFlorian Dold <florian.dold@gmail.com>2018-09-20 02:56:13 +0200
commitbbff7403fbf46f9ad92240ac213df8d30ef31b64 (patch)
treec58400ec5124da1c7d56b01aea83309f80a56c3b /node_modules/bytes/index.js
parent003fb34971cf63466184351b4db5f7c67df4f444 (diff)
update packages
Diffstat (limited to 'node_modules/bytes/index.js')
-rw-r--r--node_modules/bytes/index.js174
1 files changed, 146 insertions, 28 deletions
diff --git a/node_modules/bytes/index.js b/node_modules/bytes/index.js
index 02bd98f0d..1e39afd1d 100644
--- a/node_modules/bytes/index.js
+++ b/node_modules/bytes/index.js
@@ -1,41 +1,159 @@
+/*!
+ * bytes
+ * Copyright(c) 2012-2014 TJ Holowaychuk
+ * Copyright(c) 2015 Jed Watson
+ * MIT Licensed
+ */
+
+'use strict';
/**
- * Parse byte `size` string.
- *
- * @param {String} size
- * @return {Number}
- * @api public
+ * Module exports.
+ * @public
+ */
+
+module.exports = bytes;
+module.exports.format = format;
+module.exports.parse = parse;
+
+/**
+ * Module variables.
+ * @private
*/
-module.exports = function(size) {
- if ('number' == typeof size) return convert(size);
- var parts = size.match(/^(\d+(?:\.\d+)?) *(kb|mb|gb|tb)$/)
- , n = parseFloat(parts[1])
- , type = parts[2];
+var formatThousandsRegExp = /\B(?=(\d{3})+(?!\d))/g;
- var map = {
- kb: 1 << 10
- , mb: 1 << 20
- , gb: 1 << 30
- , tb: ((1 << 30) * 1024)
- };
+var formatDecimalsRegExp = /(?:\.0*|(\.[^0]+)0+)$/;
- return map[type] * n;
+var map = {
+ b: 1,
+ kb: 1 << 10,
+ mb: 1 << 20,
+ gb: 1 << 30,
+ tb: ((1 << 30) * 1024)
};
+var parseRegExp = /^((-|\+)?(\d+(?:\.\d+)?)) *(kb|mb|gb|tb)$/i;
+
/**
- * convert bytes into string.
+ * Convert the given value in bytes into a string or parse to string to an integer in bytes.
+ *
+ * @param {string|number} value
+ * @param {{
+ * case: [string],
+ * decimalPlaces: [number]
+ * fixedDecimals: [boolean]
+ * thousandsSeparator: [string]
+ * unitSeparator: [string]
+ * }} [options] bytes options.
+ *
+ * @returns {string|number|null}
+ */
+
+function bytes(value, options) {
+ if (typeof value === 'string') {
+ return parse(value);
+ }
+
+ if (typeof value === 'number') {
+ return format(value, options);
+ }
+
+ return null;
+}
+
+/**
+ * Format the given value in bytes into a string.
+ *
+ * If the value is negative, it is kept as such. If it is a float,
+ * it is rounded.
+ *
+ * @param {number} value
+ * @param {object} [options]
+ * @param {number} [options.decimalPlaces=2]
+ * @param {number} [options.fixedDecimals=false]
+ * @param {string} [options.thousandsSeparator=]
+ * @param {string} [options.unit=]
+ * @param {string} [options.unitSeparator=]
*
- * @param {Number} b - bytes to convert
- * @return {String}
- * @api public
+ * @returns {string|null}
+ * @public
*/
-function convert (b) {
- var tb = ((1 << 30) * 1024), gb = 1 << 30, mb = 1 << 20, kb = 1 << 10;
- if (b >= tb) return (Math.round(b / tb * 100) / 100) + 'tb';
- if (b >= gb) return (Math.round(b / gb * 100) / 100) + 'gb';
- if (b >= mb) return (Math.round(b / mb * 100) / 100) + 'mb';
- if (b >= kb) return (Math.round(b / kb * 100) / 100) + 'kb';
- return b + 'b';
+function format(value, options) {
+ if (!Number.isFinite(value)) {
+ return null;
+ }
+
+ var mag = Math.abs(value);
+ var thousandsSeparator = (options && options.thousandsSeparator) || '';
+ var unitSeparator = (options && options.unitSeparator) || '';
+ var decimalPlaces = (options && options.decimalPlaces !== undefined) ? options.decimalPlaces : 2;
+ var fixedDecimals = Boolean(options && options.fixedDecimals);
+ var unit = (options && options.unit) || '';
+
+ if (!unit || !map[unit.toLowerCase()]) {
+ if (mag >= map.tb) {
+ unit = 'TB';
+ } else if (mag >= map.gb) {
+ unit = 'GB';
+ } else if (mag >= map.mb) {
+ unit = 'MB';
+ } else if (mag >= map.kb) {
+ unit = 'KB';
+ } else {
+ unit = 'B';
+ }
+ }
+
+ var val = value / map[unit.toLowerCase()];
+ var str = val.toFixed(decimalPlaces);
+
+ if (!fixedDecimals) {
+ str = str.replace(formatDecimalsRegExp, '$1');
+ }
+
+ if (thousandsSeparator) {
+ str = str.replace(formatThousandsRegExp, thousandsSeparator);
+ }
+
+ return str + unitSeparator + unit;
+}
+
+/**
+ * Parse the string value into an integer in bytes.
+ *
+ * If no unit is given, it is assumed the value is in bytes.
+ *
+ * @param {number|string} val
+ *
+ * @returns {number|null}
+ * @public
+ */
+
+function parse(val) {
+ if (typeof val === 'number' && !isNaN(val)) {
+ return val;
+ }
+
+ if (typeof val !== 'string') {
+ return null;
+ }
+
+ // Test if the string passed is valid
+ var results = parseRegExp.exec(val);
+ var floatValue;
+ var unit = 'b';
+
+ if (!results) {
+ // Nothing could be extracted from the given string
+ floatValue = parseInt(val, 10);
+ unit = 'b'
+ } else {
+ // Retrieve the value and the unit
+ floatValue = parseFloat(results[1]);
+ unit = results[4].toLowerCase();
+ }
+
+ return Math.floor(map[unit] * floatValue);
}