diff --git a/README.md b/README.md index f4c70fe..f8c0b8e 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,8 @@ A lightweight JavaScript debugging utility, forked from [debug](https://www.npmj - 🚀 Native ESM compatibility - 🌐 Optimized for modern runtimes - ES2015+ browsers - - Node.js 18 and above + - Modern Node.js versions +- 🎨 Customizable formatting ## Installation diff --git a/eslint.config.js b/eslint.config.js index 765ce8d..86b931c 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -3,11 +3,9 @@ import { sxzz } from '@sxzz/eslint-config' export default sxzz().append({ // @keep-sorted rules: { - 'import/no-default-export': 'off', 'no-console': 'off', 'node/prefer-global/process': 'off', 'unicorn/no-array-sort': 'off', - 'unicorn/prefer-global-this': 'off', 'unicorn/prefer-string-replace-all': 'off', }, }) diff --git a/package.json b/package.json index 6b611cb..9519502 100644 --- a/package.json +++ b/package.json @@ -49,14 +49,6 @@ "release": "bumpp", "prepublishOnly": "pnpm run build" }, - "peerDependencies": { - "ms": "^2.0.0" - }, - "peerDependenciesMeta": { - "ms": { - "optional": true - } - }, "devDependencies": { "@sxzz/eslint-config": "^7.3.0", "@sxzz/prettier-config": "^2.2.5", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 46421ae..ce739be 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -7,10 +7,6 @@ settings: importers: .: - dependencies: - ms: - specifier: ^2.0.0 - version: 2.1.3 devDependencies: '@sxzz/eslint-config': specifier: ^7.3.0 diff --git a/src/browser.ts b/src/browser.ts index 1722edd..09ba0fa 100644 --- a/src/browser.ts +++ b/src/browser.ts @@ -102,7 +102,7 @@ function formatArgs( (useColors ? ' %c' : ' ') + args[0] + (useColors ? '%c ' : ' ') - }+${humanize(diff)}` + }+${this.humanize(diff)}` if (!useColors) { return @@ -160,6 +160,7 @@ const defaultOptions: Omit, 'color'> = { }, }, inspectOpts: {}, + humanize, log, } diff --git a/src/core.ts b/src/core.ts index a29e8b6..efd7c84 100644 --- a/src/core.ts +++ b/src/core.ts @@ -67,6 +67,7 @@ export function createDebug( formatters: this.formatters, inspectOpts: this.inspectOpts, log: this.log, + humanize: this.humanize, }) } Object.assign(debug, options) @@ -101,6 +102,7 @@ export function createDebug( debug.inspectOpts = {} debug.log = () => {} debug.enabled = false + debug.humanize = String } return debug diff --git a/src/node.ts b/src/node.ts index b75e87c..6b06fee 100644 --- a/src/node.ts +++ b/src/node.ts @@ -1,4 +1,3 @@ -import { createRequire } from 'node:module' import { isatty } from 'node:tty' import { formatWithOptions, inspect } from 'node:util' import { @@ -8,11 +7,9 @@ import { enabled, namespaces, } from './core.ts' -import { humanize as _humanize, selectColor } from './utils.ts' +import { humanize, selectColor } from './utils.ts' import type { Debugger, DebugOptions, InspectOptions } from './types.ts' -const require = createRequire(import.meta.url) - const colors: number[] = process.stderr.getColorDepth && process.stderr.getColorDepth() > 2 ? [ @@ -63,13 +60,6 @@ function useColors(): boolean { : isatty(process.stderr.fd) } -let humanize: typeof _humanize -try { - humanize = require('ms') -} catch { - humanize = _humanize -} - function getDate(): string { if (inspectOpts.hideDate) { return '' @@ -93,7 +83,7 @@ function formatArgs( const prefix = ` ${colorCode};1m${name} \u001B[0m` args[0] = prefix + args[0].split('\n').join(`\n${prefix}`) - args.push(`${colorCode}m+${humanize(diff)}\u001B[0m`) + args.push(`${colorCode}m+${this.humanize(diff)}\u001B[0m`) } else { args[0] = `${getDate()}${name} ${args[0]}` } @@ -130,6 +120,7 @@ const defaultOptions: Omit, 'color'> = { inspectOpts, log, + humanize, } export function createDebug( diff --git a/src/types.ts b/src/types.ts index 2725bfa..b288a98 100644 --- a/src/types.ts +++ b/src/types.ts @@ -30,6 +30,8 @@ export interface DebugOptions { formatters?: Formatters /** Node.js only */ inspectOpts?: InspectOptions + /** Humanize a duration in milliseconds */ + humanize?: (value: number) => string log?: (this: Debugger, ...args: any[]) => void }