UNPKG

@jbrowse/core

Version:

JBrowse 2 core libraries used by plugins

43 lines (42 loc) 1.7 kB
export function splitString({ str, charactersPerRow, showCoordinates, currRemainder = 0, spacingInterval = 10, }) { const numChunks = Math.ceil(str.length / charactersPerRow); const segments = new Array(numChunks); let positionCounter = currRemainder % spacingInterval; let chunkIndex = 0; let stringOffset = 0; for (; chunkIndex < numChunks + 1; ++chunkIndex) { const chunkSize = chunkIndex === 0 ? charactersPerRow - currRemainder : charactersPerRow; const currentChunk = str.slice(stringOffset, stringOffset + chunkSize); if (!currentChunk) { break; } segments[chunkIndex] = showCoordinates ? formatWithCoordinateSpacing(currentChunk, positionCounter, spacingInterval) : currentChunk; positionCounter = 0; stringOffset += chunkSize; } return { segments, remainder: calculateRemainder(segments, chunkIndex, currRemainder, charactersPerRow), }; } function formatWithCoordinateSpacing(chunk, startPosition, spacingInterval) { if (!chunk) { return ''; } let formattedChunk = ''; for (let i = 0, j = startPosition; i < chunk.length; i++, j++) { if (j % spacingInterval === 0) { formattedChunk += ' '; j = 0; } formattedChunk += chunk[i]; } return formattedChunk; } function calculateRemainder(segments, chunkIndex, currRemainder, charactersPerRow) { const lastSegmentLength = segments.at(-1)?.replaceAll(' ', '').length || 0; const additionalRemainder = chunkIndex < 2 ? currRemainder : 0; return (lastSegmentLength + additionalRemainder) % charactersPerRow; }