Created
December 22, 2009 20:06
-
-
Save subtleGradient/262001 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| Object.fromQueryString = function(queryString){ | |
| queryString = String(queryString); | |
| if (!queryString) return {}; | |
| queryString = queryString.split('?'); | |
| if (queryString.length > 1) queryString = queryString.slice(1); | |
| queryString = queryString.join('?'); | |
| var object = {}; | |
| var vars = queryString.split('&'); | |
| if (vars.length) for (var i=0, this_var; this_var = vars[i]; i++){ | |
| this_var = this_var.split('='); | |
| var key = decodeURIComponent(this_var[0]); | |
| var val = decodeURIComponent(this_var.slice(1).join('=').replace(/\+/g, ' ')); | |
| if (object[key] != undefined){ | |
| if (!object[key].push) object[key] = [object[key]]; | |
| object[key].push(val); | |
| }else{ | |
| object[key] = val; | |
| } | |
| } | |
| return object; | |
| }; |
This file contains hidden or 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
| Object.toQueryString = function(object){ | |
| var queryString = []; | |
| for (var key in object) { | |
| var value = object[key]; | |
| var result; | |
| switch (Object.type(value)){ | |
| case 'object': result = Object.toQueryString(value, key); break; | |
| case 'array': | |
| result = []; | |
| for (var i=0, val; val = value[i]; i++){ | |
| var o = {}; | |
| o[key]=val; | |
| result.push(Object.toQueryString(o)); | |
| } | |
| result = result.join('&'); | |
| break; | |
| default: result = key + '=' + encodeURIComponent(value||''); | |
| } | |
| if (result) queryString.push(result); | |
| } | |
| return queryString.join('&'); | |
| }; |
This file contains hidden or 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
| Object.toXMLSimple = function(object, tagName){ | |
| var xml = []; | |
| switch (Object.type(object)){ | |
| case 'object': | |
| if (tagName) { xml.push('<'); xml.push(tagName); xml.push('>'); } | |
| for (var property in object) { | |
| var o = object[property]; | |
| if (property.indexOf(':')+1){ | |
| o = Object.fromKeyValue(property, object[property]); | |
| object[property] = o; | |
| property=null; | |
| } | |
| xml.push(Object.toXMLSimple( o, property )); | |
| } | |
| if (tagName) { xml.push('</'); xml.push(tagName); xml.push('>'); } | |
| break; | |
| case 'array': | |
| for (var i=0; i < object.length; i++) { | |
| xml.push(Object.toXMLSimple( object[i], tagName )); | |
| } | |
| break; | |
| default: | |
| if (tagName) { xml.push('<'); xml.push(tagName); xml.push('>'); } | |
| object = String(object); | |
| if (/[<]/.test(object)){ | |
| xml.push('<![CDATA['); | |
| object+=']]>'; | |
| } | |
| xml.push(object); | |
| if (tagName) { xml.push('</'); xml.push(tagName); xml.push('>'); } | |
| } | |
| return xml.join(''); | |
| }; |
This file contains hidden or 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
| Object.type = function(obj){ | |
| if (obj === undefined) return 'undefined'; | |
| if (obj === null) return 'null'; | |
| if (obj === true) return 'true'; | |
| if (obj === false) return 'false'; | |
| var objAsString = Object.prototype.toString.call(obj); | |
| if (objAsString === '[object Array]') return 'array'; | |
| if (objAsString === '[object Date]') return 'date'; | |
| if (obj.nodeName){ | |
| switch (obj.nodeType){ | |
| case 1: return 'element'; | |
| case 3: return (/\S/).test(obj.nodeValue) ? 'textnode' : 'whitespace'; | |
| } | |
| } else if (typeof obj.length == 'number'){ | |
| if (obj.callee) return 'arguments'; | |
| else if (obj.item) return 'collection'; | |
| } | |
| return typeof obj; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment