Skip to content

Commit 21580cb

Browse files
committed
Add in-code documentation
1 parent 980e70e commit 21580cb

File tree

7 files changed

+122
-30
lines changed

7 files changed

+122
-30
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
React hook for logging per component lifecycle
2828

2929
## Features
30-
- 🪶 **Lightweight** — under *1Kb* gzipped
30+
- 🪶 **Lightweight** — under *1.5Kb* gzipped
3131
- 🗂️ **Typed** — made with TypeScript, ships with types
3232
- 🥰 **Simple** — don't worry about any changes in your props & state
3333
- 🔧 **Customizable** — work in progress 😉

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,11 @@
106106
"size-limit": [
107107
{
108108
"path": "dist/main.js",
109-
"limit": "1 kB"
109+
"limit": "1.5 kB"
110110
},
111111
{
112112
"path": "dist/module.js",
113-
"limit": "1 kB"
113+
"limit": "1.5 kB"
114114
}
115115
]
116116
}

src/constants.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const CSS_COMPONENT = 'color: DodgerBlue'
2+
export const CSS_CHANGE = 'color: green; font-weight: bold;'
3+
export const CSS_SUB_VALUE = 'color: SlateGray; font-weight: thin;'
4+
5+
export const ALLOWED_NODE_ENVS = ['dev', 'development']

src/index.tsx

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,68 @@
1-
import { useEffect, useRef } from 'react'
2-
import { UseLog, UseLogReturn, Log, PrintTypes, PrintProps } from './types'
3-
import { getComponentName, print } from './utils'
1+
// Copyright (c) Dolf Barr <[email protected]>. All rights reserved. Licensed under the MIT license.
42

5-
const CSS_COMPONENT = 'color: DodgerBlue'
6-
const CSS_CHANGE = 'color: green; font-weight: bold;'
7-
const CSS_SUB_VALUE = 'color: SlateGray; font-weight: thin;'
3+
/**
4+
* A react hook for logging through component lifecycle.
5+
*
6+
* @packageDocumentation
7+
*/
88

9-
const ALLOWED_NODE_ENVS = ['dev', 'development']
9+
import { useEffect, useRef } from 'react'
10+
import {
11+
ALLOWED_NODE_ENVS,
12+
CSS_CHANGE,
13+
CSS_COMPONENT,
14+
CSS_SUB_VALUE,
15+
} from './constants'
16+
import {
17+
UseLogConfig,
18+
UseLogReturn,
19+
LogConfig,
20+
_PrintTypes,
21+
_PrintConfig,
22+
} from './types'
23+
import { getComponentName, print } from './utils'
1024

25+
/**
26+
* Provides a function to log through react component lifecycle.
27+
*
28+
* @param config - component level configuration for any log function in the component
29+
* @see {@link UseLogConfig} for the config data structure
30+
*
31+
* @returns set of functions suitable for logging
32+
*
33+
* @example
34+
* ```ts
35+
* const {log} = useLog({environments: ['dev']})
36+
* ```
37+
*/
1138
export function useLog({
1239
styles: {
1340
componentCSS = CSS_COMPONENT,
1441
changeCSS = CSS_CHANGE,
1542
subValueCSS = CSS_SUB_VALUE,
1643
} = {},
1744
environments = ALLOWED_NODE_ENVS,
18-
}: UseLog = {}): UseLogReturn {
45+
}: UseLogConfig = {}): UseLogReturn {
1946
const componentName = getComponentName()
2047

21-
function log<T>(value: T, props?: Log): void {
48+
/**
49+
* Logging function to log through react component lifecycle.
50+
*
51+
* @param value - a value which changes will be logged
52+
* @typeParam T - type of the tracking value
53+
* @param config - component level configuration for any log function in the component
54+
* @see {@link LogConfig} for the config data structure
55+
*
56+
* @example
57+
* ```ts
58+
* log(someState, {environments: ['production']})
59+
* ```
60+
*/
61+
function log<T>(value: T, props?: LogConfig): void {
2262
const clonedValue = JSON.parse(JSON.stringify(value)) as T
2363
const prevValueRef = useRef<T>()
2464
const printProps: Pick<
25-
PrintProps<T>,
65+
_PrintConfig<T>,
2666
'value' | 'styles' | 'componentName'
2767
> = {
2868
value: clonedValue,
@@ -37,6 +77,7 @@ export function useLog({
3777
if (environments.includes(process.env.NODE_ENV ?? 'production')) {
3878
function logHooks(): void {
3979
const isUnmounting = useRef(false)
80+
4081
useEffect(function setIsUnmounting() {
4182
return function setIsUnmountingOnMount() {
4283
isUnmounting.current = true
@@ -46,7 +87,7 @@ export function useLog({
4687
useEffect(function onMount() {
4788
print({
4889
label: 'On mount',
49-
type: PrintTypes.Mount,
90+
type: _PrintTypes.Mount,
5091
...printProps,
5192
})
5293

@@ -55,7 +96,7 @@ export function useLog({
5596
return function onUnmount() {
5697
print({
5798
label: 'On unmount',
58-
type: PrintTypes.Unmount,
99+
type: _PrintTypes.Unmount,
59100
prevValue: prevValueRef.current,
60101
...printProps,
61102
})
@@ -66,7 +107,7 @@ export function useLog({
66107
function onChange() {
67108
print({
68109
label: 'On change',
69-
type: PrintTypes.Change,
110+
type: _PrintTypes.Change,
70111
prevValue: prevValueRef.current,
71112
...printProps,
72113
})

src/types.ts

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,77 @@
11
export interface Styles {
2+
/**
3+
* Inline css for rendering component name in the logs
4+
*
5+
* @defaultValue Blue text color
6+
*
7+
* @example
8+
* ```css
9+
* color:green;font-weight:bold;background-color:black;
10+
* ```
11+
*/
212
componentCSS?: string
13+
/**
14+
* Inline css for rendering current value in the logs
15+
*
16+
* @defaultValue Green text color with bold font
17+
*
18+
* @example
19+
* ```css
20+
* color:green;font-weight:bold;background-color:black;
21+
* ```
22+
*/
323
changeCSS?: string
24+
/**
25+
* Inline css for rendering any additional data like time or previous value in the logs
26+
*
27+
* @defaultValue Gray text color with thin font
28+
*
29+
* @example
30+
* ```css
31+
* color:green;font-weight:bold;background-color:black;
32+
* ```
33+
*/
434
subValueCSS?: string
535
}
636

7-
export interface UseLog {
37+
/** Describes configuration object at component level */
38+
export interface UseLogConfig {
39+
/** Contains styles object with different CSS inline styles used in logging */
840
styles?: Styles
41+
/** Contains array of environments of `process.env.NODE_ENV` in which logging will be allowed */
942
environments?: string[]
1043
}
1144

12-
export type Log = UseLog
45+
/** Describes configuration object at call level, can be used to override configuration */
46+
export type LogConfig = UseLogConfig
1347

48+
/** Return value of `useLog` hook */
1449
export interface UseLogReturn {
15-
log: <T>(value: T, props?: Log) => void
50+
/** Used for logging per component lifecycle */
51+
log: <T>(value: T, props?: LogConfig) => void
1652
}
1753

18-
export interface PrintProps<T> {
54+
/**
55+
* Describes configuration object of the inner print function
56+
* @internal
57+
*
58+
* @typeParam T - type of the tracking value
59+
*/
60+
export interface _PrintConfig<T> {
1961
value: T
2062
prevValue?: T
2163
label: string
2264
group?: string
23-
type?: PrintTypes
65+
type?: _PrintTypes
2466
styles?: Styles
2567
componentName: string
2668
}
2769

28-
export enum PrintTypes {
70+
/**
71+
* Label types of print groups
72+
* @internal
73+
*/
74+
export enum _PrintTypes {
2975
Mount = 'Mount',
3076
Unmount = 'Unmount',
3177
Change = 'Change',

src/utils.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import { getGroupLabel, getComponentName, print } from './utils'
2-
import { PrintProps, PrintTypes } from './types'
2+
import { _PrintConfig, _PrintTypes } from './types'
33

44
describe('utils', () => {
55
describe('getGroupLabel', () => {
66
it('renders', () => {
7-
expect(getGroupLabel(PrintTypes.Change)).toEqual(
7+
expect(getGroupLabel(_PrintTypes.Change)).toEqual(
88
`Change %c%c@ ${new Date().toLocaleTimeString()}`,
99
)
1010
})
1111

1212
it('renders with component name', () => {
13-
expect(getGroupLabel(PrintTypes.Mount, 'TestComponent')).toEqual(
13+
expect(getGroupLabel(_PrintTypes.Mount, 'TestComponent')).toEqual(
1414
`Mount in %c<TestComponent /> %c@ ${new Date().toLocaleTimeString()}`,
1515
)
1616
})
@@ -31,7 +31,7 @@ describe('utils', () => {
3131
.spyOn(console, 'groupEnd')
3232
.mockImplementation(() => null)
3333

34-
const printProps: PrintProps<string> = {
34+
const printProps: _PrintConfig<string> = {
3535
value: 'Test Value',
3636
label: 'A Label',
3737
componentName: 'SomeComponentName',

src/utils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { PrintProps, PrintTypes } from './types'
1+
import { _PrintConfig, _PrintTypes } from './types'
22

33
export function getGroupLabel(
4-
type: PrintTypes,
4+
type: _PrintTypes,
55
componentName?: string,
66
): string {
77
return `${String(type)} ${
@@ -35,10 +35,10 @@ export function print<T>({
3535
label,
3636
prevValue,
3737
componentName,
38-
type = PrintTypes.Change,
38+
type = _PrintTypes.Change,
3939
group = getGroupLabel(type, componentName),
4040
styles: { componentCSS, subValueCSS, changeCSS } = {},
41-
}: PrintProps<T>): void {
41+
}: _PrintConfig<T>): void {
4242
console.group(group, componentCSS, subValueCSS)
4343

4444
if (!('prevValue' in arguments[0])) {

0 commit comments

Comments
 (0)