Created
September 23, 2024 13:06
-
-
Save aleph-naught2tog/12da57e99d5f629fac6405f69bb8adef to your computer and use it in GitHub Desktop.
A module for a debug call that will log out what you want, with a message, to a customatizable depth. Wrapper around util.inspect.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import util from 'util'; | |
// NOTE: THIS IS NODE ONLY, BECAUSE OF THE RELIANCE ON NODE THE 'UTIL' MODULE. | |
/** | |
* Creates a deep, pretty-formatted, colorful, human-readable string of any | |
* object suitable for logging and debugging. | |
*`InspectOptions` is a pretty rich interface, but some reasonable defaults have | |
* been set, such as sorting keys alphabetically for ease of reading, color | |
* (giving a syntax-highlighted experience), showing full depth of the objects | |
* instead of just [Array], ensuring each set of values is on a new line, etc. | |
* If you pass in your own values, those will always take precedence. | |
*/ | |
export const deepPrettyInspect = ( | |
obj: unknown, | |
config?: InspectOptions, | |
): string => { | |
const NON_DEV_DEPTH = 5; | |
const DEFAULT_CONFIG: InspectOptions = { | |
depth: isDevelopment ? Infinity : NON_DEV_DEPTH, | |
colors: isDevelopment, | |
compact: false, | |
sorted: true, | |
}; | |
const options = { | |
...DEFAULT_CONFIG, | |
...config, | |
}; | |
const prettyString = util.inspect(obj, options); | |
return prettyString; | |
}; | |
export const deepPrettyDebug = ( | |
message: string, | |
obj: unknown, | |
config?: InspectOptions, | |
) => { | |
let fullMessage = ''; | |
if (config?.colors || isDevelopment) { | |
fullMessage += `\x1B[1;35m[${message}]\x1B[0m\n`; | |
} | |
console.debug(fullMessage, deepPrettyInspect(obj, config)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment