Skip to content

Commit 11791b0

Browse files
committed
Prevent rendering of css placeholder without css
1 parent 692cfaa commit 11791b0

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

src/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ export const CSS_SUB_VALUE = 'color: SlateGray; font-weight: thin;'
77
export const ALLOWED_NODE_ENVS = ['dev', 'development']
88

99
export const DEFAULT_LOG_LEVEL: LogLevels = 'log'
10+
11+
export const DEFAULT_LABEL_SIZE = 14

src/utils.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ describe('utils', () => {
77
jest.spyOn(utils, 'getCurrentTime').mockReturnValue('09:38 PM')
88
describe('getGroupLabel', () => {
99
it('renders', () => {
10-
expect(getGroupLabel(_PrintTypes.Change)).toEqual('Change %c%c@ 09:38 PM')
10+
expect(getGroupLabel(_PrintTypes.Change)).toEqual('Change @ 09:38 PM')
1111
})
1212

1313
it('renders with component name', () => {
1414
expect(getGroupLabel(_PrintTypes.Mount, 'TestComponent')).toEqual(
15-
'Mount in %c<TestComponent /> %c@ 09:38 PM',
15+
'Mount in <TestComponent /> @ 09:38 PM',
1616
)
1717
})
1818
})
@@ -67,7 +67,7 @@ describe('utils', () => {
6767
print(printProps)
6868

6969
expect(consoleGroup).toHaveBeenCalledWith(
70-
'Change in %c<SomeComponentName /> %c@ 09:38 PM',
70+
'Change in <SomeComponentName /> @ 09:38 PM',
7171
undefined,
7272
undefined,
7373
)
@@ -80,7 +80,7 @@ describe('utils', () => {
8080
print({ ...printProps, prevValue: 'Some Previous value' })
8181

8282
expect(consoleGroup).toHaveBeenCalledWith(
83-
'Change in %c<SomeComponentName /> %c@ 09:38 PM',
83+
'Change in <SomeComponentName /> @ 09:38 PM',
8484
undefined,
8585
undefined,
8686
)
@@ -116,7 +116,7 @@ describe('utils', () => {
116116

117117
expect(consoleGroup).not.toHaveBeenCalled()
118118
expect(consoleGroupCollapsed).toHaveBeenCalledWith(
119-
'Change in %c<SomeComponentName /> %c@ 09:38 PM',
119+
'Change in <SomeComponentName /> @ 09:38 PM',
120120
undefined,
121121
undefined,
122122
)

src/utils.ts

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,43 @@
11
import * as utils from './utils'
22
import { Printer, _PrintConfig, _PrintTypes, _SupportedConsole } from './types'
3+
import { DEFAULT_LABEL_SIZE } from './constants'
34

45
/* istanbul ignore next */
56
export function getCurrentTime(): string {
67
// No need in testing Date module
78
return new Date().toLocaleTimeString()
89
}
910

11+
export function stylePlaceholder(withCss?: boolean): string {
12+
return withCss ? '%c' : ''
13+
}
14+
15+
export function getMessageLabel<T>(
16+
value: T,
17+
label?: string,
18+
withCss?: boolean,
19+
): string {
20+
const printLabel = label
21+
? `${label.padStart(DEFAULT_LABEL_SIZE, ' ')}: `
22+
: ''.padStart(DEFAULT_LABEL_SIZE + 2, ' ')
23+
24+
return `${printLabel}${stylePlaceholder(withCss)}${String(value)}`
25+
}
26+
1027
export function getGroupLabel(
1128
type: _PrintTypes,
1229
componentName?: string,
30+
withComponentCSS?: boolean,
31+
withSubValueCSS?: boolean,
1332
): string {
33+
const componentCssPlaceholder = stylePlaceholder(withComponentCSS)
34+
const subValueCssPlaceholder = stylePlaceholder(withComponentCSS)
35+
1436
const componentNameWrapper = componentName
15-
? `in %c<${String(componentName)} /> `
16-
: '%c'
37+
? `in ${componentCssPlaceholder}<${String(componentName)} /> `
38+
: `${componentCssPlaceholder}`
1739
const typeWrapper = `${String(type)} `
18-
const timeWrapper = `%c@ ${utils.getCurrentTime()}`
40+
const timeWrapper = `${subValueCssPlaceholder}@ ${utils.getCurrentTime()}`
1941

2042
return `${typeWrapper}${componentNameWrapper}${timeWrapper}`
2143
}
@@ -71,7 +93,12 @@ export function print<T>({
7193

7294
if (flags.isGrouped) {
7395
getCurrentPrinter(flags.isCollapsed ? 'groupCollapsed' : 'group')(
74-
getGroupLabel(type, componentName),
96+
getGroupLabel(
97+
type,
98+
componentName,
99+
Boolean(componentCSS),
100+
Boolean(subValueCSS),
101+
),
75102
componentCSS,
76103
subValueCSS,
77104
)
@@ -83,9 +110,7 @@ export function print<T>({
83110
css?: string,
84111
): void => {
85112
const printer = getCurrentPrinter(logLevel)
86-
const message = `${
87-
label ? `${label.padStart(14, ' ')}: ` : ''.padStart(16, ' ')
88-
}${css ? '%c' : ''}${String(printValue)}`
113+
const message = getMessageLabel(printValue, label, Boolean(css))
89114

90115
if (!css) printer(message)
91116
if (css) printer(message, css)

0 commit comments

Comments
 (0)