url
Contains utility functions for validating, encoding and normalizing URLs.
Functions
Section titled “Functions”| Function | Description |
|---|---|
| encodeUrl | Percent-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 %. |
| isFileUrl | Determines whether a given string is a file:// URL. |
| isUrl | Determines whether a given string is a valid URL |
| normalizeFileUrl | Normalizes 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. |
| pathToFileUrl | Converts 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. |