Skip to content

Commit 2b01e06

Browse files
committed
perf: memoize strings
BREAKING CHANGE: remove (broken) support for empty namespace
1 parent 5a0eb45 commit 2b01e06

File tree

3 files changed

+32
-30
lines changed

3 files changed

+32
-30
lines changed

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@grammyjs/debug",
3-
"version": "0.2.2",
3+
"version": "0.3.0",
44
"exports": "./src/mod.ts",
55
"publish": {
66
"exclude": [

src/env.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ const prompts = (variable: string) =>
55

66
export const env = (variable: string): string =>
77
(!prompts(variable) && (context.process?.env ?? context.env)?.[variable]) || "";
8+
9+
export const noColor = context.Deno?.noColor ?? Boolean(env("NO_COLOR"));

src/mod.ts

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import { context } from "./types.ts";
21
import { colorNs, selectColor } from "./colors.ts";
3-
import { env } from "./env.ts";
2+
import { env, noColor } from "./env.ts";
43
import { Namespaces } from "./namespacing.ts";
4+
import { context } from "./types.ts";
55

66
const DEBUG: string = env("DEBUG");
77
const stderr = context.process?.stderr;
8-
const useColor = stderr?.isTTY && !env("NO_COLOR");
8+
const useColor = stderr?.isTTY && !noColor;
99
const console = context.console.Console?.(stderr) ?? context.console;
1010
const useAnsi = ["Node.js", "Deno", "Bun"].includes(
11-
context.navigator.userAgent.split("/", 1)[0],
11+
context.navigator.userAgent.split("/", 1)[0],
1212
);
1313

1414
/**
@@ -20,15 +20,13 @@ const namespaces: Namespaces = new Namespaces(DEBUG);
2020
* A debug logger instance.
2121
*/
2222
export interface DebugFn {
23-
(...data: unknown[]): void;
24-
/**
25-
* Manually enable or disable logging for this namespace.
26-
*/
27-
enabled: boolean;
23+
(...data: unknown[]): void;
24+
/**
25+
* Manually enable or disable logging for this namespace.
26+
*/
27+
enabled: boolean;
2828
}
2929

30-
const ns = (n: string) => (n ? n + " " : "");
31-
3230
/**
3331
* Create a debug instance for a namespace.
3432
*
@@ -50,24 +48,26 @@ const ns = (n: string) => (n ? n + " " : "");
5048
* @returns A debug instance.
5149
*/
5250
export function createDebug(namespace: string): DebugFn {
53-
const color = selectColor(namespace);
54-
const debugfn = (...data: unknown[]) => {
55-
if (!debugfn.enabled) return;
56-
const start = data.length ? data.shift() : "";
57-
if (useAnsi) {
58-
const name = useColor ? colorNs(namespace, color) : namespace;
59-
console.debug(ns(name) + start, ...data);
60-
} else {
61-
console.debug(
62-
`%c${ns(namespace)}%c${start}`,
63-
`color: #${color.toString(16).padStart(6, "0")}`,
64-
"color: inherit",
65-
...data,
66-
);
67-
}
68-
};
51+
const color = selectColor(namespace);
52+
const css = `color: #${color.toString(16).padStart(6, "0")}`;
53+
const ansiNamespace = useColor ? colorNs(namespace, color) : namespace;
54+
55+
const debugfn = (...data: unknown[]) => {
56+
if (!debugfn.enabled) return;
57+
const start = data.length ? data.shift() : "";
58+
if (useAnsi) {
59+
console.debug(`${ansiNamespace} ${start}`, ...data);
60+
} else {
61+
console.debug(
62+
`%c${namespace}%c ${start}`,
63+
css,
64+
"color: inherit",
65+
...data,
66+
);
67+
}
68+
};
6969

70-
debugfn.enabled = namespaces.check(namespace);
70+
debugfn.enabled = namespaces.check(namespace);
7171

72-
return debugfn;
72+
return debugfn;
7373
}

0 commit comments

Comments
 (0)