Skip to content

Commit c59e0b9

Browse files
committed
Add groups
1 parent 23e16be commit c59e0b9

File tree

5 files changed

+88
-12
lines changed

5 files changed

+88
-12
lines changed

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"objects": "always-multiline",
3737
"imports": "always-multiline",
3838
"exports": "always-multiline",
39-
"functions": "always-multiline"
39+
"functions": "always-multiline",
40+
"enums": "always-multiline"
4041
}
4142
],
4243
"space-before-function-paren": "off",

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ React hook for logging per component lifecycle
2828
- 🔧 **Customizable** — work in progress 😉
2929
- 🔬 **Tested** — up to 100% coverage
3030
- 🏎️ **Fast** — native react hooks & optimized
31-
- 📭 **No dependecies**
31+
- 📭 **Minimal dependecies** — only some lodash functions
3232

3333

3434

package-lock.json

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,5 +111,8 @@
111111
"path": "dist/module.js",
112112
"limit": "1 kB"
113113
}
114-
]
114+
],
115+
"dependencies": {
116+
"lodash": "^4.17.21"
117+
}
115118
}

src/index.tsx

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,66 @@
1+
import cloneDeep from 'lodash/cloneDeep'
12
import { useEffect, useRef } from 'react'
23

4+
const CSS_COMPONENT = 'color: DodgerBlue'
5+
const CSS_DATE = 'color: SlateGray;'
6+
37
export interface UseLogReturn {
48
log: <T>(value: T) => void
59
}
610

7-
export const useLog = (): UseLogReturn => {
11+
interface PrintProps<T> {
12+
value: T
13+
label: string
14+
group?: string
15+
type?: PrintTypes
16+
}
17+
18+
export enum PrintTypes {
19+
Mount = 'Mount',
20+
Unmount = 'Unmount',
21+
BeforeUnmount = 'Before unmount',
22+
Change = 'Change',
23+
}
24+
25+
export function useLog(): UseLogReturn {
26+
const componentName = (function getComponentName() {
27+
try {
28+
throw new Error()
29+
} catch (error) {
30+
if (error instanceof Error) {
31+
const re = /(\w+)@|at (\w+) \(/g
32+
33+
re.exec(error?.stack ?? '')
34+
re.exec(error?.stack ?? '')
35+
const m = re.exec(error?.stack ?? '') ?? []
36+
37+
return String(m[1] || m[2])
38+
}
39+
40+
return ''
41+
}
42+
})()
43+
44+
const getGroupLabel = (type: PrintTypes): string => {
45+
return `${String(type)} ${
46+
componentName ? 'in %c<' + String(componentName) + ' /> ' : '%c'
47+
}%c@ ${new Date().toLocaleTimeString()}`
48+
}
49+
50+
function print<T>({
51+
value,
52+
label,
53+
type = PrintTypes.Change,
54+
group = getGroupLabel(type),
55+
}: PrintProps<T>): void {
56+
console.group(group, CSS_COMPONENT, CSS_DATE)
57+
console.log(`${label}: ${String(value)}`)
58+
console.groupEnd()
59+
}
60+
861
function log<T>(value: T): void {
62+
const clonedValue = cloneDeep(value)
63+
964
return (() => {
1065
const isUnmounting = useRef(false)
1166
useEffect(() => {
@@ -15,19 +70,35 @@ export const useLog = (): UseLogReturn => {
1570
}, [])
1671

1772
useEffect(() => {
18-
console.log(`On mount: ${String(value)}`)
73+
print({
74+
label: 'On mount',
75+
value: clonedValue,
76+
type: PrintTypes.Mount,
77+
})
1978

2079
return () => {
21-
console.log(`On unmount: ${String(value)}`)
80+
print({
81+
label: 'On unmount',
82+
value: clonedValue,
83+
type: PrintTypes.Unmount,
84+
})
2285
}
2386
}, [])
2487

2588
useEffect(() => {
26-
console.log(`On change: ${String(value)}`)
89+
print({
90+
label: 'On change',
91+
value: clonedValue,
92+
type: PrintTypes.Change,
93+
})
2794

2895
return () => {
2996
if (isUnmounting.current) {
30-
console.log(`Before unmount: ${String(value)}`)
97+
print({
98+
label: 'Before unmount',
99+
value: clonedValue,
100+
type: PrintTypes.BeforeUnmount,
101+
})
31102
}
32103
}
33104
}, [value])

0 commit comments

Comments
 (0)