Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
b473ade178 | |||
b3223661dd | |||
c36af83918 |
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.26.14",
|
"version": "0.26.17",
|
||||||
"description": "Utility library",
|
"description": "Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
@ -78,7 +78,7 @@ export class Http {
|
|||||||
if(!this.url && !opts.url) throw new Error('URL needs to be set');
|
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 || '');
|
let url = opts.url?.startsWith('http') ? opts.url : (this.url || '') + (opts.url || '');
|
||||||
url = url.replaceAll(/([^:]\/)\/+/g, '$1');
|
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) {
|
if(opts.query) {
|
||||||
const q = Array.isArray(opts.query) ? opts.query :
|
const q = Array.isArray(opts.query) ? opts.query :
|
||||||
Object.keys(opts.query).map(k => ({key: k, value: (<any>opts.query)[k]}))
|
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 [];
|
if(!rows) return [];
|
||||||
return rows.filter(r => {
|
return rows.filter(r => {
|
||||||
// Empty search
|
// Empty search
|
||||||
const value = transform(r);
|
|
||||||
if(!search) return true;
|
if(!search) return true;
|
||||||
|
const value = transform(r);
|
||||||
// Regex search
|
// Regex search
|
||||||
if(regex) {
|
if(regex) {
|
||||||
return !!Object.values(value).filter((v: any) => {
|
return !!Object.values(value).filter((v: any) => {
|
||||||
@ -22,7 +22,7 @@ export function search(rows: any[], search: string, regex?: boolean, transform:
|
|||||||
catch { return false; }
|
catch { return false; }
|
||||||
}).length
|
}).length
|
||||||
} else {
|
} else {
|
||||||
return logicTest(r, search);
|
return logicTest(value, search);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -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