![]() |
v1.3.0.0
|
Extends the standard Web API URL type with some static utility functions and instance prototype properties & methods, especially for working with local paths and "file://" type URLs.
Properties | |
boolean | isEmpty |
boolean | isLocalPath |
boolean | isRelative |
boolean | isValid |
Public Member Functions | |
class URLURL | resolved (< string|URL > relative) |
string | toLocalPath () |
Static Public Member Functions | |
URL | fromLocalPath (string path) |
URL | fromPathOrUrl (string input, string baseDirectory="") |
boolean | isEmpty (< string|URL > input) |
boolean | isLocalPath (< string|URL > input) |
boolean | isRelative (< string|URL > input) |
boolean | isValid (< string|URL > input) |
URL | resolved (< string|URL > base,< string|URL > relative) |
string | scheme (< string|URL > url) |
string | toLocalPath (< string|URL > url) |
URL | url (< string|URL > input) |
|
read |
Returns true
if this URL has no data; otherwise returns false
.
|
read |
|
read |
Returns true
if this URL is relative; otherwise returns false
.
A URL is relative reference if its scheme is undefined.
|
read |
class URLURL resolved | ( | < string|URL > | relative | ) |
Returns the result of the merge of this URL with relative
.
This URL is used as a base to convert relative URLs to absolute.
If relative
is not a relative URL, this method will return relative
directly. Otherwise, the paths of the two URLs are merged, and the new URL returned has the scheme and authority of the base URL, but with the merged path, as in the following example:
string toLocalPath | ( | ) |
Returns this URL formatted as a local file path.
The path returned will use native directory separators.
If this URL contains a non-empty hostname, it will be encoded in the returned value in the form found on SMB networks (for example, "//servername/path/to/file.txt").
Note: if the path component of this URL contains a non-UTF-8 binary sequence (such as %80
), the behavior of this method is undefined.
|
static |
Returns a URL
representation of path
, interpreted as a local file path.
This function accepts paths separated by slashes as well as the native separator for this platform.
This function also accepts paths with a doubled leading slash (or backslash) to indicate a remote file, as in "//servername/path/to/file.txt".
|
static |
Returns a valid URL
from input
string if one can be deduced, or if that is not possible, returns an invalid (empty) URL.
This function can be useful when processing arbitrary user input, for example, when the user may provide an actual URL, or a relative or absolute system file path.
When the string is not already a valid URL, a best guess is performed, making various assumptions:
URL.fromLocalPath()
.In order to be able to handle relative paths, this method takes an optional basePath
path argument against which relative paths are resolved into absolute paths. If baseDirectory
is empty, no handling of relative paths will be done (an invalid (empty) URL will be returned for relative paths).
Input strings that look like relative paths will always return a valid URL, even if the file doesn't actually exist on the system.
Some examples:
Although similar to the new URL()
constructor or URL.parse()
, this function:
baseDirectory
) if the input is already a fully qualified URL or absolute path.null
, like URL.parse()
does).
|
static |
Returns true
if the input
URL has no data; otherwise returns false
.
|
static |
Returns true
if the input
URL is pointing to a local file path.
A URL is a local file path if the scheme is "file".
|
static |
Returns true
if the input
URL is relative; otherwise returns false
.
A URL is relative reference if its scheme is undefined.
|
static |
Returns true
if the input
URL is non-empty and valid; otherwise returns false
.
The input
value is run through a conformance test. Every part of the URL must conform to the standard encoding rules of the URI standard for the URL to be reported as valid.
Returns the result of the merge of base
URL with relative
.
The base
URL is used as a base to convert relative URLs to absolute.
If relative
is not a relative URL, this function will return relative
directly. Otherwise, the paths of the two URLs are merged, and the new URL returned has the scheme and authority of the base URL, but with the merged path, as in the following example:
|
static |
Returns the scheme of the given url
, up to, but not including, the :
separator ("http", "ftp", "file", etc).
If an empty string is returned, this means the scheme is undefined and the URL is then relative.
The scheme can only contain US-ASCII letters or digits, which means it cannot contain any character that would otherwise require encoding. Additionally, schemes are always returned in lowercase form.
On Windows, if the url is a local file path starting with a drive letter (eg. "c:\temp"), then that drive letter is returned as the scheme. Since no actual URL schemes are one character long, one can then assume it is a file path, not an actual URL.
|
static |
Returns the url
URL formatted as a local file path.
The path returned will use native directory separators.
If url
contains a non-empty hostname, it will be encoded in the returned value in the form found on SMB networks (for example, "//servername/path/to/file.txt").
Note: if the path component of url
contains a non-UTF-8 binary sequence (such as %80
), the behavior of this function is undefined.
Returns a new URL
instance verbatim from given input
, which can be a string or another URL instance.
The input is not parsed or validated. This can be used to force a type coercion to URL
. In contrast to new URL()
or URL.parse()
, URL.url()
always returns a URL
object, even if the input is an invalid URL. You can check validity with newUrl.isValid
or URL.isValid(newUrl)
.