Skip to content

Commit

Permalink
[minor] Merge lolcation into main module
Browse files Browse the repository at this point in the history
  • Loading branch information
lpinca committed May 3, 2017
1 parent e9373aa commit e06eb7f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 56 deletions.
52 changes: 50 additions & 2 deletions index.js
@@ -1,9 +1,9 @@
'use strict';

var required = require('requires-port')
, lolcation = require('./lolcation')
, qs = require('querystringify')
, protocolre = /^([a-z][a-z0-9.+-]*:)?(\/\/)?([\S\s]*)/i;
, protocolre = /^([a-z][a-z0-9.+-]*:)?(\/\/)?([\S\s]*)/i
, slashes = /^[A-Za-z][A-Za-z0-9+-.]*:\/\//;

/**
* These are the parse rules for the URL parser, it informs the parser
Expand All @@ -27,6 +27,54 @@ var rules = [
[NaN, 'hostname', undefined, 1, 1] // Set left over.
];

/**
* These properties should not be copied or inherited from. This is only needed
* for all non blob URL's as a blob URL does not include a hash, only the
* origin.
*
* @type {Object}
* @private
*/
var ignore = { hash: 1, query: 1 };

/**
* The location object differs when your code is loaded through a normal page,
* Worker or through a worker using a blob. And with the blobble begins the
* trouble as the location object will contain the URL of the blob, not the
* location of the page where our code is loaded in. The actual origin is
* encoded in the `pathname` so we can thankfully generate a good "default"
* location from it so we can generate proper relative URL's again.
*
* @param {Object|String} loc Optional default location object.
* @returns {Object} lolcation object.
* @api public
*/
function lolcation(loc) {
loc = loc || global.location || {};

var finaldestination = {}
, type = typeof loc
, key;

if ('blob:' === loc.protocol) {
finaldestination = new URL(unescape(loc.pathname), {});
} else if ('string' === type) {
finaldestination = new URL(loc, {});
for (key in ignore) delete finaldestination[key];
} else if ('object' === type) {
for (key in loc) {
if (key in ignore) continue;
finaldestination[key] = loc[key];
}

if (finaldestination.slashes === undefined) {
finaldestination.slashes = slashes.test(loc.href);
}
}

return finaldestination;
}

/**
* @typedef ProtocolExtract
* @type Object
Expand Down
53 changes: 0 additions & 53 deletions lolcation.js

This file was deleted.

2 changes: 1 addition & 1 deletion test.js
Expand Up @@ -13,7 +13,7 @@ describe('url-parse', function () {
});

it('exposes the location function', function () {
assume(parse.location).equals(require('./lolcation'));
assume(parse.location).is.a('function');
});

it('exposes the extractProtocol function', function () {
Expand Down

0 comments on commit e06eb7f

Please sign in to comment.