Skip to content

Commit a153fc5

Browse files
refactor(routeNormalization): extract helpers for improved readability
Extracted validation and transformation logic into self-documenting helper functions: - isValidRecordInput(): Type guard clearly stating 'is object and not array' - shouldRejectAsArray(): Explains the design decision to reject arrays - buildStringOnlyRecord(): Separates string-filtering logic with clear intent Benefits: - Removed inline conditional logic that required explanatory comments - Each function name now conveys purpose without additional documentation - Improved testability of individual concerns - Simplified main normalizeRouteRecord() function body All 9 routeNormalization tests pass with no regressions. Full test suite validates: 2317 tests across 171 files. TypeScript type checking clean. Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent 0565704 commit a153fc5

1 file changed

Lines changed: 16 additions & 3 deletions

File tree

src/services/routeNormalization.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,33 @@ export function normalizeRouteRecord(
2323
value: unknown,
2424
source: 'params' | 'query',
2525
): Record<string, string> {
26-
if (typeof value !== 'object' || value === null) {
26+
if (!isValidRecordInput(value)) {
2727
return {}
2828
}
2929

30-
if (Array.isArray(value)) {
30+
if (shouldRejectAsArray(value)) {
3131
logger.warn('Validation route normalization rejected array input', {
3232
source,
3333
})
3434
return {}
3535
}
3636

37+
return buildStringOnlyRecord(value as Record<string, unknown>, source)
38+
}
39+
40+
function isValidRecordInput(value: unknown): value is Record<string, unknown> {
41+
return typeof value === 'object' && value !== null && !Array.isArray(value)
42+
}
43+
44+
function shouldRejectAsArray(value: unknown): value is unknown[] {
45+
return Array.isArray(value)
46+
}
47+
48+
function buildStringOnlyRecord(record: Record<string, unknown>, source: 'params' | 'query'): Record<string, string> {
3749
const result: Record<string, string> = {}
3850
const droppedKeys: string[] = []
39-
for (const [key, entry] of Object.entries(value)) {
51+
52+
for (const [key, entry] of Object.entries(record)) {
4053
if (typeof entry === 'string') {
4154
result[key] = entry
4255
} else {

0 commit comments

Comments
 (0)