urlFix runs decodeURIComponent on a URL and (optionally) replaces the first space with a chosen separator. Use it to render Persian Wikipedia / blog URLs in UI without the noisy %xx escapes.
The exported function isurlFix(lowercaseu). The name is notURLfix.
Function
Behaviour
- Falsy input →
undefined. decodeURIComponent(url)— expands all%xxescapes, including UTF-8 multi-byte sequences for Persian and other non-ASCII characters.- If
separatoris provided, the first space in the decoded URL is replaced withseparator(singleString.replace, not global).
Pitfalls
- Separator replacement is single-shot. Only the first space is replaced. Subsequent spaces stay literal.
- The result is NOT re-encoded. It’s human-readable but no longer a valid
fetchtarget. Use it for display, logs, or slug input — keep the encoded form for HTTP requests. decodeURIComponentthrows on malformed percent-encoding (e.g. lone%, invalid UTF-8 bytes). Wrap intry/catchfor untrusted input.- Returns
undefined(not"") on falsy input. Type the call site accordingly:const pretty = urlFix(raw) ?? raw.
Source
src/modules/URLfix/index.ts · Tests: test/URLfix.spec.ts