Skip to content

Instantly share code, notes, and snippets.

@ishiduca
Created September 5, 2011 14:36
Show Gist options
  • Save ishiduca/1195132 to your computer and use it in GitHub Desktop.
Save ishiduca/1195132 to your computer and use it in GitHub Desktop.
parseURI: XMLHttpClient 第2引数 に用いる配列([ url, body, headers ])に整形する関数
// "httpRequest" [ url, body, headers ]
// httpRequest = parseURI(method, uri);
function _parseQuery (uriObj) {
var buf = [];
if (! uriObj) return null;
if (typeof uriObj === 'string') return uriObj;
if (typeof uriObj === 'object') {
if (uriObj.length && uriObj.length > 0) {
uriObj.foreach(function (keyVal, i) {
buf.push([ keyVal[0], encodeURIComponent(keyVal[1]) ].join('='));
});
return buf.join('&');
}
if (uriObj) {
for (var key in uriObj) {
buf.push([ key, encodeURIComponent(uriObj[key]) ].join('='));
}
return buf.join('&');
}
}
return null;
}
function _parseURI1 (method) {
if (method === 'POST') {
if (this[0].match(/\?/)) {
var buf = this[0].split(/\?/);
this[0] = buf[0];
this[1] = buf[1];
}
}
}
function _parseURI2 (method) {
// this[1] == null 以外は、stringに直し、method === 'POST' -> body, method === 'GET' -> urlに追加
var buf = _parseQuery(this[1]);
if (method === 'POST') {
this[1] = buf;
} else {
this[1] = null;
this[0] = (buf) ? [ this[0], buf ].join((buf.match(/^\?/)) ? '' : '?') : this[0];
}
}
function parseURI (method, uri) {
if (! method) return { error : '"method" not found' };
if (! uri) return { error : '"uri object" not found' };
method = (method.match(/^post/i)) ? 'POST' : 'GET';
var url, body = null, headers = {}, uriObj = [ url, body, headers ];
if (typeof uri === 'string') {
uriObj[0] = uri;
_parseURI1.apply(uriObj, [ method ]);
return uriObj;
}
if (! uri.length) return { error : 'uri must be "string" or "array"' };
if (typeof uri[0] !== 'string') return { error : '"url" must be "string"' };
uriObj[0] = uri[0];
_parseURI1.apply(uriObj, [ method ]);
uriObj[1] = uri[1] || uriObj[1];
_parseURI2.apply(uriObj, [ method ]);
uriObj[2] = uri[2] || {};
return uriObj;
}
function test (resFunc, forecast) {
resFunc = JSON.stringify(resFunc);
forecast = JSON.stringify(forecast);
console.log(
(resFunc == forecast)
? [ 'success', resFunc ].join(" --- ")
: [ '!failed', resFunc, forecast ].join(" --- ")
);
}
if (! Array.prototype.foreach) {
Array.prototype.foreach = function (func) {
foreach.apply(null, [ this, func ]);
};
}
function foreach (arry, func) {
var i = 0, len = arry.length;
for (; i < len; i+=1) {
func(arry[i], i);
}
}
// TEST
console.log('method === "GET"');
var method = 'GET';
test(parseURI(), { error : '"method" not found' });
test(parseURI(method), { error : '"uri object" not found' });
test(parseURI(method, "/tora"), [ '/tora', null, {} ]);
test(parseURI(method, "/tora?q=abc"), [ '/tora?q=abc', null, {} ]);
test(parseURI(method, []), { error : 'uri must be "string" or "array"' });
test(parseURI(method, [ "/tora" ]), [ '/tora', null, {} ]);
test(parseURI(method, [ "/tora?q=abc" ]), [ '/tora?q=abc', null, {} ]);
console.log('\narguments.length === 2');
test(parseURI(method, [ "/tora", "q=abc&qq=xyz" ]), [ '/tora?q=abc&qq=xyz', null, {} ]);
test(parseURI(method, [ "/tora", { q : 'abc', qq : 'xyz' } ]), [ '/tora?q=abc&qq=xyz', null, {} ]);
test(parseURI(method, [ "/tora", [["q", "abc"],["qq", "xyz"]] ]), [ '/tora?q=abc&qq=xyz', null, {} ]);
test(parseURI(method, [ "/tora?q=abc&qq=xyz", null ]), [ '/tora?q=abc&qq=xyz', null, {} ]);
console.log('\narguments.length === 3');
var headers = { 'Connection' : 'keep-alive', 'X-header' : 'heaven' };
test(parseURI(method, [ "/tora", "q=abc&qq=xyz", headers ]), [ '/tora?q=abc&qq=xyz', null, headers ]);
test(parseURI(method, [ "/tora", { q : 'abc', qq : 'xyz' }, headers ]), [ '/tora?q=abc&qq=xyz', null, headers ]);
test(parseURI(method, [ "/tora", [["q", "abc"],["qq", "xyz"]], headers ]), [ '/tora?q=abc&qq=xyz', null, headers ]);
test(parseURI(method, [ "/tora?q=abc&qq=xyz", null, headers ]), [ '/tora?q=abc&qq=xyz', null, headers ]);
console.log('\nmethod === "POST"');
var method = 'POST';
test(parseURI(), { error : '"method" not found' });
test(parseURI(method), { error : '"uri object" not found' });
test(parseURI(method, "/tora"), [ '/tora', null, {} ]);
test(parseURI(method, "/tora?q=abc"), [ '/tora', 'q=abc', {} ]);
test(parseURI(method, []), { error : 'uri must be "string" or "array"' });
test(parseURI(method, [ "/tora" ]), [ '/tora', null, {} ]);
test(parseURI(method, [ "/tora?q=abc" ]), [ '/tora', 'q=abc', {} ]);
console.log('\narguments.length === 2');
test(parseURI(method, [ "/tora", "q=abc&qq=xyz" ]), [ '/tora', 'q=abc&qq=xyz', {} ]);
test(parseURI(method, [ "/tora", { q : 'abc', qq : 'xyz' } ]), [ '/tora', 'q=abc&qq=xyz', {} ]);
test(parseURI(method, [ "/tora", [["q", "abc"],["qq", "xyz"]] ]), [ '/tora', 'q=abc&qq=xyz', {} ]);
test(parseURI(method, [ "/tora?q=abc&qq=xyz", null ]), [ '/tora', 'q=abc&qq=xyz', {} ]);
console.log('\narguments.length === 3');
var headers = { 'Connection' : 'keep-alive', 'X-header' : 'heaven' };
test(parseURI(method, [ "/tora", "q=abc&qq=xyz", headers ]), [ '/tora', 'q=abc&qq=xyz', headers ]);
test(parseURI(method, [ "/tora", { q : 'abc', qq : 'xyz' }, headers ]), [ '/tora', 'q=abc&qq=xyz', headers ]);
test(parseURI(method, [ "/tora", [["q", "abc"],["qq", "xyz"]], headers ]), [ '/tora', 'q=abc&qq=xyz', headers ]);
test(parseURI(method, [ "/tora?q=abc&qq=xyz", null, headers ]), [ '/tora', 'q=abc&qq=xyz', headers ]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment