Skip to main content
Glama
positionUtils.ts4.14 kB
import { Unit } from '../types/index.js'; /** * Извлекает контент по строкам */ export function getContentByLines(content: string, startLine: number, countLines: number): string { const lines = content.split('\n'); if (startLine < 1 || startLine > lines.length) { throw new Error(`Start line ${startLine} is out of range (1-${lines.length})`); } const startIndex = startLine - 1; // Convert to 0-based const endIndex = Math.min(startIndex + countLines, lines.length); return lines.slice(startIndex, endIndex).join('\n'); } /** * Извлекает контент по символам */ export function getContentByChars(content: string, startPos: number, countChars: number): string { if (startPos < 0 || startPos >= content.length) { throw new Error(`Start position ${startPos} is out of range (0-${content.length - 1})`); } const endPos = Math.min(startPos + countChars, content.length); return content.slice(startPos, endPos); } /** * Вставляет контент на указанную строку */ export function insertContentAtLine(content: string, lineNumber: number, insertText: string, replaceCount?: number): string { const lines = content.split('\n'); if (lineNumber < 1 || lineNumber > lines.length + 1) { throw new Error(`Line number ${lineNumber} is out of range (1-${lines.length + 1})`); } const insertIndex = lineNumber - 1; // Convert to 0-based if (replaceCount && replaceCount > 0) { // Replace existing lines const endIndex = Math.min(insertIndex + replaceCount, lines.length); lines.splice(insertIndex, endIndex - insertIndex, insertText); } else { // Insert without replacing lines.splice(insertIndex, 0, insertText); } return lines.join('\n'); } /** * Вставляет контент в указанную позицию по символам */ export function insertContentAtChar(content: string, charPosition: number, insertText: string, replaceCount?: number): string { if (charPosition < 0 || charPosition > content.length) { throw new Error(`Character position ${charPosition} is out of range (0-${content.length})`); } if (replaceCount && replaceCount > 0) { // Replace existing characters const endPos = Math.min(charPosition + replaceCount, content.length); return content.slice(0, charPosition) + insertText + content.slice(endPos); } else { // Insert without replacing return content.slice(0, charPosition) + insertText + content.slice(charPosition); } } /** * Удаляет контент по строкам */ export function removeContentByLines(content: string, startLine: number, countLines: number): string { const lines = content.split('\n'); if (startLine < 1 || startLine > lines.length) { throw new Error(`Start line ${startLine} is out of range (1-${lines.length})`); } const startIndex = startLine - 1; // Convert to 0-based const endIndex = Math.min(startIndex + countLines, lines.length); lines.splice(startIndex, endIndex - startIndex); return lines.join('\n'); } /** * Удаляет контент по символам */ export function removeContentByChars(content: string, startPos: number, countChars: number): string { if (startPos < 0 || startPos >= content.length) { throw new Error(`Start position ${startPos} is out of range (0-${content.length - 1})`); } const endPos = Math.min(startPos + countChars, content.length); return content.slice(0, startPos) + content.slice(endPos); } /** * Валидирует позицию относительно контента */ export function validatePosition(content: string, position: number, unit: Unit): void { if (unit === 'lines') { const lines = content.split('\n'); if (position < 1 || position > lines.length + 1) { throw new Error(`Line position ${position} is out of range (1-${lines.length + 1})`); } } else { if (position < 0 || position > content.length) { throw new Error(`Character position ${position} is out of range (0-${content.length})`); } } }

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/witqq/clipboard-mcp'

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