Skip to main content
Glama
chunker.ts869 B
const DEFAULT_SEPARATOR = /\s+/g; export function chunkText( text: string, chunkSize: number, chunkOverlap: number, ): string[] { if (chunkSize <= 0) { throw new Error("chunkSize must be positive"); } const normalized = text.replace(/\r\n/g, "\n").trim(); if (!normalized) { return []; } const tokens = normalized.split(DEFAULT_SEPARATOR).filter(Boolean); if (!tokens.length) { return []; } const maxTokens = Math.max(chunkSize, 32); const overlap = Math.max(0, Math.min(chunkOverlap, maxTokens - 1)); const step = Math.max(1, maxTokens - overlap); const chunks: string[] = []; for (let start = 0; start < tokens.length; start += step) { const slice = tokens.slice(start, start + maxTokens); const chunk = slice.join(" ").trim(); if (chunk) { chunks.push(chunk); } if (slice.length < maxTokens) { break; } } return chunks; }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/JaxsonWang/docs-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server