Added format milliseconds method
This commit is contained in:
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.26.16",
|
"version": "0.26.17",
|
||||||
"description": "Utility library",
|
"description": "Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
@ -43,6 +43,24 @@ export function formatBytes(bytes: number, decimals = 2) {
|
|||||||
return parseFloat((bytes / Math.pow(k, i)).toFixed(decimals)) + ' ' + sizes[i];
|
return parseFloat((bytes / Math.pow(k, i)).toFixed(decimals)) + ' ' + sizes[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert milliseconds to human-readable duration
|
||||||
|
* @param {string} number milliseconds
|
||||||
|
* @return {string} formated duration
|
||||||
|
*/
|
||||||
|
export function formatMs(number: string): string {
|
||||||
|
const ms = parseInt(number, 10);
|
||||||
|
if (isNaN(ms) || ms < 0) return "Invalid input";
|
||||||
|
const seconds = ms / 1000;
|
||||||
|
const minutes = seconds / 60;
|
||||||
|
const hours = minutes / 60;
|
||||||
|
const days = hours / 24;
|
||||||
|
if (days >= 1) return `${days.toFixed(1)} days`;
|
||||||
|
else if (hours >= 1) return `${hours.toFixed(1)} hours`;
|
||||||
|
else if (minutes >= 1) return `${minutes.toFixed(1)} minutes`;
|
||||||
|
else return `${seconds.toFixed(1)} seconds`;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extract numbers from a string & create a formated phone number: +1 (123) 456-7890
|
* Extract numbers from a string & create a formated phone number: +1 (123) 456-7890
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user