weex-nuke
Version:
基于 Rax 、Weex 的高性能组件体系 ~~
165 lines (156 loc) • 5.2 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
exports.HttpUrl = HttpUrl;
var URL_REG = new RegExp('^([a-z0-9-]+:)?' + //protocol
'[/]{2}' + //slash x 2
'(?:([^@/:?]+)(?::([^@/:]+))?@)?' + //username:password@
'([^:/?#]+)' + //hostname
'(?:[:]([0-9]+))?' + //port
'([/][^?#;]*)?' + //pathname
'(?:[?]([^#]*))?' + //search
'([#][^?]*)?$', //hash
'i');
function HttpUrl(url) {
return this instanceof HttpUrl ? this.init.call(this, url) : new HttpUrl(url);
}
HttpUrl.prototype = {
constructor: HttpUrl,
init: function init() {
var url = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
var matches = url.match(URL_REG);
if (matches) {
this.isAvailable = true;
this.params = {};
this.protocol = matches[1] || '';
this.username = matches[2] || '';
this.password = matches[3] || '';
this.hostname = matches[4];
this.port = matches[5] || '';
this.pathname = matches[6] || '/';
this.search = matches[7] || '';
this.hash = matches[8] || '';
this.host = this.hostname + (this.port ? ':' + this.port : '');
this.origin = this.protocol + '//' + this.host;
this.setSearch(this.search);
} else {
this.isAvailable = false;
}
},
setParams: function setParams(v) {
if (v && (typeof v === 'undefined' ? 'undefined' : _typeof(v)) === 'object' && !(v instanceof Date) && !(v instanceof RegExp) && !(v instanceof Array) && !(v instanceof String) && !(v instanceof Number) && !(v instanceof Boolean)) {
this.params = {};
for (var p in v) {
this.params[p] = v[p];
}
}
},
getParams: function getParams() {
return this.params;
},
setSearch: function setSearch(v) {
if (typeof v === 'string' || v instanceof String) {
v = v.toString();
this.search = v;
if (v.indexOf('?') === 0) {
v.substr(1);
}
v = v.split('&');
for (var i = 0, l = v.length; i < l; i++) {
var _v$i$split = v[i].split('='),
_v$i$split2 = _slicedToArray(_v$i$split, 2),
key = _v$i$split2[0],
val = _v$i$split2[1];
if (val !== undefined) {
val = val.toString();
}
if (key) {
try {
this.params[decodeURIComponent(key)] = decodeURIComponent(val);
} catch (e) {
this.params[key] = val;
}
}
}
}
},
getSearch: function getSearch() {
var s = [];
for (var p in this.params) {
if (this.params[p] === undefined) {
continue;
}
if (this.params[p] !== '') {
try {
s.push(encodeURIComponent(p) + '=' + encodeURIComponent(this.params[p]));
} catch (e) {
s.push(p + '=' + this.params[p]);
}
} else {
try {
s.push(encodeURIComponent(p));
} catch (e) {
s.push(p);
}
}
}
if (s.length) {
return '?' + s.join('&');
} else {
return '';
}
},
setHash: function setHash(v) {
if (typeof v === 'string' || v instanceof String) {
v = v.toString();
if (v && v.indexOf('#') < 0) {
v = '#' + v;
}
this.hash = v || '';
}
},
getHash: function getHash() {
return this.hash;
},
setHost: function setHost(v) {
if (typeof v === 'string' || v instanceof String) {
v = v.toString();
var matches = v.match(/([^:/?#]+)(?:[:]([0-9]+))?/);
if (matches) {
this.hostname = matches[1];
this.port = matches[2] || '';
}
}
},
getHost: function getHost() {
return this.hostname + (this.port ? ':' + this.port : '');
},
getProtocol: function getProtocol() {
return this.protocol;
},
toString: function toString() {
var str = this.protocol + '//';
if (this.username) {
str += this.username;
if (this.password) {
str += ':' + this.password;
}
str += '@';
}
str += this.hostname;
if (this.port && this.port !== '80') {
str += ':' + this.port;
}
if (this.pathname) {
str += this.pathname;
}
str += this.getSearch();
if (this.hash) {
str += this.hash;
}
return str;
}
};