function diffWordsToChars()

in src/app/diff.js [47:91]


  function diffWordsToChars(text1, text2) {
    const wordArray = [];
    const wordHash = {};
    const hasOwnProperty = Object.hasOwnProperty;

    // '\x00' is a valid character, but various debuggers don't like it.
    // So we'll insert a junk entry to avoid generating a null character.
    wordArray[0] = '';

    function encodeWordsToChars(text) {
      let chars = '';
      let wordStart = 0;
      let wordEnd = 0;
      let wordArrayLength = wordArray.length;
      const textEnd = text.length;
      const wb = /\B/;

      while (wordEnd < textEnd) {
        wordEnd = (!wb.test(text[wordStart])
          ? indexOf(text, wb, wordStart)
          : Math.min(wordEnd + 1, textEnd));

        if (wordEnd === -1) {
          wordEnd = textEnd;
        }


        const word = text.substring(wordStart, wordEnd);
        wordStart = wordEnd;

        if (!hasOwnProperty.call(wordHash, word)) {
          wordHash[word] = wordArrayLength;
          wordArray[wordArrayLength++] = word;
        }

        chars += String.fromCharCode(wordHash[word]);
      }

      return chars;
    }

    const chars1 = encodeWordsToChars(text1);
    const chars2 = encodeWordsToChars(text2);
    return {chars1, chars2, lineArray: wordArray};
  }