location-util
Utilities of location (URL) for browser and node. It has no dependencies.
Synopsis
var l = 'http://example.com:3000/foo?bar=buz#frag';lprotocol; // => 'http'lhost; // => 'example.com'lport; // => 3000l; // => {'bar': 'buz'}l; // => '?bar=buz'lhash; // => 'frag'l; // => '/foo'l; // => '/foo?bar=buz#frag'l; // => 'http://example.com:3000' l; // => 'http://example.com:3000/user?id=123#name'l; // => 'http://example.com:3000/entry?id=123#name'l; // => 'http://example.com:3000/entry?date=20140401#name'lhash''; // => 'http://example.com:3000/entry?date=20140401'
Methods
new Locationutil(url)
Creates an instance. It takes url
as string.
absUrl()
This method provides getter only.
It returns full URL.
protocol()
This method provides getter only.
It returns protocol (e.g. http
). If protocol is empty, it returns blank string.
host()
This method provides getter only.
It returns the part of host.
port()
This method provides getter only.
It returns port number. If port number is empty, it returns null
.
search([queries])
This method provides getter and setter.
If you use this method without any arguments, this method behave as getter. It returns the query as object (e.g. {'bar': 'buz'}
).
If query is empty, it returns blank object.
The another case, this method behave as setter. It changes query according to arguments and returns changed instance. If value of queries is null, the property specified via the first argument will be deleted.
var l = 'http://example.com?foo=bar';l; // => {'foo': 'bar'}l;l; // => {'foo': 'bar', 'buz': 'qux'}l;l; // => {'buz': 'qux'}
paramString()
This method provides getter only.
This method returns query parameter string like a ?foo=bar&buz=qux
. This method doesn't ensure the order of key-values.
var l = 'http://example.com?foo=bar';l; // => '?foo=bar'l;l; // => '?foo=bar&buz=qux'
path(pathString)
This method provides getter and setter.
If you use this method without any arguments, this method behave as getter. It returns the part of path (e.g. /foo/bar
).
If path is empty, it returns '/'.
The another case, this method behave as setter. It changes path according to arguments and returns changed instance. Path should always begin with forward slash ('/'), this method will add the forward slash if it is missing.
var l = 'http://example.com/foo';l; // => '/foo'l;l; // => '/bar/buz'
hash(hashString)
This method provides getter and setter.
If you use this method without any arguments, this method behave as getter. It returns the part of hash fragment. If hash is empty, it returns blank string.
The another case, this method behave as setter. It changes hash fragment according to arguments and returns changed instance.
var l = 'http://example.com#foo';lhash; // => 'foo'lhash'bar';lhash; // => 'bar'
url(urlString)
This method provides getter and setter.
If you use this method without any arguments, this method behave as getter. It returns the URL (e.g. /foo?bar=buz#frag). If URL is empty, it returns blank string.
The another case, this method behave as setter. It changes URL according to arguments and returns changed instance. URL should always begin with forward slash ('/'), this method will add the forward slash if it is missing.
var l = 'http://example.com/foo?bar=buz#frag';l; // => '/foo?bar=buz#frag'l;l; // => '/user?id=123#name'
origin()
This method provides only getter.
This method returns a string like so <protocol>://<host>:<port>
.
If underlying URL doesn't have protocol
or port
,
it will omit them from result.
var l = 'http://example.com:3000/foo?bar=buz#frag';l; // => 'http://example.com:3000' l = 'http://example.com/foo?bar=buz#frag';l; // => 'http://example.com'
Author
moznion (moznion@gmail.com)
License
MIT