@jiminp/tooltool
    Preparing search index...

    Function getNextChunkLength

    • Calculates the optimal length for the next text chunk.

      For text exceeding the limit, uses the rightmost eligible occurrence of the first matching separator. Its start must be at least Math.floor(max_length / 2), and its end must fit within the limit at a code-point boundary.

      Parameters

      • text: string

        The text to split; whitespace is not trimmed.

      • max_length: number

        Maximum chunk length in UTF-16 code units (positive safe integer).

      • separators: string[] = ...

        Literal split points in priority order (default: ['\n', ' ', '.']).

      Returns number

      The raw prefix length, including its separator, or 0 for empty text.

      If max_length is not a positive safe integer.

      Returns the whole length if the text already fits. Otherwise, falls back to the largest prefix within the limit that does not split a surrogate pair. With max_length equal to 1, an initial non-BMP code point returns 2. This is code-point safety, not grapheme safety: combining marks and ZWJ sequences can still be separated. The returned length may exceed the trimmed chunk's length.

      getNextChunkLength('hello world', 7); // 6 (includes the space)
      getNextChunkLength('abcdefghij', 5); // 5