export function whiteSpaceIgnorantRegex()

in src/utils/strings.js [171:200]


export function whiteSpaceIgnorantRegex(stringToSanitize) {
  let i;
  let char;
  const spaceMatch = '\\s*';
  const singleSpace = ' ';

  // If string is only spaces, return a single space.
  // Otherwise the resulting regex may have infinite matches.
  const trimmedString = stringToSanitize.trim();
  const len = trimmedString.length;
  if (!len) return singleSpace;

  const collector = [];
  // loop over each character
  for (i = 0; i < len; i += 1) {
    char = trimmedString[i];
    // if the character is an escape char, pass it and the next character
    if (char === '\\') {
      // skip one character in next iteration
      i += 1;
      // pass both escape char and char, with an empty space match before
      collector.push(`${spaceMatch}${char}`);
      collector.push(trimmedString[i]);
      // add anything else, but empty spaces
    } else if (char !== singleSpace) {
      collector.push(`${spaceMatch}${char}`);
    }
  }
  return collector.join('');
}