Comparing version 0.1.5 to 0.2.0
106
index.js
@@ -8,2 +8,3 @@ 'use strict'; | ||
var keys = ',,protocol,username,password,host,hostname,port,pathname,query,hash'.split(',') | ||
, inherit = { protocol: 1, host: 1, hostname: 1 } | ||
, parts = keys.length; | ||
@@ -37,3 +38,8 @@ | ||
// | ||
var regexp = /^(?:(?:(([^:\/#\?]+:)?(?:(?:\/\/)(?:(?:(?:([^:@\/#\?]+)(?:\:([^:@\/#\?]*))?)@)?(([^:\/#\?\]\[]+|\[[^\/\]@#?]+\])(?:\:([0-9]+))?))?)?)?((?:\/?(?:[^\/\?#]+\/+)*)(?:[^\?#]*)))?(\?[^#]+)?)(#.*)?/; | ||
var regexp = /^(?:(?:(([^:\/#\?]+:)?(?:(?:\/\/)(?:(?:(?:([^:@\/#\?]+)(?:\:([^:@\/#\?]*))?)@)?(([^:\/#\?\]\[]+|\[[^\/\]@#?]+\])(?:\:([0-9]+))?))?)?)?((?:\/?(?:[^\/\?#]+\/+)*)(?:[^\?#]*)))?(\?[^#]+)?)(#.*)?/ | ||
, bits = regexp.exec(address) | ||
, type = typeof location | ||
, url = this | ||
, i = 0 | ||
, key; | ||
@@ -51,4 +57,2 @@ // | ||
// | ||
var type = typeof location; | ||
if ('object' !== type && 'string' !== type) { | ||
@@ -65,13 +69,13 @@ parser = location; | ||
for (var i = 0, bits = regexp.exec(address), key; i < parts; key = keys[++i]) { | ||
if (key) { | ||
this[key] = bits[i] || location[key] || ''; | ||
for (; i < parts; key = keys[++i]) { | ||
if (!key) continue; | ||
// | ||
// The protocol, host, host name should always be lower cased even if they | ||
// are supplied in uppercase. This way, when people generate an `origin` | ||
// it be correct. | ||
// | ||
if (i === 2 || i === 5 || i === 6) this[key] = this[key].toLowerCase(); | ||
} | ||
url[key] = bits[i] || (key in inherit ? location[key] || '' : ''); | ||
// | ||
// The protocol, host, host name should always be lower cased even if they | ||
// are supplied in uppercase. This way, when people generate an `origin` | ||
// it be correct. | ||
// | ||
if (i === 2 || i === 5 || i === 6) url[key] = url[key].toLowerCase(); | ||
} | ||
@@ -84,3 +88,3 @@ | ||
// | ||
if (parser) this.query = parser(this.query); | ||
if (parser) url.query = parser(url.query); | ||
@@ -92,5 +96,5 @@ // | ||
// | ||
if (!required(this.port, this.protocol)) { | ||
this.host = this.hostname; | ||
this.port = ''; | ||
if (!required(url.port, url.protocol)) { | ||
url.host = url.hostname; | ||
url.port = ''; | ||
} | ||
@@ -101,6 +105,51 @@ | ||
// | ||
this.href = this.toString(); | ||
url.href = url.toString(); | ||
} | ||
/** | ||
* This is convenience method for changing properties in the URL instance to | ||
* insure that they all propagate correctly. | ||
* | ||
* @param {String} prop Property we need to adjust. | ||
* @param {Mixed} value The newly assigned value. | ||
* @returns {URL} | ||
* @api public | ||
*/ | ||
URL.prototype.set = function set(part, value, fn) { | ||
var url = this; | ||
if ('query' === part) { | ||
if ('string' === typeof value) value = (fn || qs.parse)(value); | ||
url[part] = value; | ||
} else if ('port' === part) { | ||
url[part] = value; | ||
if (!required(value, url.protocol)) { | ||
url.host = url.hostname; | ||
url[part] = ''; | ||
} else if (value) { | ||
url.host = url.hostname +':'+ value; | ||
} | ||
} else if ('hostname' === part) { | ||
url[part] = value; | ||
if (url.port) value += ':'+ url.port; | ||
url.host = value; | ||
} else if ('host' === part) { | ||
url[part] = value; | ||
if (/\:\d+/.test(value)) { | ||
value = value.split(':'); | ||
url.hostname = value[0]; | ||
url.port = value[1]; | ||
} | ||
} else { | ||
url[part] = value; | ||
} | ||
url.href = url.toString(); | ||
return url; | ||
}; | ||
/** | ||
* Transform the properties back in to a valid and full URL string. | ||
@@ -115,15 +164,16 @@ * | ||
var result = this.protocol +'//' | ||
, query; | ||
var query | ||
, url = this | ||
, result = url.protocol +'//'; | ||
if (this.username) result += this.username +':'+ this.password +'@'; | ||
if (url.username) result += url.username +':'+ url.password +'@'; | ||
result += this.hostname; | ||
if (this.port) result += ':'+ this.port; | ||
result += url.hostname; | ||
if (url.port) result += ':'+ url.port; | ||
result += this.pathname; | ||
result += url.pathname; | ||
if (this.query) { | ||
if ('object' === typeof this.query) query = stringify(this.query); | ||
else query = this.query; | ||
if (url.query) { | ||
if ('object' === typeof url.query) query = stringify(url.query); | ||
else query = url.query; | ||
@@ -133,3 +183,3 @@ result += (query.charAt(0) === '?' ? '' : '?') + query; | ||
if (this.hash) result += this.hash; | ||
if (url.hash) result += url.hash; | ||
@@ -136,0 +186,0 @@ return result; |
{ | ||
"name": "url-parse", | ||
"version": "0.1.5", | ||
"version": "0.2.0", | ||
"description": "Parse URL in node using the URL module and in the browser using the DOM", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -64,7 +64,25 @@ # url-parse | ||
- `pathname`: URL path. | ||
- `query`: Prefixed with `?` | ||
- `query`: Parsed object containing query string, unless parsing is set to false. | ||
- `hash`: Prefixed with `#` | ||
- `href`: The full URL. | ||
## URL.stringify() | ||
## URL.set(key, value) | ||
A simple helper function to change parts of the URL and propagating it through | ||
all properties. When you set a new `host` you want the same value to be applied | ||
to `port` if has a different port number, `hostname` so it has a correct name | ||
again and `href` so you have a complete URL. | ||
```js | ||
var parsed = parse('http://google.com/parse-things'); | ||
parsed.set('hostname', 'yahoo.com'); | ||
console.log(parsed.href); // http://yahoo.com/parse-things | ||
``` | ||
It's aware of default ports so you cannot set a port 80 on an URL which has | ||
`http` as protocol. | ||
## URL.toString() | ||
The returned `url` object comes with a custom `toString` method which will | ||
@@ -79,4 +97,8 @@ generate a full URL again when called. The method accepts an extra function | ||
You would rarely need to use this method as the full URL is also available as | ||
`href` property. If you are using the `URL.set` method to make changes, this | ||
will automatically update. | ||
## License | ||
MIT |
124
test.js
@@ -104,22 +104,130 @@ describe('url-parse', function () { | ||
it('does not inherit auth from source object', function () { | ||
var data = parse('/foo', parse('http://foo:bar@sub.example.com')); | ||
assume(data.port).equals(''); | ||
assume(data.username).equals(''); | ||
assume(data.password).equals(''); | ||
assume(data.host).equals('sub.example.com'); | ||
assume(data.href).equals('http://sub.example.com/foo'); | ||
}); | ||
it('does not inherit hashes and query strings from source object', function () { | ||
var data = parse('/foo', parse('http://foo:bar@sub.example.com/bar?foo=bar#hash')); | ||
var data = parse('/foo', parse('http://sub.example.com/bar?foo=bar#hash')); | ||
assume(data.port).equals(''); | ||
assume(data.username).equals('foo'); | ||
assume(data.password).equals('bar'); | ||
assume(data.host).equals('sub.example.com'); | ||
assume(data.href).equals('http://foo:bar@sub.example.com/foo'); | ||
assume(data.href).equals('http://sub.example.com/foo'); | ||
}); | ||
it('does not inherit pathnames from the source', function () { | ||
var data = parse('http://localhost', parse('http://foo:bar@sub.example.com/bar?foo=bar#hash')); | ||
assume(data.port).equals(''); | ||
assume(data.host).equals('localhost'); | ||
assume(data.href).equals('http://localhost'); | ||
}); | ||
it('does not inherit port numbers', function () { | ||
var data = parse('http://localhost', parse('http://sub.example.com:808/')); | ||
assume(data.port).equals(''); | ||
assume(data.host).equals('localhost'); | ||
assume(data.href).equals('http://localhost'); | ||
}); | ||
it('accepts a string as source argument', function () { | ||
var data = parse('/foo', 'http://foo:bar@sub.example.com/bar?foo=bar#hash'); | ||
var data = parse('/foo', 'http://sub.example.com/bar?foo=bar#hash'); | ||
assume(data.port).equals(''); | ||
assume(data.username).equals('foo'); | ||
assume(data.password).equals('bar'); | ||
assume(data.host).equals('sub.example.com'); | ||
assume(data.href).equals('http://foo:bar@sub.example.com/foo'); | ||
assume(data.href).equals('http://sub.example.com/foo'); | ||
}); | ||
describe('#set', function () { | ||
it('correctly updates the host when setting port', function () { | ||
var data = parse('http://google.com/foo'); | ||
assume(data.set('port', 8080)).equals(data); | ||
assume(data.host).equals('google.com:8080'); | ||
assume(data.href).equals('http://google.com:8080/foo'); | ||
}); | ||
it('only sets port when its not default', function () { | ||
var data = parse('http://google.com/foo'); | ||
assume(data.set('port', 80)).equals(data); | ||
assume(data.host).equals('google.com'); | ||
assume(data.href).equals('http://google.com/foo'); | ||
assume(data.set('port', 443)).equals(data); | ||
assume(data.host).equals('google.com:443'); | ||
assume(data.href).equals('http://google.com:443/foo'); | ||
}); | ||
it('updates query with object', function () { | ||
var data = parse('http://google.com/?foo=bar'); | ||
assume(data.set('query', { bar: 'foo' })).equals(data); | ||
assume(data.query.foo).equals(undefined); | ||
assume(data.query.bar).equals('foo'); | ||
assume(data.href).equals('http://google.com/?bar=foo'); | ||
}); | ||
it('updates query with a string', function () { | ||
var data = parse('http://google.com/?foo=bar'); | ||
assume(data.set('query', 'bar=foo')).equals(data); | ||
assume(data.query.foo).equals(undefined); | ||
assume(data.query.bar).equals('foo'); | ||
assume(data.href).equals('http://google.com/?bar=foo'); | ||
assume(data.set('query', '?baz=foo')).equals(data); | ||
assume(data.query.bar).equals(undefined); | ||
assume(data.query.baz).equals('foo'); | ||
assume(data.href).equals('http://google.com/?baz=foo'); | ||
}); | ||
it('updates the port when updating host', function () { | ||
var data = parse('http://google.com/?foo=bar'); | ||
assume(data.set('host', 'yahoo.com:808')).equals(data); | ||
assume(data.hostname).equals('yahoo.com'); | ||
assume(data.host).equals('yahoo.com:808'); | ||
assume(data.port).equals('808'); | ||
assume(data.href).equals('http://yahoo.com:808/?foo=bar'); | ||
}); | ||
it('updates the host when updating hostname', function () { | ||
var data = parse('http://google.com:808/?foo=bar'); | ||
assume(data.set('hostname', 'yahoo.com')).equals(data); | ||
assume(data.hostname).equals('yahoo.com'); | ||
assume(data.host).equals('yahoo.com:808'); | ||
assume(data.port).equals('808'); | ||
assume(data.href).equals('http://yahoo.com:808/?foo=bar'); | ||
}); | ||
it('updates other values', function () { | ||
var data = parse('http://google.com/?foo=bar'); | ||
assume(data.set('protocol', 'https:')).equals(data); | ||
assume(data.protocol).equals('https:'); | ||
assume(data.href).equals('https://google.com/?foo=bar'); | ||
}); | ||
}); | ||
describe('fuzzy', function () { | ||
@@ -126,0 +234,0 @@ var fuzz = require('./fuzzy') |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22678
446
103