Created
January 21, 2019 02:22
-
-
Save oscarcs/32ddaaa10a24cf38ba1290bfca61a30e to your computer and use it in GitHub Desktop.
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 customJSONStringify(value) { | |
function writeJSON(value) { | |
switch (toType(value)) { | |
case "object": | |
return writeObject(value); | |
case "array": | |
return writeArray(value); | |
//... | |
} | |
} | |
function writeObject(value) { | |
result = '{'; | |
for (var k in value.keys()) { | |
result += '"' + k + '": ' + writeJson(value[k]) + ', ' | |
} | |
result += "}"; | |
return result; | |
} | |
function writeArray(value) { | |
result = '['; | |
result += ']'; | |
return result; | |
} | |
function writeString(value) { | |
} | |
function writeNumber(value) { | |
} | |
function writeBoolean(value) { | |
} | |
function writeNull(value) { | |
} | |
// https://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/ | |
function toType(obj) { | |
return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase(); | |
} | |
return writeJSON(value); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment