UNPKG

weex-nuke

Version:

基于 Rax 、Weex 的高性能组件体系 ~~

174 lines (159 loc) 3.84 kB
'use strict'; const URL_REG = new RegExp( '^([a-z0-9-]+:)?' + //protocol '[/]{2}' + //slash x 2 '(?:([^@/:?]+)(?::([^@/:]+))?@)?' + //username:password@ '([^:/?#]+)' + //hostname '(?:[:]([0-9]+))?' + //port '([/][^?#;]*)?' + //pathname '(?:[?]([^#]*))?' + //search '([#][^?]*)?$', //hash 'i' ); export function HttpUrl(url) { return this instanceof HttpUrl ? this.init.call(this, url) : new HttpUrl(url); } HttpUrl.prototype = { constructor: HttpUrl, init(url = '') { const 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(v) { if ( v && typeof v === 'object' && !(v instanceof Date) && !(v instanceof RegExp) && !(v instanceof Array) && !(v instanceof String) && !(v instanceof Number) && !(v instanceof Boolean) ) { this.params = {}; for (let p in v) { this.params[p] = v[p]; } } }, getParams() { return this.params; }, 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 (let i = 0, l = v.length; i < l; i++) { let [key, val] = v[i].split('='); if (val !== undefined) { val = val.toString(); } if (key) { try { this.params[decodeURIComponent(key)] = decodeURIComponent(val); } catch (e) { this.params[key] = val; } } } } }, getSearch() { let s = []; for (let 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(v) { if (typeof v === 'string' || v instanceof String) { v = v.toString(); if (v && v.indexOf('#') < 0) { v = '#' + v; } this.hash = v || ''; } }, getHash() { return this.hash; }, setHost(v) { if (typeof v === 'string' || v instanceof String) { v = v.toString(); let matches = v.match(/([^:/?#]+)(?:[:]([0-9]+))?/); if (matches) { this.hostname = matches[1]; this.port = matches[2] || ''; } } }, getHost() { return this.hostname + (this.port ? ':' + this.port : ''); }, getProtocol() { return this.protocol; }, toString() { let 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; } };