Skip to content

Commit 4765828

Browse files
committed
feat: customizable humanize
1 parent 26fad6a commit 4765828

File tree

8 files changed

+11
-28
lines changed

8 files changed

+11
-28
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ A lightweight JavaScript debugging utility, forked from [debug](https://www.npmj
2121
- 🚀 Native ESM compatibility
2222
- 🌐 Optimized for modern runtimes
2323
- ES2015+ browsers
24-
- Node.js 18 and above
24+
- Modern Node.js versions
25+
- 🎨 Customizable formatting
2526

2627
## Installation
2728

eslint.config.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ import { sxzz } from '@sxzz/eslint-config'
33
export default sxzz().append({
44
// @keep-sorted
55
rules: {
6-
'import/no-default-export': 'off',
76
'no-console': 'off',
87
'node/prefer-global/process': 'off',
98
'unicorn/no-array-sort': 'off',
10-
'unicorn/prefer-global-this': 'off',
119
'unicorn/prefer-string-replace-all': 'off',
1210
},
1311
})

package.json

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,6 @@
4949
"release": "bumpp",
5050
"prepublishOnly": "pnpm run build"
5151
},
52-
"peerDependencies": {
53-
"ms": "^2.0.0"
54-
},
55-
"peerDependenciesMeta": {
56-
"ms": {
57-
"optional": true
58-
}
59-
},
6052
"devDependencies": {
6153
"@sxzz/eslint-config": "^7.3.0",
6254
"@sxzz/prettier-config": "^2.2.5",

pnpm-lock.yaml

Lines changed: 0 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/browser.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ function formatArgs(
102102
(useColors ? ' %c' : ' ') +
103103
args[0] +
104104
(useColors ? '%c ' : ' ')
105-
}+${humanize(diff)}`
105+
}+${this.humanize(diff)}`
106106

107107
if (!useColors) {
108108
return
@@ -160,6 +160,7 @@ const defaultOptions: Omit<Required<DebugOptions>, 'color'> = {
160160
},
161161
},
162162
inspectOpts: {},
163+
humanize,
163164

164165
log,
165166
}

src/core.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export function createDebug(
6767
formatters: this.formatters,
6868
inspectOpts: this.inspectOpts,
6969
log: this.log,
70+
humanize: this.humanize,
7071
})
7172
}
7273
Object.assign(debug, options)
@@ -101,6 +102,7 @@ export function createDebug(
101102
debug.inspectOpts = {}
102103
debug.log = () => {}
103104
debug.enabled = false
105+
debug.humanize = String
104106
}
105107

106108
return debug

src/node.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { createRequire } from 'node:module'
21
import { isatty } from 'node:tty'
32
import { formatWithOptions, inspect } from 'node:util'
43
import {
@@ -8,11 +7,9 @@ import {
87
enabled,
98
namespaces,
109
} from './core.ts'
11-
import { humanize as _humanize, selectColor } from './utils.ts'
10+
import { humanize, selectColor } from './utils.ts'
1211
import type { Debugger, DebugOptions, InspectOptions } from './types.ts'
1312

14-
const require = createRequire(import.meta.url)
15-
1613
const colors: number[] =
1714
process.stderr.getColorDepth && process.stderr.getColorDepth() > 2
1815
? [
@@ -63,13 +60,6 @@ function useColors(): boolean {
6360
: isatty(process.stderr.fd)
6461
}
6562

66-
let humanize: typeof _humanize
67-
try {
68-
humanize = require('ms')
69-
} catch {
70-
humanize = _humanize
71-
}
72-
7363
function getDate(): string {
7464
if (inspectOpts.hideDate) {
7565
return ''
@@ -93,7 +83,7 @@ function formatArgs(
9383
const prefix = ` ${colorCode};1m${name} \u001B[0m`
9484

9585
args[0] = prefix + args[0].split('\n').join(`\n${prefix}`)
96-
args.push(`${colorCode}m+${humanize(diff)}\u001B[0m`)
86+
args.push(`${colorCode}m+${this.humanize(diff)}\u001B[0m`)
9787
} else {
9888
args[0] = `${getDate()}${name} ${args[0]}`
9989
}
@@ -130,6 +120,7 @@ const defaultOptions: Omit<Required<DebugOptions>, 'color'> = {
130120
inspectOpts,
131121

132122
log,
123+
humanize,
133124
}
134125

135126
export function createDebug(

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ export interface DebugOptions {
3030
formatters?: Formatters
3131
/** Node.js only */
3232
inspectOpts?: InspectOptions
33+
/** Humanize a duration in milliseconds */
34+
humanize?: (value: number) => string
3335

3436
log?: (this: Debugger, ...args: any[]) => void
3537
}

0 commit comments

Comments
 (0)