Skip to content

Commit ae5e94f

Browse files
committed
feat: add documentation for helpers
1 parent 84facb1 commit ae5e94f

5 files changed

Lines changed: 1020 additions & 1000 deletions

File tree

.vitepress/config.mts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ export default defineConfig({
5656
{ text: 'Mail', link: '/guide/deeper/mail' },
5757
{ text: 'Storage', link: '/guide/deeper/filesystem' },
5858
{ text: 'Musket CLI', link: '/guide/deeper/musket' },
59+
{ text: 'Helpers', link: '/guide/helpers' },
60+
{ text: 'Strings', link: '/guide/strings' },
61+
{ text: 'Debug', link: '/guide/debug' },
5962
{ text: 'Deployment', link: '/guide/deployment' },
6063
]
6164
},
Lines changed: 94 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,38 @@
11
# Debug Helpers
22

3-
The `@h3ravel/support` package provides convenient debugging helpers inspired by Laravel's debugging utilities. These helpers make it easy to inspect variables, objects, and data structures during development.
3+
## Introduction
4+
5+
H3ravel provides convenient debugging helpers inspired by Laravel's debugging utilities via the `@h3ravel/support` package. These helpers make it easy to inspect variables, objects, and data structures during development, many of them are also used within the framework itself; however, you are free to use them in your own applications if you find them convenient.
6+
7+
To use these helpers in your application, you may install the `@h3ravel/support` package by running:
8+
9+
::: code-group
10+
11+
```sh [npm]
12+
$ npm install @h3ravel/support --save
13+
```
14+
15+
```sh [yarn]
16+
$ yarn add @h3ravel/support
17+
```
18+
19+
```sh [pnpm]
20+
$ pnpm add @h3ravel/support
21+
```
22+
23+
```sh [bun]
24+
$ bun create @h3ravel/support
25+
```
26+
27+
:::
28+
29+
The `@h3ravel/support` is installed by default when using H3ravel
430

531
## Available Helpers
632

733
- [`dump()`](#dump) - Output values and continue execution
834
- [`dd()`](#dd) - Output values and terminate execution
935

10-
---
11-
1236
## `dump()`
1337

1438
Dumps one or more values to the console with detailed inspection and continues program execution. This is useful when you want to inspect values without stopping your application.
@@ -40,73 +64,71 @@ dump(...args: unknown[]): void
4064
#### Basic Usage
4165

4266
```typescript
43-
import { dump } from '@h3ravel/support'
67+
import { dump } from '@h3ravel/support';
4468

4569
const user = {
4670
id: 1,
4771
name: 'John Doe',
48-
email: 'john@example.com'
49-
}
72+
email: 'john@example.com',
73+
};
5074

51-
dump(user)
75+
dump(user);
5276
// Outputs the user object and continues execution
53-
console.log('Program continues...')
77+
console.log('Program continues...');
5478
```
5579

5680
#### Dumping Multiple Values
5781

5882
```typescript
59-
import { dump } from '@h3ravel/support'
83+
import { dump } from '@h3ravel/support';
6084

61-
const name = 'Alice'
62-
const age = 25
63-
const hobbies = ['reading', 'coding', 'gaming']
85+
const name = 'Alice';
86+
const age = 25;
87+
const hobbies = ['reading', 'coding', 'gaming'];
6488

65-
dump(name, age, hobbies)
89+
dump(name, age, hobbies);
6690
// Outputs all three values in order
6791
```
6892

6993
#### Debugging in Functions
7094

7195
```typescript
72-
import { dump } from '@h3ravel/support'
96+
import { dump } from '@h3ravel/support';
7397

7498
function processOrder(order: any) {
75-
dump(order) // Inspect the order
76-
99+
dump(order); // Inspect the order
100+
77101
// Continue processing...
78-
const total = order.items.reduce((sum, item) => sum + item.price, 0)
79-
80-
dump(total) // Inspect the calculated total
81-
82-
return total
102+
const total = order.items.reduce((sum, item) => sum + item.price, 0);
103+
104+
dump(total); // Inspect the calculated total
105+
106+
return total;
83107
}
84108
```
85109

86110
#### Inspecting Complex Objects
87111

88112
```typescript
89-
import { dump } from '@h3ravel/support'
113+
import { dump } from '@h3ravel/support';
90114

91115
const complexData = {
92116
user: {
93117
profile: {
94118
settings: {
95119
theme: 'dark',
96-
notifications: true
97-
}
98-
}
120+
notifications: true,
121+
},
122+
},
99123
},
100124
metadata: new Map([['key1', 'value1']]),
101-
created: new Date()
102-
}
125+
created: new Date(),
126+
};
103127

104-
dump(complexData)
128+
dump(complexData);
105129
// Shows complete nested structure with all details
106130
```
107131

108-
---
109-
110132
## `dd()`
111133

112134
Dumps one or more values to the console with detailed inspection and then **terminates the program**. This is Laravel's famous "Dump and Die" function, perfect for debugging when you want to inspect a value and stop execution immediately.
@@ -138,102 +160,99 @@ dd(...args: unknown[]): never
138160
#### Basic Usage
139161

140162
```typescript
141-
import { dd } from '@h3ravel/support'
163+
import { dd } from '@h3ravel/support';
142164

143165
const user = {
144166
id: 1,
145167
name: 'John Doe',
146-
email: 'john@example.com'
147-
}
168+
email: 'john@example.com',
169+
};
148170

149-
dd(user)
171+
dd(user);
150172
// Outputs the user object and STOPS execution
151-
console.log('This line will NEVER execute')
173+
console.log('This line will NEVER execute');
152174
```
153175

154176
#### Quick Debugging in Middleware
155177

156178
```typescript
157-
import { dd } from '@h3ravel/support'
179+
import { dd } from '@h3ravel/support';
158180

159181
function authenticate(request: any) {
160-
const token = request.headers.authorization
161-
162-
dd(token) // Inspect token and stop here
163-
182+
const token = request.headers.authorization;
183+
184+
dd(token); // Inspect token and stop here
185+
164186
// Nothing below this line will execute
165-
return validateToken(token)
187+
return validateToken(token);
166188
}
167189
```
168190

169191
#### Debugging API Responses
170192

171193
```typescript
172-
import { dd } from '@h3ravel/support'
194+
import { dd } from '@h3ravel/support';
173195

174196
async function fetchUser(id: number) {
175-
const response = await fetch(`/api/users/${id}`)
176-
const data = await response.json()
177-
178-
dd(data) // Inspect the response and stop
179-
180-
return data // This will never be reached
197+
const response = await fetch(`/api/users/${id}`);
198+
const data = await response.json();
199+
200+
dd(data); // Inspect the response and stop
201+
202+
return data; // This will never be reached
181203
}
182204
```
183205

184206
#### Inspecting Multiple Values Before Exit
185207

186208
```typescript
187-
import { dd } from '@h3ravel/support'
209+
import { dd } from '@h3ravel/support';
188210

189211
function calculateTotal(items: any[]) {
190-
const subtotal = items.reduce((sum, item) => sum + item.price, 0)
191-
const tax = subtotal * 0.1
192-
const total = subtotal + tax
193-
194-
dd(subtotal, tax, total) // Inspect all values and stop
212+
const subtotal = items.reduce((sum, item) => sum + item.price, 0);
213+
const tax = subtotal * 0.1;
214+
const total = subtotal + tax;
215+
216+
dd(subtotal, tax, total); // Inspect all values and stop
195217
}
196218
```
197219

198-
---
199-
200220
## Comparison: `dump()` vs `dd()`
201221

202-
| Feature | `dump()` | `dd()` |
203-
|---------|----------|--------|
204-
| Output to console | ✅ Yes | ✅ Yes |
205-
| Detailed inspection | ✅ Yes | ✅ Yes |
206-
| Shows hidden properties | ✅ Yes | ✅ Yes |
207-
| Colored output | ✅ Yes | ✅ Yes |
208-
| Continues execution | ✅ Yes | ❌ No |
209-
| Terminates process | ❌ No | ✅ Yes (exit code 1) |
210-
| Use case | Debug without stopping | Debug and stop immediately |
211-
212-
---
222+
| Feature | `dump()` | `dd()` |
223+
| ----------------------- | ---------------------- | -------------------------- |
224+
| Output to console | ✅ Yes | ✅ Yes |
225+
| Detailed inspection | ✅ Yes | ✅ Yes |
226+
| Shows hidden properties | ✅ Yes | ✅ Yes |
227+
| Colored output | ✅ Yes | ✅ Yes |
228+
| Continues execution | ✅ Yes | ❌ No |
229+
| Terminates process | ❌ No | ✅ Yes (exit code 1) |
230+
| Use case | Debug without stopping | Debug and stop immediately |
213231

214232
## When to Use Each Helper
215233

216234
### Use `dump()` when:
235+
217236
- You want to inspect values at multiple points in your code
218237
- You need your application to continue running
219238
- You're debugging in a loop or recurring process
220239
- You want to trace the flow of data through your application
221240

222241
### Use `dd()` when:
242+
223243
- You want to inspect a value and stop execution immediately
224244
- You're debugging an issue and don't need code after that point to run
225245
- You want to prevent side effects from occurring after a certain point
226246
- You're doing quick debugging and want immediate feedback
227247

228-
---
229-
230248
## Tips
231249

232250
1. **Remove before production**: Both helpers are meant for development. Remove them before deploying to production.
233251

234252
2. **Multiple values**: Both functions accept multiple arguments, which is useful for comparing values:
253+
235254
```typescript
236-
dd(expected, actual, difference)
255+
dd(expected, actual, difference);
237256
```
238257

239258
3. **Deep inspection**: Unlike `console.log()`, these helpers show the complete structure of objects without truncation.
@@ -242,18 +261,17 @@ function calculateTotal(items: any[]) {
242261

243262
5. **Chaining**: Since `dump()` returns `void`, you can't chain it, but you can use it inline:
244263
```typescript
245-
const result = someFunction()
246-
dump(result)
247-
return result
264+
const result = someFunction();
265+
dump(result);
266+
return result;
248267
```
249268

250-
---
251-
252269
## Implementation Details
253270

254271
Both helpers use Node.js's `util.inspect()` with the following options:
272+
255273
- `showHidden: true` - Shows non-enumerable properties
256274
- `depth: null` - No depth limit (shows complete structure)
257275
- `colors: true` - Colored output for better readability
258276

259-
The output is sent to `console.log()` for each argument, and `dd()` calls `process.exit(1)` after dumping.
277+
The output is sent to `console.log()` for each argument, and `dd()` calls `process.exit(1)` after dumping.

0 commit comments

Comments
 (0)