Comparing version
247
index.js
@@ -1,8 +0,38 @@ | ||
var urlParse = require('url').parse; | ||
var url = require("fast-url-parser"); | ||
var querystring = require('querystring'); | ||
var isBrowser = (typeof window !== "undefined"); | ||
var getDefaults = function(){ | ||
// the node url library is quite slow & is often a code bottleneck | ||
// this wraps fast-url-parser to return url-parser-like results | ||
function urlParse(href) { | ||
var parsed = url.parse(href); | ||
var result = { | ||
// fields from fast-url-parser | ||
_protocol: parsed._protocol, | ||
_href: parsed._href, | ||
_port: parsed._port, | ||
_query: parsed._query, | ||
auth: parsed.auth, | ||
slashes: parsed.slashes, | ||
host: parsed.host, | ||
hostname: parsed.hostname, | ||
hash: parsed.hash, | ||
search: parsed.search, | ||
pathname: parsed.pathname, | ||
_prependSlash: parsed._prependSlash, | ||
// fields that node url library returns too | ||
port: parsed._port === -1 ? null : parsed._port.toString(), | ||
path: (parsed.pathname || "") + (parsed.search || ""), | ||
href: href, | ||
query: parsed.search ? parsed.search.slice(1) : parsed.search, | ||
protocol: parsed._protocol + ":" | ||
}; | ||
return result; | ||
} | ||
var getDefaults = function () { | ||
var defaultUrl = "http://localhost:80"; | ||
if (isBrowser){ | ||
if (isBrowser) { | ||
defaultUrl = window.location.href.toString(); | ||
@@ -20,6 +50,6 @@ } | ||
var objectEach = function(obj, cb){ | ||
for (var k in obj){ | ||
var objectEach = function (obj, cb) { | ||
for (var k in obj) { | ||
if (Object.prototype.hasOwnProperty.call(obj, k)) { | ||
cb(obj[k], k); | ||
cb(obj[k], k); | ||
} | ||
@@ -29,22 +59,22 @@ } | ||
var argsArray = function(obj){ | ||
if (!obj) { return []; } | ||
if (Array.isArray(obj)) { return obj.slice() ; } | ||
var args = []; | ||
objectEach(obj, function(v, k){ | ||
args[k] = v; | ||
}); | ||
return args; | ||
}; | ||
var argsArray = function (obj) { | ||
if (!obj) { return []; } | ||
if (Array.isArray(obj)) { return obj.slice(); } | ||
var args = []; | ||
objectEach(obj, function (v, k) { | ||
args[k] = v; | ||
}); | ||
return args; | ||
}; | ||
var arrLast = function(arr){ | ||
return arr[arr.length-1]; | ||
var arrLast = function (arr) { | ||
return arr[arr.length - 1]; | ||
}; | ||
var arrFlatten = function(input, output) { | ||
var arrFlatten = function (input, output) { | ||
if (!output) { output = []; } | ||
for (var i = 0; i < input.length; i++){ | ||
for (var i = 0; i < input.length; i++) { | ||
var value = input[i]; | ||
if (Array.isArray(value)) { | ||
arrFlatten(value, output); | ||
arrFlatten(value, output); | ||
} else { | ||
@@ -58,4 +88,4 @@ output.push(value); | ||
var UrlGrey = function(url){ | ||
if (!url && isBrowser){ | ||
var UrlGrey = function (url) { | ||
if (!url && isBrowser) { | ||
url = window.location.href.toString(); | ||
@@ -67,23 +97,22 @@ } | ||
UrlGrey.prototype.parsed = function(){ | ||
if (!this._parsed){ | ||
UrlGrey.prototype.parsed = function () { | ||
if (!this._parsed) { | ||
this._parsed = urlParse(this.url); | ||
var defaults = getDefaults(); | ||
var p = this._parsed; | ||
p.protocol = p.protocol || defaults.protocol; | ||
p.protocol = p.protocol.slice(0,-1); | ||
if (p.hash){ | ||
p.protocol = p._protocol || defaults._protocol; | ||
if (p.hash) { | ||
p.hash = p.hash.substring(1); | ||
} | ||
p.username = ''; | ||
p.username = ''; | ||
p.password = ''; | ||
if (p.protocol !== 'file'){ | ||
if (p.protocol !== 'file') { | ||
p.port = parseInt(p.port, 10); | ||
if (p.auth){ | ||
if (p.auth) { | ||
var auth = p.auth.split(':'); | ||
p.username = auth[0]; | ||
p.username = auth[0]; | ||
p.password = auth[1]; | ||
} | ||
} | ||
if (isBrowser){ | ||
if (isBrowser) { | ||
p.hostname = p.hostname || defaults.hostname; | ||
@@ -95,15 +124,15 @@ } | ||
this._parsed = { | ||
protocol : this._parsed.protocol, | ||
auth: this._parsed.auth, | ||
host: this._parsed.host, | ||
port: this._parsed.port, | ||
hostname: this._parsed.hostname, | ||
hash: this._parsed.hash, | ||
search: this._parsed.search, | ||
query: this._parsed.query, | ||
pathname: this._parsed.pathname, | ||
path: this._parsed.path, | ||
href: this._parsed.href, | ||
username: this._parsed.username, | ||
password: this._parsed.password | ||
protocol: this._parsed.protocol, | ||
auth: this._parsed.auth, | ||
host: this._parsed.host, | ||
port: this._parsed.port, | ||
hostname: this._parsed.hostname, | ||
hash: this._parsed.hash, | ||
search: this._parsed.search, | ||
query: this._parsed.query, | ||
pathname: this._parsed.pathname, | ||
path: this._parsed.path, | ||
href: this._parsed.href, | ||
username: this._parsed.username, | ||
password: this._parsed.password | ||
}; | ||
@@ -116,7 +145,7 @@ | ||
UrlGrey.prototype.extendedPath = function(url){ | ||
if (url){ | ||
UrlGrey.prototype.extendedPath = function (url) { | ||
if (url) { | ||
var p = urlParse(url); | ||
var obj = new UrlGrey(this.toString()); | ||
if (p.hash){ | ||
if (p.hash) { | ||
p.hash = p.hash.substring(1); | ||
@@ -137,8 +166,8 @@ } | ||
UrlGrey.prototype.port = function(num){ | ||
UrlGrey.prototype.port = function (num) { | ||
var hostname = this.parsed().hostname; | ||
// setter | ||
if (num){ | ||
if (this.protocol() === 'file'){ | ||
if (num) { | ||
if (this.protocol() === 'file') { | ||
throw new Error("file urls don't have ports"); | ||
@@ -153,8 +182,8 @@ } | ||
var output = this._parsed.port; | ||
if (!output){ | ||
switch(this.protocol()){ | ||
case 'http' : return 80; | ||
case 'https' : return 443; | ||
default : return null; | ||
} | ||
if (!output) { | ||
switch (this.protocol()) { | ||
case 'http': return 80; | ||
case 'https': return 443; | ||
default: return null; | ||
} | ||
} | ||
@@ -164,6 +193,6 @@ return parseInt(output, 10); | ||
UrlGrey.prototype.query = function(mergeObject){ | ||
UrlGrey.prototype.query = function (mergeObject) { | ||
if (arguments.length === 0) { | ||
return querystring.parse(this.queryString()); | ||
} else if (mergeObject === null || mergeObject === false){ | ||
} else if (mergeObject === null || mergeObject === false) { | ||
return this.queryString(''); | ||
@@ -173,4 +202,4 @@ } else { | ||
var oldQuery = querystring.parse(this.queryString()); | ||
objectEach(mergeObject, function(v, k){ | ||
if (v === null || v === false){ | ||
objectEach(mergeObject, function (v, k) { | ||
if (v === null || v === false) { | ||
delete oldQuery[k]; | ||
@@ -187,8 +216,8 @@ } else { | ||
UrlGrey.prototype.rawQuery = function(mergeObject){ | ||
UrlGrey.prototype.rawQuery = function (mergeObject) { | ||
if (arguments.length === 0) { | ||
if (this.queryString().length === 0) { return {}; } | ||
return this.queryString().split("&").reduce(function(obj, pair) { | ||
return this.queryString().split("&").reduce(function (obj, pair) { | ||
pair = pair.split("="); | ||
@@ -200,3 +229,3 @@ var key = pair[0]; | ||
}, {}); | ||
} else if (mergeObject === null || mergeObject === false){ | ||
} else if (mergeObject === null || mergeObject === false) { | ||
return this.queryString(''); | ||
@@ -206,4 +235,4 @@ } else { | ||
var oldQuery = querystring.parse(this.queryString()); | ||
objectEach(mergeObject, function(v, k){ | ||
if (v === null){ | ||
objectEach(mergeObject, function (v, k) { | ||
if (v === null) { | ||
delete oldQuery[k]; | ||
@@ -215,3 +244,3 @@ } else { | ||
var pairs = []; | ||
objectEach(oldQuery, function(v, k){ | ||
objectEach(oldQuery, function (v, k) { | ||
pairs.push(k + '=' + v); | ||
@@ -231,8 +260,8 @@ }); | ||
// add a method called queryString that manipulates 'query' | ||
addPropertyGetterSetter('query', 'queryString'); | ||
addPropertyGetterSetter('pathname', 'path'); | ||
addPropertyGetterSetter('query', 'queryString'); | ||
addPropertyGetterSetter('pathname', 'path'); | ||
UrlGrey.prototype.path = function(){ | ||
UrlGrey.prototype.path = function () { | ||
var args = arrFlatten(argsArray(arguments)); | ||
if (args.length !== 0){ | ||
if (args.length !== 0) { | ||
var obj = new UrlGrey(this.toString()); | ||
@@ -243,7 +272,7 @@ var str = args.join('/'); | ||
args = str.split('/'); | ||
for(var i = 0; i < args.length; i++){ | ||
for (var i = 0; i < args.length; i++) { | ||
args[i] = this.encode(args[i]); | ||
} | ||
str = args.join('/'); | ||
if (str[0] !== '/'){ str = '/' + str; } | ||
if (str[0] !== '/') { str = '/' + str; } | ||
obj.parsed().pathname = str; | ||
@@ -255,5 +284,5 @@ return obj; | ||
UrlGrey.prototype.rawPath = function(){ | ||
UrlGrey.prototype.rawPath = function () { | ||
var args = arrFlatten(argsArray(arguments)); | ||
if (args.length !== 0){ | ||
if (args.length !== 0) { | ||
var obj = new UrlGrey(this.toString()); | ||
@@ -263,3 +292,3 @@ var str = args.join('/'); | ||
str = str.replace(/\/$/, ''); // remove all trailing slashes | ||
if (str[0] !== '/'){ str = '/' + str; } | ||
if (str[0] !== '/') { str = '/' + str; } | ||
obj.parsed().pathname = str; | ||
@@ -271,3 +300,3 @@ return obj; | ||
UrlGrey.prototype.encode = function(str){ | ||
UrlGrey.prototype.encode = function (str) { | ||
try { | ||
@@ -280,14 +309,14 @@ return encodeURIComponent(str); | ||
UrlGrey.prototype.decode = function(str){ | ||
UrlGrey.prototype.decode = function (str) { | ||
return decode(str); | ||
}; | ||
UrlGrey.prototype.parent = function(){ | ||
UrlGrey.prototype.parent = function () { | ||
// read-only. (can't SET parent) | ||
var pieces = this.path().split("/"); | ||
var popped = pieces.pop(); | ||
if (popped === ''){ // ignore trailing slash | ||
if (popped === '') { // ignore trailing slash | ||
popped = pieces.pop(); | ||
} | ||
if (!popped){ | ||
if (!popped) { | ||
throw new Error("The current path has no parent path"); | ||
@@ -298,4 +327,4 @@ } | ||
UrlGrey.prototype.rawChild = function(suffixes){ | ||
if (suffixes){ | ||
UrlGrey.prototype.rawChild = function (suffixes) { | ||
if (suffixes) { | ||
suffixes = argsArray(arguments); | ||
@@ -307,3 +336,3 @@ return this.query(false).hash('').rawPath(this.path(), suffixes); | ||
var last = arrLast(pieces); | ||
if ((pieces.length > 1) && (last === '')){ | ||
if ((pieces.length > 1) && (last === '')) { | ||
// ignore trailing slashes | ||
@@ -317,5 +346,5 @@ pieces.pop(); | ||
UrlGrey.prototype.child = function(suffixes){ | ||
UrlGrey.prototype.child = function (suffixes) { | ||
suffixes = argsArray(arguments); | ||
if (suffixes.length > 0){ | ||
if (suffixes.length > 0) { | ||
return this.query(false).hash('').path(this.path(), suffixes); | ||
@@ -327,3 +356,3 @@ } | ||
var last = arrLast(pieces); | ||
if ((pieces.length > 1) && (last === '')){ | ||
if ((pieces.length > 1) && (last === '')) { | ||
// ignore trailing slashes | ||
@@ -336,12 +365,12 @@ pieces.pop(); | ||
UrlGrey.prototype.toJSON = function(){ | ||
UrlGrey.prototype.toJSON = function () { | ||
return this.toString(); | ||
}; | ||
UrlGrey.prototype.toString = function(){ | ||
UrlGrey.prototype.toString = function () { | ||
var p = this.parsed(); | ||
var retval = this.protocol() + '://'; | ||
if (this.protocol() !== 'file'){ | ||
if (this.protocol() !== 'file') { | ||
var userinfo = p.username + ':' + p.password; | ||
if (userinfo !== ':'){ | ||
if (userinfo !== ':') { | ||
retval += userinfo + '@'; | ||
@@ -351,3 +380,3 @@ } | ||
var port = portString(this); | ||
if (port !== ''){ | ||
if (port !== '') { | ||
retval += ':' + port; | ||
@@ -358,6 +387,6 @@ } | ||
var qs = this.queryString(); | ||
if (qs){ | ||
if (qs) { | ||
retval += '?' + qs; | ||
} | ||
if (p.hash){ | ||
if (p.hash) { | ||
retval += '#' + p.hash; | ||
@@ -368,5 +397,5 @@ } | ||
var pathPieces = function(path){ | ||
var pathPieces = function (path) { | ||
var pieces = path.split('/'); | ||
for(var i = 0; i < pieces.length; i++){ | ||
for (var i = 0; i < pieces.length; i++) { | ||
pieces[i] = decode(pieces[i]); | ||
@@ -377,3 +406,3 @@ } | ||
var decode = function(str){ | ||
var decode = function (str) { | ||
try { | ||
@@ -386,10 +415,10 @@ return decodeURIComponent(str); | ||
var portString = function(o){ | ||
if (o.protocol() === 'https'){ | ||
if (o.port() === 443){ | ||
var portString = function (o) { | ||
if (o.protocol() === 'https') { | ||
if (o.port() === 443) { | ||
return ''; | ||
} | ||
} | ||
if (o.protocol() === 'http'){ | ||
if (o.port() === 80){ | ||
if (o.protocol() === 'http') { | ||
if (o.port() === 80) { | ||
return ''; | ||
@@ -454,11 +483,11 @@ } | ||
module.exports = function(url){ return new UrlGrey(url); }; | ||
module.exports = function (url) { return new UrlGrey(url); }; | ||
function addPropertyGetterSetter(propertyName, methodName){ | ||
if (!methodName){ | ||
function addPropertyGetterSetter(propertyName, methodName) { | ||
if (!methodName) { | ||
methodName = propertyName; | ||
} | ||
UrlGrey.prototype[methodName] = function(str){ | ||
UrlGrey.prototype[methodName] = function (str) { | ||
if (!str && str !== "") { | ||
return this.parsed()[propertyName]; | ||
return this.parsed()[propertyName]; | ||
} else { | ||
@@ -465,0 +494,0 @@ var obj = new UrlGrey(this.toString()); |
@@ -8,3 +8,3 @@ { | ||
], | ||
"version": "0.4.4", | ||
"version": "1.0.0", | ||
"bugs": { | ||
@@ -23,17 +23,17 @@ "url": "https://github.com/cainus/urlgrey/issues" | ||
], | ||
"dependencies": {}, | ||
"dependencies": { | ||
"fast-url-parser": "^1.1.3" | ||
}, | ||
"devDependencies": { | ||
"tape": "2.3.0", | ||
"jshint": "2.3.0", | ||
"jscoverage": "0.3.6", | ||
"mocha-lcov-reporter": "0.0.1", | ||
"coveralls": "2.5.0", | ||
"mocha": "1.8.1", | ||
"browserify": "2.35.2", | ||
"chai": "1.8.1", | ||
"istanbul": "0.1.45", | ||
"uglify-js": "2.4.3" | ||
"coveralls": "^3.1.0", | ||
"istanbul": "^0.4.5", | ||
"mocha-lcov-reporter": "^1.3.0", | ||
"uglify-js": "^3.13.9", | ||
"chai": "^4.3.4", | ||
"jscoverage": "^0.6.0", | ||
"jshint": "^2.13.0", | ||
"mocha": "^9.0.0" | ||
}, | ||
"engine": { | ||
"node": ">=0.8.6" | ||
"node": ">=14.0.0" | ||
}, | ||
@@ -40,0 +40,0 @@ "main": "index.js", |
@@ -28,5 +28,5 @@  | ||
##API specifics: | ||
## API specifics: | ||
###url.child([lastPart]) | ||
### url.child([lastPart]) | ||
@@ -39,3 +39,3 @@ Setter/getter for the last part of a path: | ||
``` | ||
###url.decode(encodedString); | ||
### url.decode(encodedString); | ||
Returns the decoded version of the input string using node's standard querystring.unescape(). | ||
@@ -46,3 +46,3 @@ ```javascript | ||
###url.encode(unencodedString); | ||
### url.encode(unencodedString); | ||
Returns the encoded version of the input string using node's standard querystring.escape(). | ||
@@ -53,3 +53,3 @@ ```javascript | ||
###url.hash([newHash]) | ||
### url.hash([newHash]) | ||
Setter/getter for the url fragment/anchor/hash of a path. | ||
@@ -61,3 +61,3 @@ ```javascript | ||
``` | ||
###url.hostname([newHostname]) | ||
### url.hostname([newHostname]) | ||
Setter/getter for the url hostname. | ||
@@ -69,3 +69,3 @@ ```javascript | ||
``` | ||
###url.parent(); | ||
### url.parent(); | ||
Get the parent URI of the current URI. (This property is read-only). | ||
@@ -77,3 +77,3 @@ ```javascript | ||
###url.password([newPassword]); | ||
### url.password([newPassword]); | ||
Setter/getter for the password portion of the url. | ||
@@ -85,3 +85,3 @@ ```javascript | ||
``` | ||
###url.extendedPath([string]); | ||
### url.extendedPath([string]); | ||
Setter/getter for the path, querystring and fragment portion of the url | ||
@@ -96,3 +96,3 @@ all at once. | ||
###url.path([mixed]); | ||
### url.path([mixed]); | ||
Setter/getter for the path portion of the url. | ||
@@ -112,3 +112,3 @@ ```javascript | ||
###url.port([newPort]); | ||
### url.port([newPort]); | ||
Setter/getter for the port portion of the url. | ||
@@ -122,3 +122,3 @@ ```javascript | ||
###url.protocol([newProtocol]); | ||
### url.protocol([newProtocol]); | ||
@@ -133,3 +133,3 @@ | ||
###url.query([mixed]); | ||
### url.query([mixed]); | ||
@@ -154,3 +154,3 @@ Setter/getter for the querystring using javascript objects. | ||
###url.queryString([newQueryString]); | ||
### url.queryString([newQueryString]); | ||
@@ -166,3 +166,3 @@ Setter/getter for the querystring using a plain string representation. This is lower-level than .query(), but allows complete control of the querystring. | ||
###url.rawChild(); | ||
### url.rawChild(); | ||
@@ -172,7 +172,7 @@ This method is the same as url.child() but does not automatically url-encode | ||
###url.rawPath(); | ||
### url.rawPath(); | ||
This method is the same as url.path() but does not automatically url-encode | ||
any part of the path. | ||
###url.rawQuery(); | ||
### url.rawQuery(); | ||
This method is the same as url.query() but does not automatically url-encode | ||
@@ -182,3 +182,3 @@ any of the keys or values in an input object. | ||
###url.toJson(); | ||
### url.toJson(); | ||
Returns the json representation of the uri object, which is simply the uri as a string. The output is exactly the same as .toString(). This method is read-only. | ||
@@ -189,3 +189,3 @@ ```javascript | ||
###url.toString(); | ||
### url.toString(); | ||
Returns the string representation of the uri object, which is simply the uri as a string. This method is read-only. | ||
@@ -196,3 +196,3 @@ ```javascript | ||
###url.username([newUsername]) | ||
### url.username([newUsername]) | ||
Setter/getter for the username portion of the url. | ||
@@ -205,3 +205,3 @@ ```javascript | ||
##Installation: | ||
## Installation: | ||
### node.js: | ||
@@ -218,12 +218,12 @@ `npm install urlgrey --save` | ||
##Contributing: | ||
###Testing: | ||
####Run the node tests: | ||
## Contributing: | ||
### Testing: | ||
#### Run the node tests: | ||
* `make test` | ||
####Run the browser file:// tests: | ||
#### Run the browser file:// tests: | ||
* `make browser-build` | ||
* ...then open test.html in a browser | ||
####Run the browser tests on a real server: | ||
#### Run the browser tests on a real server: | ||
* `make browser-build` | ||
@@ -233,6 +233,6 @@ * `python -m SimpleHTTPServer 9999` | ||
###Building before committing | ||
### Building before committing | ||
* `make precommit` | ||
###Running node tests with a coverage report | ||
### Running node tests with a coverage report | ||
* `make test-cov` | ||
@@ -239,0 +239,0 @@ |
var isBrowser = !(typeof module !== 'undefined' && module.exports); | ||
if (!isBrowser){ | ||
if (!isBrowser) { | ||
// non-browser code | ||
@@ -8,6 +8,7 @@ var chai = require('chai'); | ||
} | ||
var assert = chai.assert; | ||
describe("urlgrey", function(){ | ||
describe("chainability", function(){ | ||
it("doesn't over-write the original url", function(){ | ||
describe("urlgrey", function () { | ||
describe("chainability", function () { | ||
it("doesn't over-write the original url", function () { | ||
var urlStr = "https://user:pass@subdomain.asdf.com/path?asdf=1234#frag"; | ||
@@ -29,4 +30,4 @@ var url = urlgrey(urlStr); | ||
}); | ||
describe("#toJSON", function(){ | ||
it("returns the same thing as toString", function(){ | ||
describe("#toJSON", function () { | ||
it("returns the same thing as toString", function () { | ||
var url = "https://user:pass@subdomain.asdf.com/path?asdf=1234#frag"; | ||
@@ -36,12 +37,12 @@ urlgrey(url).toJSON().should.equal(urlgrey(url).toString()); | ||
}); | ||
describe("#hostname", function(){ | ||
it("gets the hostname", function(){ | ||
describe("#hostname", function () { | ||
it("gets the hostname", function () { | ||
var url = "https://user:pass@subdomain.asdf.com/path?asdf=1234#frag"; | ||
urlgrey(url).hostname().should.equal('subdomain.asdf.com'); | ||
}); | ||
if (isBrowser){ | ||
it("gets the hostname even if unset", function(){ | ||
if (isBrowser) { | ||
it("gets the hostname even if unset", function () { | ||
var url = "/path?asdf=1234#frag"; | ||
var u = urlgrey(url); | ||
if (u.protocol() === 'file'){ | ||
if (u.protocol() === 'file') { | ||
// chrome uses localhost. other browsers don't | ||
@@ -54,3 +55,3 @@ chai.expect((u.hostname() === '') || (u.hostname() === 'localhost')).to.eql(true); | ||
} | ||
it("sets the hostname", function(){ | ||
it("sets the hostname", function () { | ||
var url = "http://subdomain.asdf.com"; | ||
@@ -61,12 +62,12 @@ urlgrey(url).hostname("blah") | ||
}); | ||
describe("#port", function(){ | ||
it("gets the port", function(){ | ||
describe("#port", function () { | ||
it("gets the port", function () { | ||
var url = "https://user:pass@subdomain.asdf.com:9090"; | ||
urlgrey(url).port().should.equal(9090); | ||
}); | ||
it("gets a correct default port when it's missing", function(){ | ||
it("gets a correct default port when it's missing", function () { | ||
var url = "https://user:pass@subdomain.asdf.com"; | ||
urlgrey(url).port().should.equal(443); | ||
}); | ||
it("omits the port when it's 80", function(){ | ||
it("omits the port when it's 80", function () { | ||
var url = "http://subdomain.asdf.com:9090"; | ||
@@ -76,3 +77,3 @@ urlgrey(url).port(80) | ||
}); | ||
it("sets the port", function(){ | ||
it("sets the port", function () { | ||
var url = "https://subdomain.asdf.com"; | ||
@@ -83,8 +84,8 @@ urlgrey(url).port(9090) | ||
}); | ||
describe("#rawPath", function(){ | ||
it("gets the path", function(){ | ||
describe("#rawPath", function () { | ||
it("gets the path", function () { | ||
var url = "https://user:pass@subdomain.asdf.com/path?asdf=1234#frag"; | ||
urlgrey(url).rawPath().should.equal('/path'); | ||
}); | ||
it("sets the path", function(){ | ||
it("sets the path", function () { | ||
var url = "https://subdomain.asdf.com"; | ||
@@ -94,3 +95,3 @@ urlgrey(url).rawPath("blah") | ||
}); | ||
it("does not encode pieces of the path", function(){ | ||
it("does not encode pieces of the path", function () { | ||
var url = "https://subdomain.asdf.com"; | ||
@@ -100,14 +101,14 @@ urlgrey(url).rawPath("not encode here", "and/not/here") | ||
}); | ||
it ("sets the path from strings and arrays of strings", function(){ | ||
it("sets the path from strings and arrays of strings", function () { | ||
var url = "https://asdf.com"; | ||
urlgrey(url).rawPath(['qwer', '/asdf'], 'qwer/1234/', '/1234/') | ||
.toString().should.equal('https://asdf.com/qwer/asdf/qwer/1234/1234'); | ||
.toString().should.equal('https://asdf.com/qwer/asdf/qwer/1234/1234'); | ||
}); | ||
}); | ||
describe("#path", function(){ | ||
it("gets the path", function(){ | ||
describe("#path", function () { | ||
it("gets the path", function () { | ||
var url = "https://user:pass@subdomain.asdf.com/path?asdf=1234#frag"; | ||
urlgrey(url).path().should.equal('/path'); | ||
}); | ||
it("sets the path", function(){ | ||
it("sets the path", function () { | ||
var url = "https://subdomain.asdf.com"; | ||
@@ -117,3 +118,3 @@ urlgrey(url).path("blah") | ||
}); | ||
it("url encodes pieces of the path, but not slashes", function(){ | ||
it("url encodes pieces of the path, but not slashes", function () { | ||
var url = "https://subdomain.asdf.com"; | ||
@@ -123,14 +124,14 @@ urlgrey(url).path("encode here", "but/not/here") | ||
}); | ||
it ("sets the path from strings and arrays of strings", function(){ | ||
it("sets the path from strings and arrays of strings", function () { | ||
var url = "https://asdf.com"; | ||
urlgrey(url).path(['qwer', '/asdf'], 'qwer/1234/', '/1234/') | ||
.toString().should.equal('https://asdf.com/qwer/asdf/qwer/1234/1234'); | ||
.toString().should.equal('https://asdf.com/qwer/asdf/qwer/1234/1234'); | ||
}); | ||
}); | ||
describe("#hash", function(){ | ||
it("gets the hash", function(){ | ||
describe("#hash", function () { | ||
it("gets the hash", function () { | ||
var url = "https://user:pass@subdomain.asdf.com/path?asdf=1234#frag"; | ||
urlgrey(url).hash().should.equal('frag'); | ||
}); | ||
it("sets the hash", function(){ | ||
it("sets the hash", function () { | ||
var url = "https://subdomain.asdf.com"; | ||
@@ -141,8 +142,8 @@ urlgrey(url).hash("blah") | ||
}); | ||
describe("#password", function(){ | ||
it("gets the password", function(){ | ||
describe("#password", function () { | ||
it("gets the password", function () { | ||
var url = "https://user:pass@subdomain.asdf.com/path?asdf=1234#frag"; | ||
urlgrey(url).password().should.equal('pass'); | ||
}); | ||
it("sets the password", function(){ | ||
it("sets the password", function () { | ||
var url = "https://user:pass@subdomain.asdf.com"; | ||
@@ -153,8 +154,8 @@ urlgrey(url).password("blah") | ||
}); | ||
describe("#username", function(){ | ||
it("gets the username", function(){ | ||
describe("#username", function () { | ||
it("gets the username", function () { | ||
var url = "https://user:pass@subdomain.asdf.com/path?asdf=1234#frag"; | ||
urlgrey(url).username().should.equal('user'); | ||
}); | ||
it("sets the username", function(){ | ||
it("sets the username", function () { | ||
var url = "https://user:pass@subdomain.asdf.com"; | ||
@@ -165,4 +166,4 @@ urlgrey(url).username("blah") | ||
}); | ||
describe("#parent", function(){ | ||
it("returns the second-last item in the path if there is no input", function(){ | ||
describe("#parent", function () { | ||
it("returns the second-last item in the path if there is no input", function () { | ||
var url = "http://asdf.com/path/kid?asdf=1234#frag"; | ||
@@ -172,3 +173,3 @@ urlgrey(url).parent() | ||
}); | ||
it("ignores a trailing slash", function(){ | ||
it("ignores a trailing slash", function () { | ||
var url = "http://asdf.com/path/kid/?asdf=1234#frag"; | ||
@@ -178,3 +179,3 @@ urlgrey(url).parent() | ||
}); | ||
it("throws an exception when no parent path exists", function(){ | ||
it("throws an exception when no parent path exists", function () { | ||
var url = "http://asdf.com/"; | ||
@@ -184,3 +185,3 @@ try { | ||
should.fail("expected exception was not raised."); | ||
} catch (ex){ | ||
} catch (ex) { | ||
ex.message.should.equal('The current path has no parent path'); | ||
@@ -190,8 +191,8 @@ } | ||
}); | ||
describe("#extendedPath", function(){ | ||
it("returns the part of the url after the host:port", function(){ | ||
describe("#extendedPath", function () { | ||
it("returns the part of the url after the host:port", function () { | ||
var url = "http://asdf.com:8080/path?asdf=1234#frag"; | ||
urlgrey(url).extendedPath().should.equal('/path?asdf=1234#frag'); | ||
}); | ||
it("lets you set the part of the url after the host:port", function(){ | ||
it("lets you set the part of the url after the host:port", function () { | ||
var url = "http://asdf.com:8080/path?asdf=1234#frag"; | ||
@@ -202,4 +203,4 @@ urlgrey(url).extendedPath('/asdf?qwer=1234#fraggle').toString() | ||
}); | ||
describe("#rawChild", function(){ | ||
it("returns a url with the given path suffix added", function(){ | ||
describe("#rawChild", function () { | ||
it("returns a url with the given path suffix added", function () { | ||
var url = "http://asdf.com/path?asdf=1234#frag"; | ||
@@ -209,3 +210,3 @@ urlgrey(url).rawChild('{kid here}') | ||
}); | ||
it("returns a url with the given path suffixes added, without escaping", function(){ | ||
it("returns a url with the given path suffixes added, without escaping", function () { | ||
var url = "http://asdf.com/path?asdf=1234#frag"; | ||
@@ -215,7 +216,7 @@ urlgrey(url).rawChild('{kid here}', '{and here}') | ||
}); | ||
it("returns the last item in the path if there is no input", function(){ | ||
it("returns the last item in the path if there is no input", function () { | ||
var url = "http://asdf.com/path/kid?asdf=1234#frag"; | ||
urlgrey(url).rawChild().should.equal('kid'); | ||
}); | ||
it("ignores a trailing slash", function(){ | ||
it("ignores a trailing slash", function () { | ||
var url = "http://asdf.com/path/kid/?asdf=1234#frag"; | ||
@@ -225,4 +226,4 @@ urlgrey(url).rawChild().should.equal('kid'); | ||
}); | ||
describe("#child", function(){ | ||
it("returns a url with the given path suffix added", function(){ | ||
describe("#child", function () { | ||
it("returns a url with the given path suffix added", function () { | ||
var url = "http://asdf.com/path?asdf=1234#frag"; | ||
@@ -232,3 +233,3 @@ urlgrey(url).child('kid here') | ||
}); | ||
it("returns a url with the given path suffixes added", function(){ | ||
it("returns a url with the given path suffixes added", function () { | ||
var url = "http://asdf.com/path?asdf=1234#frag"; | ||
@@ -238,3 +239,3 @@ urlgrey(url).child('kid here', 'and here') | ||
}); | ||
it("returns a url with the given path suffix added even if it's 0", function(){ | ||
it("returns a url with the given path suffix added even if it's 0", function () { | ||
var url = "http://asdf.com/path?asdf=1234#frag"; | ||
@@ -244,15 +245,15 @@ urlgrey(url).child(0) | ||
}); | ||
it("returns the last item in the path if there is no input", function(){ | ||
it("returns the last item in the path if there is no input", function () { | ||
var url = "http://asdf.com/path/kid?asdf=1234#frag"; | ||
urlgrey(url).child().should.equal('kid'); | ||
}); | ||
it("ignores a trailing slash", function(){ | ||
it("ignores a trailing slash", function () { | ||
var url = "http://asdf.com/path/kid/?asdf=1234#frag"; | ||
urlgrey(url).child().should.equal('kid'); | ||
}); | ||
it("url-decodes the child if it's encoded", function(){ | ||
it("url-decodes the child if it's encoded", function () { | ||
var url = "http://asdf.com/path/the%20kid"; | ||
urlgrey(url).child().should.equal('the kid'); | ||
}); | ||
it("url-encodes the child if necessary", function(){ | ||
it("url-encodes the child if necessary", function () { | ||
var url = "http://asdf.com/path/"; | ||
@@ -262,20 +263,20 @@ urlgrey(url).child('the kid').toString().should.equal('http://asdf.com/path/the%20kid'); | ||
}); | ||
describe("#parsed", function(){ | ||
it("returns some stuff", function(){ | ||
describe("#parsed", function () { | ||
it("returns some stuff", function () { | ||
var url = "http://gdizzle:pazz@asdf.com:5678/path/kid/?asdf=1234#frag"; | ||
var actual = urlgrey(url).parsed(); | ||
var expected = { | ||
"protocol": "http", | ||
"auth": "gdizzle:pazz", | ||
"host": "asdf.com:5678", | ||
"port": 5678, | ||
"hostname": "asdf.com", | ||
"hash": "frag", | ||
"search": "?asdf=1234", | ||
"query": "asdf=1234", | ||
"pathname": "/path/kid/", | ||
"path": "/path/kid/?asdf=1234", | ||
"href": "http://gdizzle:pazz@asdf.com:5678/path/kid/?asdf=1234#frag", | ||
"username": "gdizzle", | ||
"password": "pazz" | ||
"protocol": "http", | ||
"auth": "gdizzle:pazz", | ||
"host": "asdf.com:5678", | ||
"port": 5678, | ||
"hostname": "asdf.com", | ||
"hash": "frag", | ||
"search": "?asdf=1234", | ||
"query": "asdf=1234", | ||
"pathname": "/path/kid/", | ||
"path": "/path/kid/?asdf=1234", | ||
"href": "http://gdizzle:pazz@asdf.com:5678/path/kid/?asdf=1234#frag", | ||
"username": "gdizzle", | ||
"password": "pazz" | ||
}; | ||
@@ -285,8 +286,9 @@ chai.expect(actual).to.eql(expected); | ||
}); | ||
describe("#toString", function(){ | ||
it("returns the input string if unmodified", function(){ | ||
describe("#toString", function () { | ||
it("returns the input string if unmodified", function () { | ||
var url = "https://user:pass@subdomain.asdf.com/path?asdf=1234#frag"; | ||
urlgrey(url).toString().should.equal(url); | ||
}); | ||
it("returns an absolute uri even if one is not given", function(){ | ||
it("returns an absolute uri even if one is not given", function () { | ||
var url = "/path?asdf=1234#frag"; | ||
@@ -297,4 +299,4 @@ urlgrey(url).toString() | ||
}); | ||
describe("#protocol", function(){ | ||
it("gets the protocol", function(){ | ||
describe("#protocol", function () { | ||
it("gets the protocol", function () { | ||
var url = "https://user:pass@subdomain.asdf.com/path?asdf=1234#frag"; | ||
@@ -304,9 +306,9 @@ urlgrey(url).protocol().should.equal('https'); | ||
if (isBrowser){ | ||
it ("gets the protocol as the current one if unset", function(){ | ||
urlgrey('').protocol() | ||
.should.equal(window.location.href.slice(0, 4)); | ||
}); | ||
if (isBrowser) { | ||
it("gets the protocol as the current one if unset", function () { | ||
urlgrey('').protocol() | ||
.should.equal(window.location.href.slice(0, 4)); | ||
}); | ||
} else { | ||
it("gets the protocol as http if unset", function(){ | ||
it("gets the protocol as http if unset", function () { | ||
var url = "/path?asdf=1234#frag"; | ||
@@ -317,3 +319,3 @@ urlgrey(url).protocol().should.equal('http'); | ||
it("sets the protocol", function(){ | ||
it("sets the protocol", function () { | ||
var url = "https://user:pass@subdomain.asdf.com/path?asdf=1234#frag"; | ||
@@ -325,21 +327,22 @@ var expected = "http://user:pass@subdomain.asdf.com/path?asdf=1234#frag"; | ||
describe("#queryString", function(){ | ||
it("sets the queryString", function(){ | ||
describe("#queryString", function () { | ||
it("sets the queryString", function () { | ||
var updated = urlgrey("http://s.asdf.com").queryString('foo=1234'); | ||
updated.toString().should.equal("http://s.asdf.com?foo=1234"); | ||
updated.query().should.deep.equal({foo: "1234"}); | ||
var query = updated.query(); | ||
assert.deepEqual(query, { foo: "1234" }); | ||
}); | ||
it("changes the queryString", function(){ | ||
it("changes the queryString", function () { | ||
var updated = urlgrey("http://s.asdf.com?foo=1234&bar=5678").queryString('baz=3456'); | ||
updated.toString().should.equal("http://s.asdf.com?baz=3456"); | ||
updated.query().should.deep.equal({baz: "3456"}); | ||
assert.deepEqual(updated.query(), { baz: "3456" }); | ||
}); | ||
it("gets the queryString", function(){ | ||
it("gets the queryString", function () { | ||
chai.expect( | ||
urlgrey("http://s.asdf.com/?qwer=1234").queryString() | ||
urlgrey("http://s.asdf.com/?qwer=1234").queryString() | ||
).to.equal("qwer=1234"); | ||
}); | ||
it("'roundtrips' the queryString", function(){ | ||
it("'roundtrips' the queryString", function () { | ||
urlgrey("http://s.asdf.com/?qwer=1234").queryString('asdf=1234') | ||
@@ -350,83 +353,85 @@ .queryString().should.equal("asdf=1234"); | ||
}); | ||
describe("#rawQuery", function(){ | ||
it("adds a querystring", function(){ | ||
var updated = urlgrey("http://asdf.com").rawQuery({foo:'12 34'}); | ||
describe("#rawQuery", function () { | ||
it("adds a querystring", function () { | ||
var updated = urlgrey("http://asdf.com").rawQuery({ foo: '12 34' }); | ||
updated.toString().should.equal("http://asdf.com?foo=12 34"); | ||
updated.rawQuery().should.deep.equal({foo: "12 34"}); | ||
assert.deepEqual(updated.rawQuery(), { foo: "12 34" }); | ||
}); | ||
it("appends a querystring", function(){ | ||
var updated = urlgrey("http://asdf.com?foo=1234").rawQuery({bar:'56 78'}); | ||
it("appends a querystring", function () { | ||
var updated = urlgrey("http://asdf.com?foo=1234").rawQuery({ bar: '56 78' }); | ||
updated.toString().should.equal("http://asdf.com?foo=1234&bar=56 78"); | ||
updated.rawQuery().should.deep.equal({foo: "1234", bar: "56 78"}); | ||
assert.deepEqual(updated.rawQuery(), { foo: "1234", bar: "56 78" }); | ||
}); | ||
it("modifies a querystring", function(){ | ||
var updated = urlgrey("http://asdf.com?foo=1234&bar=abcd").rawQuery({foo: "56 78"}); | ||
it("modifies a querystring", function () { | ||
var updated = urlgrey("http://asdf.com?foo=1234&bar=abcd").rawQuery({ foo: "56 78" }); | ||
updated.toString().should.equal("http://asdf.com?foo=56 78&bar=abcd"); | ||
updated.rawQuery().should.deep.equal({foo: "56 78", bar: "abcd"}); | ||
assert.deepEqual(updated.rawQuery(), { foo: "56 78", bar: "abcd" }); | ||
}); | ||
it("clears a querystring", function(){ | ||
it("clears a querystring", function () { | ||
var updated = urlgrey("http://asdf.com?foo=1234").rawQuery(false); | ||
updated.toString().should.equal("http://asdf.com"); | ||
updated.rawQuery().should.deep.equal({}); | ||
}); | ||
it("clears an element of a querystring with null or false", function(){ | ||
it("clears an element of a querystring with null or false", function () { | ||
var updated = urlgrey("http://asdf.com") | ||
.rawQuery({foo: 1, bar: 2, baz: 3}) | ||
.rawQuery({foo: 0, bar: null}); | ||
.rawQuery({ foo: 1, bar: 2, baz: 3 }) | ||
.rawQuery({ foo: 0, bar: null }); | ||
updated.toString().should.equal("http://asdf.com?foo=0&baz=3"); | ||
updated.rawQuery().should.deep.equal({foo: "0", baz: "3"}); | ||
assert.deepEqual(updated.rawQuery(), { foo: "0", baz: "3" }); | ||
}); | ||
it("extracts a querystring as an object", function(){ | ||
urlgrey("http://asdf.com?asdf=56%2078").rawQuery().should.deep.equal({asdf:'56%2078'}); | ||
it("extracts a querystring as an object", function () { | ||
var queryObject = urlgrey("http://asdf.com?asdf=56%2078").rawQuery(); | ||
assert.deepEqual(queryObject, { asdf: '56%2078' }); | ||
}); | ||
}); | ||
describe("#query", function(){ | ||
it("adds a querystring", function(){ | ||
var updated = urlgrey("http://asdf.com").query({foo:'12 34'}); | ||
describe("#query", function () { | ||
it("adds a querystring", function () { | ||
var updated = urlgrey("http://asdf.com").query({ foo: '12 34' }); | ||
updated.toString().should.equal("http://asdf.com?foo=12%2034"); | ||
updated.query().should.deep.equal({foo: "12 34"}); | ||
assert.deepEqual(updated.query(), { foo: "12 34" }); | ||
}); | ||
it("appends a querystring", function(){ | ||
var updated = urlgrey("http://asdf.com?foo=1234").query({bar:'56 78'}); | ||
it("appends a querystring", function () { | ||
var updated = urlgrey("http://asdf.com?foo=1234").query({ bar: '56 78' }); | ||
updated.toString().should.equal("http://asdf.com?foo=1234&bar=56%2078"); | ||
updated.query().should.deep.equal({foo: "1234", bar: "56 78"}); | ||
assert.deepEqual(updated.query(), { foo: "1234", bar: "56 78" }); | ||
}); | ||
it("modifies a querystring", function(){ | ||
var updated = urlgrey("http://asdf.com?foo=1234&bar=abcd").query({foo: "56 78"}); | ||
it("modifies a querystring", function () { | ||
var updated = urlgrey("http://asdf.com?foo=1234&bar=abcd").query({ foo: "56 78" }); | ||
updated.toString().should.equal("http://asdf.com?foo=56%2078&bar=abcd"); | ||
updated.query().should.deep.equal({foo: "56 78", bar: "abcd"}); | ||
assert.deepEqual(updated.query(), { foo: "56 78", bar: "abcd" }); | ||
}); | ||
it("clears a querystring", function(){ | ||
it("clears a querystring", function () { | ||
var updated = urlgrey("http://asdf.com?foo=1234").query(false); | ||
updated.toString().should.equal("http://asdf.com"); | ||
updated.query().should.deep.equal({}); | ||
assert.deepEqual(updated.query(), {}); | ||
}); | ||
it("clears an element of a querystring with null or false", function(){ | ||
it("clears an element of a querystring with null or false", function () { | ||
var updated = urlgrey("http://asdf.com") | ||
.rawQuery({foo: 1, bar: 2, baz: 3}) | ||
.rawQuery({foo: 0, bar: null}); | ||
.rawQuery({ foo: 1, bar: 2, baz: 3 }) | ||
.rawQuery({ foo: 0, bar: null }); | ||
updated.toString().should.equal("http://asdf.com?foo=0&baz=3"); | ||
updated.query().should.deep.equal({foo: "0", baz: "3"}); | ||
assert.deepEqual(updated.query(), { foo: "0", baz: "3" }); | ||
}); | ||
it("extracts a querystring as an object", function(){ | ||
urlgrey("http://asdf.com?asdf=56%2078").query().should.deep.equal({asdf:'56 78'}); | ||
it("extracts a querystring as an object", function () { | ||
assert.deepEqual(urlgrey("http://asdf.com?asdf=56%2078").query(), { asdf: '56 78' }); | ||
}); | ||
}); | ||
describe('#encode', function(){ | ||
it ("returns a url-encoded version of its input string", function(){ | ||
describe('#encode', function () { | ||
it("returns a url-encoded version of its input string", function () { | ||
urlgrey('').encode("this is a test").should.equal("this%20is%20a%20test"); | ||
}); | ||
}); | ||
describe('#decode', function(){ | ||
it ("returns a url-decoded version of its input string", function(){ | ||
describe('#decode', function () { | ||
it("returns a url-decoded version of its input string", function () { | ||
urlgrey('').decode("this%20is%20a%20test").should.equal("this is a test"); | ||
@@ -433,0 +438,0 @@ }); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
317676
0.34%8
-20%10719
0.27%1
-50%1
-50%1
Infinity%+ Added
+ Added
+ Added