Skip to content

Instantly share code, notes, and snippets.

View jkohlin's full-sized avatar

Johan jkohlin

  • Sweden
  • 01:55 (UTC +01:00)
View GitHub Profile
@jkohlin
jkohlin / stringifyUnknown.ts
Last active October 31, 2023 15:48
stringifyUnknown
// Useful when passing an error from try/catch to a logger
export function stringifyUnknown(obj: unknown): string {
const defaultErrorString = 'unknown error'
const notNullish = obj ?? defaultErrorString
if (typeof notNullish === 'string') {
return notNullish
}
if (typeof notNullish === 'object') {
return JSON.stringify(notNullish, ['message', 'arguments', 'type', 'name', 'stack'], 2)
}
@jkohlin
jkohlin / colorlog.ts
Last active November 16, 2023 17:29
Create colorized console logs easier.
interface ColorItem {
value: string | number | object
color?: string
}
type Input = (string | ColorItem)[]
/**
* Logs data to the console with customizable colors and formatting.
* @param {Input} data - An array of data to log. Each item can be a string, number, or object with a "value" property.