Skip to content

Instantly share code, notes, and snippets.

@codeofnode
Created July 7, 2017 13:25
Show Gist options
  • Save codeofnode/7e36c05cdb1c3377d32fab59503793e5 to your computer and use it in GitHub Desktop.
Save codeofnode/7e36c05cdb1c3377d32fab59503793e5 to your computer and use it in GitHub Desktop.
stringify anything in javascript
/**
* 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