File size: 1,207 Bytes
2bd3674
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
26
27
28
29
30
31
import {TranslationSentences} from './types/StreamingTypes';

export function getTotalSentencesLength(
  translatedSentences: TranslationSentences,
) {
  return translatedSentences.reduce((acc, curr) => acc + curr.length, 0);
}

/**
 * @returns A new array of strings where the total length of the strings === targetIndex,
 * aka it's as if we joined all the strings together, called joined.slice(0, targetIndex), and then
 * split the string back into an array of strings.
 */
export function sliceTranslationSentencesUpToIndex(
  translatedSentences: TranslationSentences,
  targetIndex: number,
): TranslationSentences {
  return translatedSentences.reduce<TranslationSentences>((acc, sentence) => {
    const accTotalLength = getTotalSentencesLength(acc);
    if (accTotalLength === targetIndex) {
      return acc;
    }
    // If adding the current sentence does not exceed the targetIndex, then add the whole sentence
    if (accTotalLength + sentence.length <= targetIndex) {
      return [...acc, sentence];
    }
    // If adding the current sentence DOES exceed the targetIndex, then slice the sentence and add it
    return [...acc, sentence.slice(0, targetIndex - accTotalLength)];
  }, []);
}