Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ensure_single_line.cjs « locale « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: abdd56c35892fa0ed34b25e2a63ee804e9be5e2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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;
};