Created
September 6, 2011 03:58
-
-
Save ishiduca/1196542 to your computer and use it in GitHub Desktop.
URI.Query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function foreach (arry, func) { | |
var i = 0, len = arry.length; | |
if (len) { | |
for (; i < len; i += 1) { | |
func(arry[i], i); | |
} | |
} | |
} | |
Array.prototype.foreach = function (func) { | |
foreach.apply(null, [ this, func ]); | |
}; | |
function test (resFunc, forecast) { | |
resFunc = (typeof resFunc === 'string') ? resFunc : JSON.stringify(resFunc); | |
forecast = (typeof forecast === 'string') ? forecast : JSON.stringify(forecast); | |
console.log( | |
(resFunc == forecast) | |
? [ 'success: ', resFunc ].join('') | |
: [ '!failed: ', resFunc, forecast].join(' --- ') | |
); | |
} | |
function Query () {}; | |
(function (Q) { | |
Q.parse = function (str, type) { | |
type = type || 'HASH'; | |
type = (type.match(/^array$/i)) ? 'ARRAY' : 'HASH'; | |
if (! str) return (type === 'ARRAY') ? [] : {}; | |
if (typeof str !== 'string') return { error: 'failed: 1st argument must be "string"' }; | |
str = (str.match(/^\?/)) ? str.slice(1) : str; | |
var obj, buf; | |
if (type === 'ARRAY') { | |
obj = []; | |
(str.split(/&/)).foreach(function (keyVal) { | |
buf = keyVal.split(/=/); | |
obj.push([ buf[0], decodeURIComponent(buf[1]) ]); | |
}); | |
return obj; | |
} | |
if (type === 'HASH') { | |
obj = {}; | |
(str.split(/&/)).foreach(function (keyVal) { | |
buf = keyVal.split(/=/); | |
obj[ buf[0] ] = decodeURIComponent(buf[1]); | |
}); | |
return obj; | |
} | |
}; | |
Q.stringify = function (obj) { | |
if (! obj) return null; // ''; | |
if (typeof obj === 'string') return obj; | |
var buf = []; | |
if (obj instanceof Array) { | |
obj.foreach(function (keyVal) { | |
buf.push([ keyVal[0], encodeURIComponent(keyVal[1]) ].join('=')); | |
}); | |
return (buf.length > 0) ? buf.join('&') : null; | |
} | |
if (typeof obj === 'object') { | |
for (var key in obj) { | |
buf.push([ key, encodeURIComponent(obj[key]) ].join('=')); | |
} | |
return (buf.length > 0) ? buf.join('&') : null; | |
} | |
return { error: 'failed: arguments must be "hash" or "array"' }; | |
}; | |
}) (Query.prototype); | |
var QUERY = new Query; | |
test(QUERY.parse(), {}); | |
test(QUERY.parse(null), {}); | |
test(QUERY.parse(""), {}); | |
test(QUERY.parse(undefined), {}); | |
test(QUERY.parse({}), { error: 'failed: 1st argument must be "string"' }); | |
var _array = 'array'; | |
test(QUERY.parse(null, _array), []); | |
test(QUERY.parse("", _array), []); | |
test(QUERY.parse(undefined, _array), []); | |
var japStr = 'ミッちょん みちこ'; | |
test(QUERY.parse("mode=tora&q=" + encodeURIComponent(japStr)), { mode : "tora", q : japStr }); | |
test(QUERY.parse("?mode=tora&q=" + encodeURIComponent(japStr), _array), [ [ "mode" , "tora" ], ["q" , japStr ] ]); | |
console.log("---------------"); | |
test(QUERY.stringify(), null); | |
test(QUERY.stringify(""), null); | |
test(QUERY.stringify({}), null); | |
test(QUERY.stringify([]), null); | |
test(QUERY.stringify({ mode : 'gaw', val : 'karipaku'}), "mode=gaw&val=karipaku"); | |
test(QUERY.stringify([ [ "mode", "vow" ], [ "val", "wow" ] ]), "mode=vow&val=wow"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment