combineOriginalAndKana.js
/**
 * Combine any JP string input with it's corresponding -kana transcription, to groups of original-kana pairs
 * useful for kanji-only furigana readability.
 *
 * @param aOriginal {string} - original JP string comprised of kanji / hiragana / katakana symbols
 * @param aKana {string} - corresponding JP string in hiragana / katakana symbols only
 * @return combinedGroups [] - array of string pairs of original-to-kana combinations.
 */
Combine any JP string input with it's corresponding -kana transcription, to groups of original-kana pairs
useful for kanji-only furigana readability.


Combine any JP string input with it ' s corresponding - kana transcription to groups of original - kana pairs useful for kanji - only furigana readability .
function combineOriginalAndKana(aOriginal, aKana) {
  let i = 0;
  let j = 0;
  const originalLen = aOriginal.length;
  const kanaLen = aKana.length;
  const combinedGroups = [];
  while (
    (i < originalLen && j + 1 < kanaLen) ||
    (j + 1 === kanaLen && combinedGroups.length === 0)
  ) {
    let originals = "";
    let kanas = "";
    if (_isKanji(aOriginal[i])) {
      originals += aOriginal[i];
      kanas += aKana[j];
      while (j + 1 < kanaLen && aOriginal[i + 1] !== aKana[j + 1]) {
        kanas += aKana[j + 1];
        j++;
      }
      while (i + 1 < originalLen && _isKanji(aOriginal[i + 1])) {
        originals += aOriginal[i + 1];
        i++;
      }
      i++;
      j++;
    } else {
      while (j < kanaLen && !_isKanji(aOriginal[i])) {
        originals += aOriginal[i];
        kanas += aOriginal[i];
        i++; 
        j++; 
      }
    }
    combinedGroups.push([originals.trim(), kanas.trim()]);
  }
  return combinedGroups;
}
function combineOriginalAndKana ( aOriginal , aKana ) { let i = 0 ; let j = 0 ; const originalLen = aOriginal . length ; const kanaLen = aKana . length ; const combinedGroups = [ ] ; while ( ( i < originalLen && j + 1 < kanaLen ) || ( j + 1 === kanaLen && combinedGroups . length === 0 ) ) { let originals = "" ; let kanas = "" ; if ( _isKanji ( aOriginal [ i ] ) ) { originals += aOriginal [ i ] ; kanas += aKana [ j ] ; while ( j + 1 < kanaLen && aOriginal [ i + 1 ] !== aKana [ j + 1 ] ) { kanas += aKana [ j + 1 ] ; j ++ ; } while ( i + 1 < originalLen && _isKanji ( aOriginal [ i + 1 ] ) ) { originals += aOriginal [ i + 1 ] ; i ++ ; } i ++ ; j ++ ; } else { while ( j < kanaLen && ! _isKanji ( aOriginal [ i ] ) ) { originals += aOriginal [ i ] ; kanas += aOriginal [ i ] ; i ++ ; j ++ ; } } combinedGroups . push ( [ originals . trim ( ) , kanas . trim ( ) ] ) ; } return combinedGroups ; }
Combine any JP string input with it's corresponding -kana transcription, to groups of original-kana pairs
 useful for kanji-only furigana readability.
Combine any JP string input with it ' s corresponding - kana transcription to groups of original - kana pairs useful for kanji - only furigana readability .
// while: not finished orig AND next kanas available, OR next kana is tbe last one and not any result yet. // start a new group // grab next kanas, until the next kana matches the next orig char (or no more kana left) // grab next contiguous kanjis // now, next chars are not kanji, and both equal => advance indexes and start a new group. // current orig is not kanji // grab origs & kanas, until a kanji appears (or no more next chars) // advance originals index // advance kanas index