* chore(deps-dev): bump eslint-plugin-unicorn from 64.0.0 to 65.0.0 Bumps [eslint-plugin-unicorn](https://github.com/sindresorhus/eslint-plugin-unicorn) from 64.0.0 to 65.0.0. - [Release notes](https://github.com/sindresorhus/eslint-plugin-unicorn/releases) - [Commits](https://github.com/sindresorhus/eslint-plugin-unicorn/compare/v64.0.0...v65.0.0) --- updated-dependencies: - dependency-name: eslint-plugin-unicorn dependency-version: 65.0.0 dependency-type: direct:development update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com> * fix: no-array-from-fill * fix: unicorn-js/no-duplicate-set-values * fix: no-this-outside-of-class * fix: no-unnecessary-nested-ternary * fix: prefer-array-last-methods * fix: prefer-queue-microtask * fix: prefer-split-limit * fix: no-array-from-fill * fix: no-unused-array-method-return * fix: prefer-single-call * fix: prefer-includes-over-repeated-comparisons * fix: consistent-compound-words * fix: prefer-includes-over-repeated-comparisons * chore: add eslint-plugin-unicorn v65 recommended preset * fix: correct duration parsing logic in parseDuration function * fix: update oxlint-disable --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Tony <TonyRL@users.noreply.github.com>
63 lines
2.0 KiB
TypeScript
63 lines
2.0 KiB
TypeScript
import path from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
import { stringifyQuery } from 'ufo';
|
|
|
|
export const getRouteNameFromPath = (path: string) => {
|
|
const p = path.split('/').filter(Boolean);
|
|
if (p.length > 0) {
|
|
return p[0];
|
|
}
|
|
return null;
|
|
};
|
|
|
|
export const getPath = (request: Request): string => {
|
|
// Optimized: RegExp is faster than indexOf() + slice()
|
|
const match = request.url.match(/^https?:\/\/[^/]+(\/[^?]*)/);
|
|
return match ? match[1] : '';
|
|
};
|
|
|
|
const humanize = (times: string[]) => {
|
|
const [delimiter, separator] = [',', '.'];
|
|
const orderTimes = times.map((v) => v.replaceAll(/(\d)(?=(\d{3})+(?!\d))/g, '$1' + delimiter));
|
|
return orderTimes.join(separator);
|
|
};
|
|
|
|
export const time = (start: number) => {
|
|
const delta = Date.now() - start;
|
|
return humanize([delta < 1000 ? delta + 'ms' : Math.round(delta / 1000) + 's']);
|
|
};
|
|
|
|
export const getCurrentPath = (metaUrl: string) => {
|
|
const __filename = path.join(fileURLToPath(metaUrl));
|
|
return path.dirname(__filename);
|
|
};
|
|
|
|
function isPureObject(o: any) {
|
|
return Object.prototype.toString.call(o) === '[object Object]';
|
|
}
|
|
|
|
export function getSearchParamsString(searchParams: any) {
|
|
const searchParamsString = isPureObject(searchParams) ? stringifyQuery(searchParams) : null;
|
|
return searchParamsString ?? new URLSearchParams(searchParams).toString();
|
|
}
|
|
|
|
/**
|
|
* parse duration string to seconds
|
|
* @param {string} timeStr - duration string like "01:01:01" / "01:01" / "59"
|
|
* @returns {number} - total seconds
|
|
*/
|
|
export function parseDuration(timeStr: string | undefined | null): number | undefined {
|
|
if (!timeStr) {
|
|
return;
|
|
}
|
|
const clean = timeStr.trim().replaceAll(/[^\d:]/g, '');
|
|
return clean.split(':').reduceRight((total, part, idx, arr) => {
|
|
const n = Number(part);
|
|
if (Number.isNaN(n)) {
|
|
throw new TypeError(`Invalid segment: ${part}`);
|
|
}
|
|
return total + n * Math.pow(60, arr.length - 1 - idx);
|
|
}, 0);
|
|
}
|