* Stringify, prettify and colorize log arguments. * * @param args The log arguments. * * @returns The stringified output.
(args)
| 25 | * @returns The stringified output. |
| 26 | */ |
| 27 | function stringify(args) { |
| 28 | return args |
| 29 | .map((arg) => { |
| 30 | // If argument is an error, stringify it |
| 31 | if (arg instanceof Error) { |
| 32 | return arg.stack ?? `${arg.name}: ${arg.message}`; |
| 33 | } |
| 34 | |
| 35 | // Otherwise, convert argument to JSON string |
| 36 | let jsonString = JSON.stringify( |
| 37 | arg, |
| 38 | (_, value) => { |
| 39 | // Get type of value |
| 40 | const type = typeof value; |
| 41 | |
| 42 | // If it is a bigint, convert it to a number |
| 43 | if (type === 'bigint') { |
| 44 | return Number(value); |
| 45 | } |
| 46 | |
| 47 | // If it is a non supported object, convert it to its constructor name |
| 48 | if (value && (type === 'object' || type === 'function')) { |
| 49 | const name = Object.getPrototypeOf(value)?.constructor?.name; |
| 50 | if (name && name !== 'Object' && name !== 'Array') { |
| 51 | return `[${name}]`; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // If it is a non supported value, convert it to a string |
| 56 | if ( |
| 57 | value === undefined || |
| 58 | value === Infinity || |
| 59 | value === -Infinity || |
| 60 | Number.isNaN(value) |
| 61 | ) { |
| 62 | return `[${value}]`; |
| 63 | } |
| 64 | |
| 65 | // Otherwise, return value as is |
| 66 | return value; |
| 67 | }, |
| 68 | 2 |
| 69 | ); |
| 70 | |
| 71 | // Transform and colorize specific JSON tokens |
| 72 | const output = []; |
| 73 | while (jsonString) { |
| 74 | for (const [token, regex] of jsonTokens) { |
| 75 | const match = regex.exec(jsonString); |
| 76 | if (match) { |
| 77 | const substring = match[0]; |
| 78 | jsonString = jsonString.substring(substring.length); |
| 79 | if (token === 'key') { |
| 80 | output.push( |
| 81 | `<span class="text-slate-700 dark:text-slate-300">${substring.slice(1, -1)}</span>` |
| 82 | ); |
| 83 | } else if (token === 'instance') { |
| 84 | output.push( |
no test coverage detected
searching dependent graphs…