summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/locale/ensure_single_line.js
blob: c2c637770016d7735c0c2fc3d96bbc4df5b88959 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/* eslint-disable import/no-commonjs */

const SPLIT_REGEX = /\s*[\r\n]+\s*/;

/**
 *
 * strips newlines from strings and replaces them with a single space
 *
 * @example
 *
 * ensureSingleLine('foo  \n  bar') === 'foo bar'
 *
 * @param {String} str
 * @returns {String}
 */
module.exports = function ensureSingleLine(str) {
  // This guard makes the function significantly faster
  if (str.includes('\n') || str.includes('\r')) {
    return str
      .split(SPLIT_REGEX)
      .filter((s) => s !== '')
      .join(' ');
  }
  return str;
};