Created
July 20, 2017 10:16
-
-
Save OzieWest/904a3f577b4842c40b49b4be7e63adb9 to your computer and use it in GitHub Desktop.
jsfy
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
| function jsfy(objFromJson: any) { | |
| if (typeof objFromJson === 'undefined') { | |
| return undefined; | |
| } | |
| if (objFromJson === null) { | |
| return null; | |
| } | |
| if (typeof objFromJson === 'string') { | |
| return `'${objFromJson}'`; | |
| } | |
| if (typeof objFromJson === 'number' || typeof objFromJson === 'boolean') { | |
| return objFromJson; | |
| } | |
| if (Array.isArray(objFromJson)) { | |
| return `[${objFromJson.map(jsfy).join(', ')}]`; | |
| } | |
| if (objFromJson instanceof Date) { | |
| return `'${objFromJson.toISOString()}'`; | |
| } | |
| if (typeof objFromJson === 'object') { | |
| const keys = Object.keys(objFromJson); | |
| let props = ''; | |
| for (let i = 0; i < keys.length; i += 1) { | |
| props = props + (keys[i] + ': ' + jsfy(objFromJson[keys[i]])); | |
| } | |
| return '{ ' + props + ' }'; | |
| } | |
| return objFromJson.toString(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment