Skip to content

Commit 7c05c07

Browse files
ahonn3rd-Eden
authored andcommitted
[fix] Check global object, return empty location when undefined (#142)
* [fix] Check if global variable exists * [test] Add tests: parse when global variable is undefined * [test] change global not defined tests title
1 parent ca17b42 commit 7c05c07

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ var ignore = { hash: 1, query: 1 };
5050
* @api public
5151
*/
5252
function lolcation(loc) {
53-
loc = loc || global.location || {};
53+
var location = global && global.location || {};
54+
loc = loc || location;
5455

5556
var finaldestination = {}
5657
, type = typeof loc

test/test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ describe('url-parse', function () {
2929
assume(url.hostname).to.be.a('string');
3030
});
3131

32+
it('works when the global variable is not defined', function () {
33+
var globalVar = global;
34+
global = undefined;
35+
var url = parse('http://google.com/?foo=bar', true);
36+
37+
assume(url).to.be.an('object');
38+
assume(url.pathname).to.be.a('string');
39+
assume(url.host).to.be.a('string');
40+
assume(url.hostname).to.be.a('string');
41+
42+
global = globalVar;
43+
});
44+
3245
describe('extractProtocol', function () {
3346
it('extracts the protocol data', function () {
3447
assume(parse.extractProtocol('')).eql({

0 commit comments

Comments
 (0)