Skip to content

Instantly share code, notes, and snippets.

@nyteshade
Created December 8, 2016 21:29
Show Gist options
  • Save nyteshade/b24983c14515a126a886929323674025 to your computer and use it in GitHub Desktop.
Save nyteshade/b24983c14515a126a886929323674025 to your computer and use it in GitHub Desktop.
Kill whitespace before and after template strings
/**
* A small helper for multiline template strings that allows you to
* not worry about new lines and indentations within your code from
* breaking up the format of the string.
*
* @param {Array} strings an array of Strings from the template, broken up by
* where the substitions are to be inserted.
* @param {Array} values an array of values to be inserted after each string
* of a matching index.
* @return {String} a template String without any prefixed or postfixed tabs
* and other whitespaced characters.
*/
function multiline(strings, ...values) {
let result = [];
for (let i = 0; i < strings.length; i++) {
let string = strings[i];
let value = values.length > i && `${values[i]} ` || ''
result.push(string
.replace(/(^\s*)?(.*)(\s*$)?/g, '$2')
.replace(/\r?\n/g, ' ')
);
result.push(value);
}
return result.join('');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment