Last active
June 14, 2016 14:09
-
-
Save rom1504/58bc84eea871ea171a99aa09ac42d111 to your computer and use it in GitHub Desktop.
compact stringify
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
const p=require('./protocol.json'); | |
const json_string_of_jarray=(a,lb,t) => '['+(lb ? "\n" : "")+(a.map((v,i) => { | |
const withoutBreak=(lb ? t+" " : "")+json_string_of_jvalue(v,false); | |
return withoutBreak.length+(i!=(a.length-1) ? 1 : 0)>80 && lb ? (lb ? t+" " : "")+json_string_of_jvalue(v,lb,t+" ") : withoutBreak; | |
})).join(lb ? ",\n" : ", ")+(lb ? "\n"+t : "")+']'; | |
const json_string_of_jobject=(o,lb,t) => '{'+(lb ? "\n" : "")+Object.keys(o).map((k,i) => { | |
const withoutBreak=(lb ? t+' ' : '')+'"'+k+'": '+json_string_of_jvalue(o[k],false); | |
return withoutBreak.length+(i!=(o.length-1) ? 1 : 0)>80 && lb ? (lb ? t+' ' : '')+'"'+k+'": '+json_string_of_jvalue(o[k],true,t+" ") : withoutBreak; | |
}).join(lb ? ",\n" : ", ")+(lb ? "\n"+t : "")+'}'; | |
const json_string_of_jvalue=(v,lb=true,t="") => { | |
if(v===null) | |
return "NULL"; | |
if(v.constructor===Array) | |
return json_string_of_jarray(v,lb,t); | |
if(typeof v === 'object') | |
return json_string_of_jobject(v,lb,t); | |
if(typeof v === 'string') | |
return '"'+v+'"'; | |
if(typeof v === 'boolean') | |
return v ? "true" : "false"; | |
if(typeof v === 'number') | |
return v; | |
throw Error("unexpected type !"); | |
} | |
console.log(json_string_of_jvalue(p)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment