Skip to content

Commit 692cfaa

Browse files
committed
Refactor print
1 parent 41c24aa commit 692cfaa

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

src/utils.test.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,9 @@ describe('utils', () => {
8585
undefined,
8686
)
8787
expect(consoleLog).toHaveBeenCalledWith(
88-
'Previous value: %cSome Previous value',
89-
undefined,
90-
)
91-
expect(consoleLog).toHaveBeenCalledWith(
92-
' Current value: %cTest Value',
93-
undefined,
88+
'Previous value: Some Previous value',
9489
)
90+
expect(consoleLog).toHaveBeenCalledWith(' Current value: Test Value')
9591
expect(consoleLog).toHaveBeenCalledTimes(2)
9692
expect(consoleGroupEnd).toHaveBeenCalled()
9793
})

src/utils.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,25 @@ export function print<T>({
7777
)
7878
}
7979

80+
const printAtLevel = (
81+
label?: string,
82+
printValue: T = value,
83+
css?: string,
84+
): void => {
85+
const printer = getCurrentPrinter(logLevel)
86+
const message = `${
87+
label ? `${label.padStart(14, ' ')}: ` : ''.padStart(16, ' ')
88+
}${css ? '%c' : ''}${String(printValue)}`
89+
90+
if (!css) printer(message)
91+
if (css) printer(message, css)
92+
}
93+
8094
if ('prevValue' in arguments[0]) {
81-
getCurrentPrinter(logLevel)(
82-
`Previous value: %c${String(arguments[0].prevValue)}`,
83-
subValueCSS,
84-
)
85-
getCurrentPrinter(logLevel)(` Current value: %c${String(value)}`, changeCSS)
95+
printAtLevel('Previous value', arguments[0].prevValue, subValueCSS)
96+
printAtLevel('Current value', value, changeCSS)
8697
} else {
87-
getCurrentPrinter(logLevel)(
88-
`${label ? `${label.padStart(14, ' ')}: ` : ''.padStart(16, ' ')}${String(
89-
value,
90-
)}`,
91-
)
98+
printAtLevel(label)
9299
}
93100

94101
if (flags.isGrouped) getCurrentPrinter('groupEnd')()

0 commit comments

Comments
 (0)