Skip to content

url

Contains utility functions for validating, encoding and normalizing URLs.

FunctionDescription
encodeUrlPercent-encodes the symbols that break a URL: backslashes, spaces and the control characters. Every other character — including non-ASCII ones and a literal % — is left as is, so the result stays readable. A literal % is only encoded when shouldEncodePercent is set. Leaving it alone is the default because it keeps the encoding idempotent for already-encoded input; the cost is that a path which genuinely contains % (a folder actually named a%20b) decodes back to something else. Turn the flag on whenever the input is a raw filesystem path that may contain %.
isFileUrlDetermines whether a given string is a file:// URL.
isUrlDetermines whether a given string is a valid URL
normalizeFileUrlNormalizes a file:// URL to a pretty form by converting backslashes to forward slashes. The URL is expected to already be decoded. Non-file:// URLs are returned unchanged.
pathToFileUrlConverts a filesystem path into a file:/// URL, percent-encoding it with encodeUrl. The backslashes are turned into forward slashes after the encoding, not before — a Windows path is encoded to %5C first and only then rewritten to /, which is what makes C:\a b\c produce the working file:///C:/a%20b/c rather than a link broken at the first space. The file:/// prefix always ends up with exactly three slashes: any leading slashes the path already has are dropped first, because file:////home/x parses as the path //home/x, not /home/x. A Windows UNC path (\\server\share) is therefore rendered as file:///server/share rather than the canonical host form file://server/share, which this function deliberately does not produce.