Skip to content

Instantly share code, notes, and snippets.

@asika32764
Last active August 29, 2015 14:04
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asika32764/6556efdf5c593ce140bb to your computer and use it in GitHub Desktop.
Save asika32764/6556efdf5c593ce140bb to your computer and use it in GitHub Desktop.
The simple JS URI object. Repository move to https://github.com/asika32764/SimpleURI.js
/*!
* The simple JS URI object.
*
* @license GNU General Public License version 2 or later; see LICENSE
* @link https://gist.github.com/asika32764/6556efdf5c593ce140bb
*/
/**
* The simple JS URI object.
*
* Usage
* ```
* var uri = new URI('http://yourhost.com');
*
* uri.path = 'some/dir';
* uri.port = 8080;
*
* uri.setQuery({flower: 'sakura', tree: 'santa'});
* uri.setVar('foo', 'bar');
*
* uri.toString();
* ```
*
* No Conflict
*
* ```
* var MyURI = URI.noConflict();
* ```
*/
;(function()
{
"use strict";
var Class = "SimpleURI",
bak = window.URI;
/**
* Class init.
*
* @param {string} uri URI string.
*
* @constructor
*/
Class = window.URI = window[Class] = function(uri)
{
this.parse(uri);
};
/**
* Parse uri string.
*
* @param {string} uri URI string.
*/
Class.prototype.parse = function(uri)
{
var parser = document.createElement('a');
parser.href = uri;
this.scheme = parser.protocol || '';
this.username = parser.username || '';
this.password = parser.password || '';
this.host = parser.hostname || '';
this.port = parser.port || '';
this.path = parser.pathname || '';
this.query = parseQueryString(parser.search) || '';
this.hash = parser.hash || '';
};
/**
* Set Query.
*
* @param {Object} queries Query object.
*/
Class.prototype.setQuery = function(queries)
{
this.query = queries;
};
/**
* Set query value
*
* @param {string} key Query key.
* @param {string} value Query value.
*/
Class.prototype.setVar = function(key, value)
{
this.query[key] = value;
};
/**
* Get query value
*
* @param {string} key Query key.
* @param {string} defaultVal The default value if key not exists.
*
* @returns {*}
*/
Class.prototype.getVar = function(key, defaultVal)
{
return this.query[key] || defaultVal;
};
/**
* Http build query.
*
* @param {object} data The data to build query, should be an object.
* @param {string} separator query separator, default is '&'.
*
* @note This function has many influences from: http://phpjs.org/functions/http_build_query/
*
* @returns {string}
*/
Class.prototype.httpBuildQuery = function(data, separator)
{
var value,
key,
queries = [],
query;
separator = separator || '&';
for (key in data)
{
value = data[key];
query = buildQuery(key, value, separator);
if (query !== '')
{
queries.push(query);
}
}
return queries.join(separator);
};
/**
* Make uri To string.
*
* @returns {string}
*/
Class.prototype.toString = function(parts)
{
var uri = '';
var query = this.httpBuildQuery(this.query);
parts = parts || ['scheme', 'user', 'pass', 'host', 'port', 'path', 'query', 'fragment'];
uri += parts.indexOf('scheme') != -1 ? (this.scheme ? this.scheme + '//' : '') : '';
uri += parts.indexOf('user') != -1 ? this.username : '';
uri += parts.indexOf('pass') != -1 ? (this.password ? ':' : '') + this.password + (this.username ? '@' : '') : '';
uri += parts.indexOf('host') != -1 ? this.host : '';
uri += parts.indexOf('port') != -1 ? (this.port ? ':' : '') + this.port : '';
uri += parts.indexOf('path') != -1 ? '/' + this.path.replace(/^\/|\/$/g, '') : '';
uri += parts.indexOf('query') != -1 ? (query ? '?' + query : '') : '';
uri += parts.indexOf('fragment') != -1 ? (this.hash ? '#' + this.hash : '') : '';
return uri;
};
/**
* Make this object on conflict to other library.
*
* @returns {string}
*/
Class.noConflict = function()
{
if (window.URI === this)
{
window.URI = bak;
}
return Class;
};
/**
* Parse http query.
*
* @param queryString {string}
*
* @note This function has many influences from mootools.js
*
* @private
* @returns {Object}
*/
var parseQueryString = function(queryString)
{
var vars,
object = {},
key,
val;
if (!queryString)
{
return {};
}
// remove the leading question mark from the query string if it is present
if (queryString.charAt(0) == '?')
{
queryString = queryString.substring(1);
}
vars = queryString.split(/[&;]/);
if (!vars.length)
{
return object;
}
for (var k in vars)
{
val = vars[k];
var index = val.indexOf('=') + 1;
var value = index ? val.substr(index) : '';
var keys = index ? val.substr(0, index - 1).match(/([^\]\[]+|(\B)(?=\]))/g) : [val];
var obj = object;
if (!keys)
{
return;
}
value = decodeURIComponent(value);
for (var i in keys)
{
key = decodeURIComponent(keys[i]);
var current = obj[key];
if (i < keys.length - 1)
{
obj = obj[key] = current || {};
}
else if (typeof current == 'object')
{
current.push(value);
}
else
{
obj[key] = current != null ? [current, value] : value;
}
}
}
return object;
};
/**
* Build query helper.
*
* @param {string} key
* @param {*} val
* @param {string} separator
*
* @private
* @returns {string}
*/
var buildQuery = function(key, val, separator)
{
var k, queries = [];
if (val === true)
{
val = '1';
}
else if (val === false)
{
val = '0';
}
if (val != null)
{
if (typeof val === 'string' || typeof val === 'number')
{
return key + '=' + val;
}
else if (typeof val === 'object')
{
for (k in val)
{
if (val[k] != null)
{
queries.push(buildQuery(key + '[' + k + ']', val[k], separator));
}
}
return queries.join(separator);
}
else
{
throw new Error('There was an error processing for buildQuery().');
}
} else
{
return '';
}
};
})();

URI.js

A simple JS URI object

Usage

var uri = new URI('http://yourhost.com');

uri.path = 'some/dir';
uri.port = 8080;

uri.setQuery({flower: 'sakura', tree: 'santa'});
uri.setVar('foo', 'bar');

uri.toString();

No Conflict

var MyURI = URI.noConflict();

// OR

var MyURI = SimpleURI.noConflict();

Quick Insert

Full file

//rawgit.com/asika32764/6556efdf5c593ce140bb/raw/c4a897f66ce6ee0b42f0314bb35ab709a765a926/URI.js

Compressed

//rawgit.com/asika32764/6556efdf5c593ce140bb/raw/c4a897f66ce6ee0b42f0314bb35ab709a765a926/URI.min.js
/*!
* The simple JS URI object.
*
* @license GNU General Public License version 2 or later; see LICENSE
*/
;(function(){var b="SimpleURI",a=window.URI;b=window.URI=window[b]=function(e){this.parse(e)};b.prototype.parse=function(e){var f=document.createElement("a");f.href=e;this.scheme=f.protocol||"";this.username=f.username||"";this.password=f.password||"";this.host=f.hostname||"";this.port=f.port||"";this.path=f.pathname||"";this.query=d(f.search)||"";this.hash=f.hash||""};b.prototype.setQuery=function(e){this.query=e};b.prototype.setVar=function(e,f){this.query[e]=f};b.prototype.getVar=function(e,f){return this.query[e]||f};b.prototype.httpBuildQuery=function(i,j){var h,f,e=[],g;j=j||"&";for(f in i){h=i[f];g=c(f,h,j);if(g!==""){e.push(g)}}return e.join(j)};b.prototype.toString=function(g){var e="";var f=this.httpBuildQuery(this.query);g=g||["scheme","user","pass","host","port","path","query","fragment"];e+=g.indexOf("scheme")!=-1?(this.scheme?this.scheme+"//":""):"";e+=g.indexOf("user")!=-1?this.username:"";e+=g.indexOf("pass")!=-1?(this.password?":":"")+this.password+(this.username?"@":""):"";e+=g.indexOf("host")!=-1?this.host:"";e+=g.indexOf("port")!=-1?(this.port?":":"")+this.port:"";e+=g.indexOf("path")!=-1?"/"+this.path.replace(/^\/|\/$/g,""):"";e+=g.indexOf("query")!=-1?(f?"?"+f:""):"";e+=g.indexOf("fragment")!=-1?(this.hash?"#"+this.hash:""):"";return e};b.noConflict=function(){if(window.URI===this){window.URI=a}return b};var d=function(e){var o,g={},q,f;if(!e){return{}}if(e.charAt(0)=="?"){e=e.substring(1)}o=e.split(/[&;]/);if(!o.length){return g}for(var h in o){f=o[h];var m=f.indexOf("=")+1;var p=m?f.substr(m):"";var r=m?f.substr(0,m-1).match(/([^\]\[]+|(\B)(?=\]))/g):[f];var l=g;if(!r){return}p=decodeURIComponent(p);for(var j in r){q=decodeURIComponent(r[j]);var n=l[q];if(j<r.length-1){l=l[q]=n||{}}else{if(typeof n=="object"){n.push(p)}else{l[q]=n!=null?[n,p]:p}}}}return g};var c=function(g,i,h){var e,f=[];if(i===true){i="1"}else{if(i===false){i="0"}}if(i!=null){if(typeof i==="string"||typeof i==="number"){return g+"="+i}else{if(typeof i==="object"){for(e in i){if(i[e]!=null){f.push(c(g+"["+e+"]",i[e],h))}}return f.join(h)}else{throw new Error("There was an error processing for buildQuery().")}}}else{return""}}})();
@Rplus
Copy link

Rplus commented Aug 4, 2014

可以用 https://rawgit.com/ 來生成 CDN 的網址
不然流量過大會被 ban XD

https://cdn.rawgit.com/asika32764/6556efdf5c593ce140bb/raw/URI.min.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment