Skip to content

Commit 34d7312

Browse files
Add JSDoc to public API (#513)
Add JSDoc comments to utility functions Co-authored-by: Cursor Agent <cursoragent@cursor.com>
1 parent d89eb21 commit 34d7312

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

src/utils/useClientOnlyRender.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { useEffect, useState } from 'react'
22

3+
/**
4+
* Returns `true` only after the first client render. Useful for avoiding
5+
* hydration mismatches when markup differs between SSR and client.
6+
*/
37
export function useClientOnlyRender() {
48
const [rendered, setRendered] = useState(false)
59
useEffect(() => {

src/utils/useLocalStorage.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ function getWithExpiry<T>(key: string) {
2222
}
2323
}
2424

25+
/**
26+
* React state that persists to `localStorage` (with optional TTL).
27+
*
28+
* - `key`: localStorage key to read/write
29+
* - `defaultValue`: initial value if no stored value
30+
* - `ttl` (ms): optional time-to-live; expired values are cleared and ignored
31+
*/
2532
export function useLocalStorage<T>(
2633
key: string,
2734
defaultValue: T,

src/utils/utils.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
/**
2+
* Uppercases the first character of a string and returns the result.
3+
*/
14
export function capitalize(str: string) {
25
return str.charAt(0).toUpperCase() + str.slice(1)
36
}
47

8+
/**
9+
* Converts a kebab-case slug (eg. "my-example") into a Title Case string.
10+
*/
511
export function slugToTitle(str: string) {
612
return str
713
.split('-')
@@ -18,11 +24,18 @@ export function slugToTitle(str: string) {
1824
// },
1925
// }
2026

27+
/**
28+
* Returns the last element from an array.
29+
*/
2130
export function last<T>(arr: T[]) {
2231
return arr[arr.length - 1]
2332
}
2433

25-
// Generates path replacing tokens with params
34+
/**
35+
* Generates a route path by replacing token segments in an id with params.
36+
* The id uses dot-notation which is converted to slashes; `$param` tokens are
37+
* replaced from `params`, and `$*` is treated as the catch-all segment.
38+
*/
2639
export function generatePath(
2740
id: string,
2841
params: Record<string, string | undefined>
@@ -36,6 +49,9 @@ export function generatePath(
3649
return result
3750
}
3851

52+
/**
53+
* Returns a shallow-copied array with items shuffled.
54+
*/
3955
export function shuffle<T>(arr: T[]) {
4056
const random = Math.random()
4157
const result = arr.slice()
@@ -50,10 +66,18 @@ export function shuffle<T>(arr: T[]) {
5066
return result
5167
}
5268

69+
/**
70+
* Returns a single random element from an array. Accepts an optional
71+
* `random` function for deterministic tests.
72+
*/
5373
export function sample(arr: any[], random = Math.random()) {
5474
return arr[Math.floor(random * arr.length)]
5575
}
5676

77+
/**
78+
* Sorts an array by a computed value. Undefined values are ordered last and
79+
* numeric strings are coerced to numbers for intuitive sorting.
80+
*/
5781
export function sortBy<T>(arr: T[], accessor: (d: T) => any = (d) => d): T[] {
5882
return arr
5983
.map((d: any, i: any) => [d, i])
@@ -76,6 +100,9 @@ export function sortBy<T>(arr: T[], accessor: (d: T) => any = (d) => d): T[] {
76100
.map((d: any) => d[0])
77101
}
78102

103+
/**
104+
* Returns true if the string fully represents a number (no extra characters).
105+
*/
79106
export function isNumericString(str: string): boolean {
80107
if (typeof str !== 'string') {
81108
return false // we only process strings!
@@ -134,6 +161,10 @@ export function removeLeadingSlash(path: string): string {
134161
return path.replace(/^\//, '')
135162
}
136163

164+
/**
165+
* Measures and logs the execution time of the provided function and returns
166+
* its result. Works with both sync and async functions.
167+
*/
137168
export async function logTime<T>(
138169
lable: string,
139170
fn: () => T

0 commit comments

Comments
 (0)