templateLiteralTag.js 813 B

123456789101112131415161718192021
  1. /* eslint-disable import/prefer-default-export */
  2. import { arrayReduce } from '../helpers/array';
  3. /**
  4. * Tags a multiline string and return new one without line break characters and following spaces.
  5. *
  6. * @param {Array} strings Parts of the entire string without expressions.
  7. * @param {...String} expressions Expressions converted to strings, which are added to the entire string.
  8. * @returns {String}
  9. */
  10. export function toSingleLine(strings, ...expressions) {
  11. const result = arrayReduce(strings, (previousValue, currentValue, index) => {
  12. const valueWithoutWhiteSpaces = currentValue.replace(/(?:\r?\n\s+)/g, '');
  13. const expressionForIndex = expressions[index] ? expressions[index] : '';
  14. return previousValue + valueWithoutWhiteSpaces + expressionForIndex;
  15. }, '');
  16. return result.trim();
  17. }