export function matchesLiteralSections()

in fusion-plugin-i18n/src/node.js [29:64]


export function matchesLiteralSections(literalSections: Array<string>) {
  return (translation: string) => {
    let lastMatchIndex = 0;

    if (literalSections.length === 1) {
      const literal = literalSections[0];
      return literal !== '' && translation === literal;
    }

    return literalSections.every((literal, literalIndex) => {
      if (literal === '') {
        // literal section either:
        // - starts/ends the literal
        // - is the result of two adjacent interpolations
        return true;
      } else if (literalIndex === 0 && translation.startsWith(literal)) {
        lastMatchIndex += literal.length;
        return true;
      } else if (
        literalIndex === literalSections.length - 1 &&
        translation.endsWith(literal)
      ) {
        return true;
      } else {
        // start search from `lastMatchIndex`
        const matchIndex = translation.indexOf(literal, lastMatchIndex);
        if (matchIndex !== -1) {
          lastMatchIndex = matchIndex + literal.length;
          return true;
        }
      }
      // matching failed
      return false;
    });
  };
}