Adding vitest for testing
Currently implemented - FormatBytes - MathHelpers
This commit is contained in:
@@ -9,12 +9,16 @@ export { formatBytes, formatBytesLong, stringByteFormat };
|
||||
/**
|
||||
* converts a int number into bytes with prefix in two decimals precision
|
||||
* currently precision is fixed, if dynamic needs check for max/min precision
|
||||
* @param {Number} bytes bytes in int
|
||||
* @param {Number|BigInt} bytes bytes in int
|
||||
* @return {String} string in GB/MB/KB
|
||||
*/
|
||||
function formatBytes(bytes)
|
||||
{
|
||||
var i = -1;
|
||||
// If this ia bigint -> convert to number, we need the decimals
|
||||
if (typeof bytes === "bigint") {
|
||||
bytes = Number(bytes);
|
||||
}
|
||||
do {
|
||||
bytes = bytes / 1024;
|
||||
i++;
|
||||
@@ -26,17 +30,26 @@ function formatBytes(bytes)
|
||||
|
||||
/**
|
||||
* like formatBytes, but returns bytes for <1KB and not 0.n KB
|
||||
* @param {Number} bytes bytes in int
|
||||
* @param {Number|BigInt} bytes bytes in int
|
||||
* @return {String} string in GB/MB/KB
|
||||
*/
|
||||
function formatBytesLong(bytes)
|
||||
{
|
||||
// If this ia bigint -> convert to number, we need the decimals
|
||||
if (typeof bytes === "bigint") {
|
||||
bytes = Number(bytes);
|
||||
}
|
||||
if (isNaN(bytes)) {
|
||||
return bytes.toString();
|
||||
}
|
||||
let negative = false;
|
||||
if (bytes < 0) {
|
||||
negative = true;
|
||||
bytes *= -1;
|
||||
}
|
||||
var i = Math.floor(Math.log(bytes) / Math.log(1024));
|
||||
var sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
||||
return (
|
||||
return (negative ? '-' : '') + (
|
||||
(
|
||||
bytes /
|
||||
Math.pow(1024, i)
|
||||
@@ -49,9 +62,10 @@ function formatBytesLong(bytes)
|
||||
/**
|
||||
* Convert a string with B/K/M/etc into a byte number
|
||||
* @param {String|Number|Object} bytes Any string with B/K/M/etc
|
||||
* @param {Boolean} raw [default=false] Return not rounded values
|
||||
* @return {String|Number} A byte number, or original string as is
|
||||
*/
|
||||
function stringByteFormat(bytes)
|
||||
function stringByteFormat(bytes, raw=false)
|
||||
{
|
||||
// if anything not string return
|
||||
if (!(typeof bytes === 'string' || bytes instanceof String)) {
|
||||
@@ -74,7 +88,11 @@ function stringByteFormat(bytes)
|
||||
bytes = m1 * Math.pow(1024, valid_units.indexOf(m2));
|
||||
}
|
||||
}
|
||||
return bytes;
|
||||
// if we want to have the raw data returned
|
||||
if (raw) {
|
||||
return bytes;
|
||||
}
|
||||
return Math.round(bytes);
|
||||
}
|
||||
|
||||
// __END__
|
||||
|
||||
@@ -10,11 +10,11 @@ export { dec2hex, getRandomIntInclusive, roundPrecision };
|
||||
* dec2hex :: Integer -> String
|
||||
* i.e. 0-255 -> '00'-'ff'
|
||||
* @param {Number} dec decimal string
|
||||
* @return {String} hex encdoded number
|
||||
* @return {String} hex encdoded number, prefix with 0x
|
||||
*/
|
||||
function dec2hex(dec)
|
||||
{
|
||||
return ('0' + dec.toString(16)).substring(-2);
|
||||
return ('0x' + dec.toString(16)).substring(-2);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -22,7 +22,7 @@ function dec2hex(dec)
|
||||
* with min/max inclusive.
|
||||
* eg: 1,5 will create a number ranging from 1 o 5
|
||||
* @param {Number} min minimum int number inclusive
|
||||
* @param {Number} max maximumg int number inclusive
|
||||
* @param {Number} max maximum int number inclusive
|
||||
* @return {Number} Random number
|
||||
*/
|
||||
function getRandomIntInclusive(min, max)
|
||||
@@ -41,7 +41,7 @@ function getRandomIntInclusive(min, max)
|
||||
*/
|
||||
function roundPrecision(number, precision)
|
||||
{
|
||||
if (!isNaN(number) || !isNaN(precision)) {
|
||||
if (isNaN(number) || isNaN(precision)) {
|
||||
return number;
|
||||
}
|
||||
return Math.round(number * Math.pow(10, precision)) / Math.pow(10, precision);
|
||||
|
||||
Reference in New Issue
Block a user