Last active
February 21, 2022 20:06
-
-
Save IgorFachini/14250ce18a033e212006f3f93d4e003a to your computer and use it in GitHub Desktop.
JavaScript: Convert string boolean to boolean, suports objects, arrays recursivelly.
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
/** | |
* Convert string booleans to boolean | |
* @param {Array, Object, String} date | |
* @returns {Array, Object, String, Boolean} | |
*/ | |
const convertStringBoolean = (data) => { | |
if (Array.isArray(data)) return data.map(d => convertStringBoolean(d)) | |
if (typeof data === 'object') { | |
for (const key in data) { | |
data[key] = convertStringBoolean(data[key]) | |
} | |
} | |
if (typeof data === 'string' && (data === 'true' || data === 'false')) return data === 'true' | |
return data | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment