Last active
March 16, 2022 04:30
-
-
Save esctabcapslock/948350439d7ad1012ba5b5fa7ac19169 to your computer and use it in GitHub Desktop.
JSON.stringify 모르고 만듦. 하는 일 같음.
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 struct2string(st,deep, p){ | |
if (st==null) return'null'; | |
else if (st==undefined) return 'undefined'; | |
else if (typeof(st)=='string') return str2str(st); | |
else if (typeof(st)!='object') return st.toString(); | |
if(!deep ) return str2str(st.toString()); | |
function str2str(str){ | |
return '"'+str.replace(/\n/g,'\\n').replace(/\`/g,'\\`').replace(/\"/g,'\\"').replace(/\r/g,'\\r').replace(/\{/g,'\\{').replace(/\}/g,'\\}')+'"' | |
} | |
function array2str(ar){ | |
var out='['; | |
for(var i=0; i<ar.length; i++) out+=struct2string(ar[i], deep-1)+',\n'; | |
return out+']\n'; | |
} | |
function else2str(st){ | |
var out='{'; | |
for(var i in st) { try{var xx = struct2string(st[i], deep-1, st)}catch{console.log('11',st[i], typeof(xx), deep, i, st,p)} | |
out+=`'${i}': ${xx},\n`} | |
return out+'}\n'; | |
} | |
if (st.constructor.name=='Array') return array2str(st); | |
else return else2str(st) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment