|
1 | 1 | import { couleur } from "./couleur" |
2 | 2 | import { isBrowser, stringToRgb } from "./helpers" |
3 | 3 |
|
| 4 | +let LAST_TIME = Date.now() |
| 5 | + |
4 | 6 | /** |
5 | 7 | * debug |
| 8 | + * @param namespace - The namespace to log |
| 9 | + * @returns A function that logs the namespace and arguments to the console |
| 10 | + * |
| 11 | + * ex: |
| 12 | + * import debug from "@wbe/debug" |
| 13 | + * const log = debug("myNamespace") |
| 14 | + * log("Hello World") // logs "myNamespace Hello World +0ms" |
6 | 15 | */ |
7 | | -// prettier-ignore |
8 | | -export const debug = (namespace?: string) => (...rest: any[]): void => { |
9 | | - const rgb = stringToRgb(namespace) |
10 | | - |
11 | | - const showLog = (value: string): boolean => |
12 | | - value?.includes(":*") |
13 | | - ? namespace.startsWith( value.split(":*")[0]) |
14 | | - : value === namespace || value === "*" |
15 | | - |
16 | | - // Allow to bypass dropping of console.log from the build process |
17 | | - // tested with esbuild config: pure: ["console.log"] or drop: ["console"] |
18 | | - const log = console['log'] |
19 | | - |
20 | | - if (isBrowser) |
21 | | - { |
22 | | - showLog(localStorage.getItem("debug")) |
23 | | - && |
24 | | - log( |
25 | | - namespace && `%c${namespace}`, `color: rgb(${rgb[0]}, ${rgb[1]}, ${rgb[2]})`, |
26 | | - ...rest |
27 | | - ) |
28 | | - } |
29 | | - |
30 | | - else |
31 | | -{ |
32 | | - showLog(process.env.DEBUG) |
33 | | - && |
34 | | - log( |
35 | | - namespace && couleur.bold(couleur.rgb(rgb[0], rgb[1], rgb[2])(namespace)), |
36 | | - ...rest |
37 | | - ) |
| 16 | +export const debug = |
| 17 | + (namespace?: string, elapsedTime = true) => |
| 18 | + (...rest: any[]): void => { |
| 19 | + // Calculate elapsed time since last execution |
| 20 | + const now = Date.now() |
| 21 | + const elapsed = now - LAST_TIME |
| 22 | + LAST_TIME = now |
| 23 | + const elapsedString = `+${elapsed}ms` |
| 24 | + |
| 25 | + // Get the namespace color |
| 26 | + const rgb = stringToRgb(namespace) |
| 27 | + |
| 28 | + // Define when to show the log |
| 29 | + const showLog = (value: string): boolean => |
| 30 | + value?.includes(":*") |
| 31 | + ? namespace.startsWith(value.split(":*")[0]) |
| 32 | + : value === namespace || value === "*" |
| 33 | + |
| 34 | + // Allow to bypass dropping of console.log from the build process |
| 35 | + // has been test with esbuild drop: ["console"] & pure: ["console.log"] |
| 36 | + const log = console["log"] |
| 37 | + |
| 38 | + /** |
| 39 | + * Browser environment |
| 40 | + */ |
| 41 | + if (isBrowser) { |
| 42 | + if (showLog(localStorage.getItem("debug"))) { |
| 43 | + const colorStyle = `color: rgb(${rgb[0]}, ${rgb[1]}, ${rgb[2]}); font-weight: bold` |
| 44 | + const args = [] |
| 45 | + |
| 46 | + // Start with the colored namespace format specifier and its style |
| 47 | + let format = `%c${namespace}` |
| 48 | + args.push(colorStyle) |
| 49 | + |
| 50 | + // Process the rest arguments |
| 51 | + // Use %c for strings to allow potential future styling or just display |
| 52 | + // Use %o for objects, arrays, etc., for better inspection |
| 53 | + for (let i = 0; i < rest.length; i++) { |
| 54 | + const arg = rest[i] |
| 55 | + if (typeof arg === "string") { |
| 56 | + format += ` %c${arg}` |
| 57 | + args.push("color: inherit") |
| 58 | + } else { |
| 59 | + format += " %o" |
| 60 | + args.push(arg) |
| 61 | + } |
| 62 | + } |
| 63 | + // Append the elapsed time format specifier and its style |
| 64 | + if (elapsedTime) { |
| 65 | + format += ` %c${elapsedString}` |
| 66 | + args.push(colorStyle) |
| 67 | + } |
| 68 | + // Append the whole formatted string and log it |
| 69 | + args.unshift(format) |
| 70 | + log(...args) |
| 71 | + } |
| 72 | + } else { |
| 73 | + /** |
| 74 | + * Node.js environment |
| 75 | + */ |
| 76 | + const wColor = (s: string) => |
| 77 | + couleur.bold(couleur.rgb(rgb[0], rgb[1], rgb[2])(s)) |
| 78 | + |
| 79 | + if (showLog(process.env.DEBUG)) { |
| 80 | + elapsedTime |
| 81 | + ? log(wColor(namespace), ...rest, wColor(elapsedString)) |
| 82 | + : log(wColor(namespace), ...rest) |
| 83 | + } |
| 84 | + } |
38 | 85 | } |
39 | | -} |
|
0 commit comments