Skip to content

Commit

Permalink
fix inconsistencies between color/nocolor logging
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed Jul 29, 2024
1 parent 1357f4a commit 2041b1a
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions logging.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,35 @@ export const UNCOLOR = symbol.create()
/* c8 ignore start */
/**
* @param {Array<undefined|string|Symbol|Object|number|function():any>} args
* @return {Array<string|object|number>}
* @return {Array<string|object|number|undefined>}
*/
export const computeNoColorLoggingArgs = args => {
if (args.length === 1 && args[0]?.constructor === Function) {
args = /** @type {Array<string|Symbol|Object|number>} */ (/** @type {[function]} */ (args)[0]())
}
const strBuilder = []
const logArgs = []
// try with formatting until we find something unsupported
let i = 0
for (; i < args.length; i++) {
const arg = args[i]
if (arg === undefined) {
logArgs.push('undefined')
break
} else if (arg.constructor === String || arg.constructor === Number) {
logArgs.push(arg)
strBuilder.push(arg)
} else if (arg.constructor === Object) {
logArgs.push(JSON.stringify(arg))
break
}
}
if (i > 0) {
// create logArgs with what we have so far
logArgs.push(strBuilder.join(''))
}
// append the rest
for (; i < args.length; i++) {
const arg = args[i]
if (!(arg instanceof Symbol)) {
logArgs.push(arg)
}
}
return logArgs
Expand Down

0 comments on commit 2041b1a

Please sign in to comment.