Skip to content

Instantly share code, notes, and snippets.

@tigerhawkvok
Last active December 27, 2015 23:39
Show Gist options
  • Save tigerhawkvok/7407940 to your computer and use it in GitHub Desktop.
Save tigerhawkvok/7407940 to your computer and use it in GitHub Desktop.
Stringify implementation -- played with after speaking with Andrew about it
function stringify(o) {
var c=new Array;
$.each(o,function(key,val){
if(typeof(val)!='object' || Array.isArray(val)) c.push(clean(key)+':'+clean(val));
else c.push(clean(key)+':'+stringify(val));
});
return "{"+c.join(",")+"}";
}
function clean(string) {
return escape(string).split('"').join('\"');
}
function escape(val) {
if(typeof(val)=="number" || typeof(val)=="boolean") return val.toString();
if(Array.isArray(val)) return "["+val.join(",")+"]";
return '"'+val+'"';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment