Created
July 7, 2017 13:25
-
-
Save codeofnode/7e36c05cdb1c3377d32fab59503793e5 to your computer and use it in GitHub Desktop.
stringify anything in javascript
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
/** | |
* stringify if not already string | |
* @param {*} input - the input to the function. | |
* @return {string} output - the stringified data. | |
*/ | |
const stringify = function stringify(input) { | |
if (typeof input === 'string') return input; | |
if (typeof input === 'object') { | |
try { | |
return JSON.stringify(input); | |
} catch (err) { | |
return String(input); | |
} | |
} | |
return String(input); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment