Skip to content

tryDecodeURI

reference
1 min readUpdated

Kind: Function

Source: src/utils/url.ts

Part of: Utils

Try to apply decodeURI() to given string. If it fails, skip invalid percent encoding or invalid UTF-8 sequences, and apply decodeURI() to the rest as much as possible.

tryDecodeURI attempts to decode a URI string with decodeURI(). If decoding fails because the input contains invalid percent encoding or invalid UTF-8 sequences, it skips the invalid data and decodes the remaining portions where possible.

Signature

ts
function tryDecodeURI(str: string): string

Parameters

NameType
strstring

Returns: string

Diagram

mermaid
graph LR
  Input[URI string] --> Decode[Attempt decodeURI]
  Decode -->|Success| Result[Return decoded string]
  Decode -->|Failure| Recover[Skip invalid encoding data]
  Recover --> Partial[Decode remaining portions]
  Partial --> Result

Usage

ts
import { tryDecodeURI } from './utils/url';

const value = 'https://example.com/search?q=hello%20world%ZZ';

const decoded = tryDecodeURI(value);

console.log(decoded);
// https://example.com/search?q=hello world%ZZ

AI Coding Instructions

  • Keep this helper focused on tolerant URI decoding behavior.
  • Use decodeURI() rather than decodeURIComponent() so URI delimiters retain their expected handling.
  • Do not throw when malformed percent encoding or invalid UTF-8 data is present.
  • Preserve undecodable input data while decoding portions that can be decoded.

Used by

1 reference from 1 file. Each is a place in this repository where the symbol is actually used — go read one rather than trusting an example.

Imported by (1)

  • ServeStaticOptionssrc/middleware/serve-static/index.ts:13

Was this page helpful?

Download as PDF
tryDecodeURI — Hono (narrator proof)