Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
2d2b2b8216 | |||
b473ade178 | |||
b3223661dd | |||
c36af83918 |
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ztimson/utils",
|
||||
"version": "0.26.14",
|
||||
"version": "0.26.18",
|
||||
"description": "Utility library",
|
||||
"author": "Zak Timson",
|
||||
"license": "MIT",
|
||||
|
@ -78,7 +78,7 @@ export class Http {
|
||||
if(!this.url && !opts.url) throw new Error('URL needs to be set');
|
||||
let url = opts.url?.startsWith('http') ? opts.url : (this.url || '') + (opts.url || '');
|
||||
url = url.replaceAll(/([^:]\/)\/+/g, '$1');
|
||||
if(opts.fragment) url.includes('#') ? url.replace(/#.*(\?|\n)/g, (match, arg1) => `#${opts.fragment}${arg1}`) : `${url}#${opts.fragment}`;
|
||||
if(opts.fragment) url.includes('#') ? url.replace(/#.*([?\n])/g, (match, arg1) => `#${opts.fragment}${arg1}`) : `${url}#${opts.fragment}`;
|
||||
if(opts.query) {
|
||||
const q = Array.isArray(opts.query) ? opts.query :
|
||||
Object.keys(opts.query).map(k => ({key: k, value: (<any>opts.query)[k]}))
|
||||
|
@ -13,8 +13,8 @@ export function search(rows: any[], search: string, regex?: boolean, transform:
|
||||
if(!rows) return [];
|
||||
return rows.filter(r => {
|
||||
// Empty search
|
||||
const value = transform(r);
|
||||
if(!search) return true;
|
||||
const value = transform(r);
|
||||
// Regex search
|
||||
if(regex) {
|
||||
return !!Object.values(value).filter((v: any) => {
|
||||
@ -22,7 +22,7 @@ export function search(rows: any[], search: string, regex?: boolean, transform:
|
||||
catch { return false; }
|
||||
}).length
|
||||
} else {
|
||||
return logicTest(r, search);
|
||||
return logicTest(value, search);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -43,6 +43,23 @@ export function formatBytes(bytes: number, decimals = 2) {
|
||||
return parseFloat((bytes / Math.pow(k, i)).toFixed(decimals)) + ' ' + sizes[i];
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert milliseconds to human-readable duration
|
||||
* @param {string} ms milliseconds
|
||||
* @return {string} formated duration
|
||||
*/
|
||||
export function formatMs(ms: number): string {
|
||||
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
|
||||
*
|
||||
|
Reference in New Issue
Block a user