Skip to content

Commit 7b0606b

Browse files
committed
Remove custom labels & polish labels
1 parent 11791b0 commit 7b0606b

File tree

6 files changed

+57
-38
lines changed

6 files changed

+57
-38
lines changed

src/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ export const ALLOWED_NODE_ENVS = ['dev', 'development']
99
export const DEFAULT_LOG_LEVEL: LogLevels = 'log'
1010

1111
export const DEFAULT_LABEL_SIZE = 14
12+
13+
export const PREVIOUS_VALUE_LABEL = 'Previous Value'
14+
export const CURRENT_VALUE_LABEL = 'Current Value'

src/index.test.tsx

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ describe('useLog', () => {
3737
expect(result.current.log).toBeTruthy()
3838

3939
renderHook(() => result.current.log('Test'))
40-
expect(consoleLog).toBeCalledWith(' On mount: Test')
40+
expect(consoleLog).toBeCalledWith(' On Mount: Test')
4141
expect(consoleLog).toBeCalledWith(
42-
'Previous value: %cTest',
42+
'Previous Value: %cTest',
4343
'color: SlateGray; font-weight: thin;',
4444
)
4545
expect(consoleLog).toBeCalledWith(
46-
' Current value: %cTest',
46+
' Current Value: %cTest',
4747
'color: green; font-weight: bold;',
4848
)
4949
expect(consoleLog).toBeCalledTimes(3)
@@ -77,19 +77,19 @@ describe('useLog', () => {
7777
'color: DodgerBlue',
7878
'color: SlateGray; font-weight: thin;',
7979
)
80-
expect(consoleLog).toBeCalledWith(' On mount: null')
80+
expect(consoleLog).toBeCalledWith(' On Mount: null')
8181

8282
expect(consoleGroup).toBeCalledWith(
8383
'Change in %c<TestComponent /> %c@ 09:38 PM',
8484
'color: DodgerBlue',
8585
'color: SlateGray; font-weight: thin;',
8686
)
8787
expect(consoleLog).toBeCalledWith(
88-
'Previous value: %cnull',
88+
'Previous Value: %cnull',
8989
'color: SlateGray; font-weight: thin;',
9090
)
9191
expect(consoleLog).toBeCalledWith(
92-
' Current value: %cnull',
92+
' Current Value: %cnull',
9393
'color: green; font-weight: bold;',
9494
)
9595
expect(consoleGroup).toBeCalledWith(
@@ -98,11 +98,11 @@ describe('useLog', () => {
9898
'color: SlateGray; font-weight: thin;',
9999
)
100100
expect(consoleLog).toBeCalledWith(
101-
'Previous value: %cnull',
101+
'Previous Value: %cnull',
102102
'color: SlateGray; font-weight: thin;',
103103
)
104104
expect(consoleLog).toBeCalledWith(
105-
' Current value: %cnull',
105+
' Current Value: %cnull',
106106
'color: green; font-weight: bold;',
107107
)
108108
expect(consoleLog).toBeCalledTimes(5)
@@ -121,11 +121,11 @@ describe('useLog', () => {
121121
'color: SlateGray; font-weight: thin;',
122122
)
123123
expect(consoleLog).toBeCalledWith(
124-
'Previous value: %cnull',
124+
'Previous Value: %cnull',
125125
'color: SlateGray; font-weight: thin;',
126126
)
127127
expect(consoleLog).toBeCalledWith(
128-
' Current value: %conChange 1s',
128+
' Current Value: %conChange 1s',
129129
'color: green; font-weight: bold;',
130130
)
131131
expect(consoleLog).toBeCalledTimes(7)
@@ -144,11 +144,11 @@ describe('useLog', () => {
144144
'color: SlateGray; font-weight: thin;',
145145
)
146146
expect(consoleLog).toBeCalledWith(
147-
'Previous value: %conChange 1s',
147+
'Previous Value: %conChange 1s',
148148
'color: SlateGray; font-weight: thin;',
149149
)
150150
expect(consoleLog).toBeCalledWith(
151-
' Current value: %conChange 2s',
151+
' Current Value: %conChange 2s',
152152
'color: green; font-weight: bold;',
153153
)
154154
expect(consoleLog).toBeCalledTimes(9)
@@ -166,11 +166,11 @@ describe('useLog', () => {
166166
'color: SlateGray; font-weight: thin;',
167167
)
168168
expect(consoleLog).toBeCalledWith(
169-
'Previous value: %conChange 2s',
169+
'Previous Value: %conChange 2s',
170170
'color: SlateGray; font-weight: thin;',
171171
)
172172
expect(consoleLog).toBeCalledWith(
173-
' Current value: %cnull',
173+
' Current Value: %cnull',
174174
'color: green; font-weight: bold;',
175175
)
176176
expect(consoleLog).toBeCalledTimes(11)

src/index.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ export function useLog({
9898

9999
useEffect(function onMount() {
100100
print({
101-
label: 'On mount',
102101
type: _PrintTypes.Mount,
103102
...printProps,
104103
})
@@ -107,7 +106,6 @@ export function useLog({
107106

108107
return function onUnmount() {
109108
print({
110-
label: 'On unmount',
111109
type: _PrintTypes.Unmount,
112110
prevValue: prevValueRef.current,
113111
...printProps,
@@ -118,7 +116,6 @@ export function useLog({
118116
useEffect(
119117
function onChange() {
120118
print({
121-
label: 'On change',
122119
type: _PrintTypes.Change,
123120
prevValue: prevValueRef.current,
124121
...printProps,

src/types.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ export interface UseLogReturn {
7676
export interface _PrintConfig<T> {
7777
value: T
7878
prevValue?: T
79-
label?: string
8079
type?: _PrintTypes
8180
styles?: Styles
8281
componentName: string
@@ -101,7 +100,6 @@ export type _PrintFlags =
101100

102101
/**
103102
* Label types of print groups
104-
* @internal
105103
*/
106104
export enum _PrintTypes {
107105
Mount = 'Mount',

src/utils.test.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as utils from './utils'
22
import { _PrintConfig, _PrintTypes, Printer } from './types'
33

4-
const { getGroupLabel, getComponentName, print, getPrinter } = utils
4+
const { getGroupLabel, getComponentName, print, getPrinter, getMessage } = utils
55

66
describe('utils', () => {
77
jest.spyOn(utils, 'getCurrentTime').mockReturnValue('09:38 PM')
@@ -59,7 +59,6 @@ describe('utils', () => {
5959

6060
const printProps: _PrintConfig<string> = {
6161
value: 'Test Value',
62-
label: 'A Label',
6362
componentName: 'SomeComponentName',
6463
}
6564

@@ -71,7 +70,7 @@ describe('utils', () => {
7170
undefined,
7271
undefined,
7372
)
74-
expect(consoleLog).toHaveBeenCalledWith(' A Label: Test Value')
73+
expect(consoleLog).toHaveBeenCalledWith(' On Change: Test Value')
7574
expect(consoleLog).toHaveBeenCalledTimes(1)
7675
expect(consoleGroupEnd).toHaveBeenCalled()
7776
})
@@ -85,9 +84,9 @@ describe('utils', () => {
8584
undefined,
8685
)
8786
expect(consoleLog).toHaveBeenCalledWith(
88-
'Previous value: Some Previous value',
87+
'Previous Value: Some Previous value',
8988
)
90-
expect(consoleLog).toHaveBeenCalledWith(' Current value: Test Value')
89+
expect(consoleLog).toHaveBeenCalledWith(' Current Value: Test Value')
9190
expect(consoleLog).toHaveBeenCalledTimes(2)
9291
expect(consoleGroupEnd).toHaveBeenCalled()
9392
})
@@ -101,7 +100,7 @@ describe('utils', () => {
101100
})
102101

103102
expect(consoleGroup).not.toHaveBeenCalled()
104-
expect(consoleLog).toHaveBeenCalledWith(' A Label: Test Value')
103+
expect(consoleLog).toHaveBeenCalledWith(' On Change: Test Value')
105104
expect(consoleGroupEnd).not.toHaveBeenCalled()
106105
})
107106

@@ -120,7 +119,7 @@ describe('utils', () => {
120119
undefined,
121120
undefined,
122121
)
123-
expect(consoleLog).toHaveBeenCalledWith(' A Label: Test Value')
122+
expect(consoleLog).toHaveBeenCalledWith(' On Change: Test Value')
124123
expect(consoleGroupEnd).toHaveBeenCalled()
125124
})
126125

@@ -173,12 +172,27 @@ describe('utils', () => {
173172
})
174173

175174
it('prints without label', () => {
176-
print({
177-
...printProps,
178-
label: undefined,
179-
})
175+
print(printProps)
176+
177+
expect(consoleLog).toHaveBeenCalledWith(' On Change: Test Value')
178+
})
179+
})
180180

181-
expect(consoleLog).toHaveBeenCalledWith(' Test Value')
181+
describe('getMessage', () => {
182+
it('returns message', () => {
183+
expect(getMessage('Test Value', 'Some Label')).toEqual(
184+
' Some Label: Test Value',
185+
)
186+
})
187+
188+
it('returns message without label', () => {
189+
expect(getMessage('Test Value')).toEqual(' Test Value')
190+
})
191+
192+
it('returns message with css', () => {
193+
expect(getMessage('Test Value', 'Some Label', true)).toEqual(
194+
' Some Label: %cTest Value',
195+
)
182196
})
183197
})
184198
})

src/utils.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import * as utils from './utils'
22
import { Printer, _PrintConfig, _PrintTypes, _SupportedConsole } from './types'
3-
import { DEFAULT_LABEL_SIZE } from './constants'
3+
import {
4+
CURRENT_VALUE_LABEL,
5+
DEFAULT_LABEL_SIZE,
6+
PREVIOUS_VALUE_LABEL,
7+
} from './constants'
48

59
/* istanbul ignore next */
610
export function getCurrentTime(): string {
@@ -12,7 +16,11 @@ export function stylePlaceholder(withCss?: boolean): string {
1216
return withCss ? '%c' : ''
1317
}
1418

15-
export function getMessageLabel<T>(
19+
export function getLabel(type: _PrintTypes): string {
20+
return `On ${type}`
21+
}
22+
23+
export function getMessage<T>(
1624
value: T,
1725
label?: string,
1826
withCss?: boolean,
@@ -75,7 +83,6 @@ export function getPrinter(
7583

7684
export function print<T>({
7785
value,
78-
label,
7986
prevValue,
8087
componentName,
8188
flags = {
@@ -110,17 +117,17 @@ export function print<T>({
110117
css?: string,
111118
): void => {
112119
const printer = getCurrentPrinter(logLevel)
113-
const message = getMessageLabel(printValue, label, Boolean(css))
120+
const message = getMessage(printValue, label, Boolean(css))
114121

115122
if (!css) printer(message)
116123
if (css) printer(message, css)
117124
}
118125

119126
if ('prevValue' in arguments[0]) {
120-
printAtLevel('Previous value', arguments[0].prevValue, subValueCSS)
121-
printAtLevel('Current value', value, changeCSS)
127+
printAtLevel(PREVIOUS_VALUE_LABEL, arguments[0].prevValue, subValueCSS)
128+
printAtLevel(CURRENT_VALUE_LABEL, value, changeCSS)
122129
} else {
123-
printAtLevel(label)
130+
printAtLevel(getLabel(type))
124131
}
125132

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

0 commit comments

Comments
 (0)