Last active
September 20, 2019 03:36
-
-
Save madeinfree/c634c947c16067707a7a617f7ed5df90 to your computer and use it in GitHub Desktop.
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
module.exports = { | |
generateMultipleInsertValues(data) { | |
return data.reduce((result, nextData, index) => { | |
result = result + Object.keys(nextData).reduce((values, key, keyIndex, allKeys) => { | |
if (keyIndex === 0) { | |
if (typeof nextData[key] === 'string') { | |
return values + `('${nextData[key]}', ` | |
} else { | |
return values + `(${nextData[key]}, ` | |
} | |
} | |
if (typeof nextData[key] === 'string') { | |
values = values + `'${nextData[key]}'` | |
} else { | |
values = values + `${nextData[key]}` | |
} | |
if (keyIndex !== allKeys.length - 1) { | |
values = values + `, ` | |
} | |
return values | |
}, '') | |
if (index === data.length - 1) { | |
return `${result})` | |
} else { | |
return `${result}), ` | |
} | |
}, '') | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment