Last active
December 27, 2015 23:39
-
-
Save tigerhawkvok/7407940 to your computer and use it in GitHub Desktop.
Stringify implementation -- played with after speaking with Andrew about it
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 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