| 1234567891011121314151617 |
- export function chunkText(text, { chunkSize, chunkOverlap }) {
- const normalized = String(text ?? "").replace(/\r\n/g, "\n").trim();
- if (!normalized) return [];
- const chunks = [];
- let start = 0;
- while (start < normalized.length) {
- const end = Math.min(start + chunkSize, normalized.length);
- const chunk = normalized.slice(start, end).trim();
- if (chunk) chunks.push(chunk);
- if (end >= normalized.length) break;
- start = Math.max(0, end - chunkOverlap);
- }
- return chunks;
- }
|