URL Encode / Decode — TypeScript source
Percent-encode or decode URLs and query parameters. Choose component (encodeURIComponent) or full-URI (encodeURI) mode. 100% client-side.
This is the TypeScript implementation — the same logic the interactive tool runs, in a shareable, citable form.
// Pure surface of the url-encode tool (slug: url-encode). The operations are
// the four platform builtins, but pinning the mode × scope matrix here with
// safe-character vectors defines what a Go twin must reproduce — Go's
// net/url escaping differs (QueryEscape uses '+' for spaces; JS always uses
// %20; the unreserved sets do not match).
export type UrlCodeMode = 'encode' | 'decode';
export type UrlScope = 'component' | 'uri';
export type UrlTransformResult =
| { output: string; error: null }
| { output: ''; error: null } // empty input
| { output: ''; error: string };
/** Encode/decode under the component scope (encodeURIComponent — everything
* but A-Z a-z 0-9 - _ . ! ~ * ' ( )) or the full-URI scope (encodeURI —
* additionally preserves ; / ? : @ & = + $ , #). Malformed % sequences on
* decode surface the platform URIError message. */
export function transformUrl(
input: string,
mode: UrlCodeMode,
scope: UrlScope,
): UrlTransformResult {
if (!input) return { output: '', error: null };
try {
if (mode === 'encode') {
return { output: scope === 'component' ? encodeURIComponent(input) : encodeURI(input), error: null };
}
return { output: scope === 'component' ? decodeURIComponent(input) : decodeURI(input), error: null };
} catch (e) {
return { output: '', error: e instanceof Error ? e.message : 'Malformed URI sequence' };
}
}
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →