This function would trim leading and trailing whitespace from the string and replace any internal repeating whitespace characters with a single space " ".
1) For Data Elements in Launch, just check the Clean Text box on the configuration view.
2) If a function is needed, use the following:
//Adapted from https://github.com/adobe/reactor-turbine/blob/master/src/cleanText.js
function cleanText(str) {
return typeof str === 'string' ? str.replace(/\s+/g, ' ').trim() : str;