When implementing string operations that depend on the environment (terminal/UI rendering or filesystem paths), avoid naive .length/single-separator logic. Use algorithms that are correct for Unicode/code points and for the actual width/splitting rules of the runtime.
Apply:
/ and \ (after trimming trailing separators), so Windows and POSIX paths behave the same.Example patterns:
// Width-safe truncation (prefer established libs)
import stringWidth from 'string-width';
import cliTruncate from 'cli-truncate';
function truncateForTerminal(label: string, maxCells: number) {
// Truncates by terminal cells; avoids naive .length truncation.
// (Use/align with the same render-side truncation approach.)
return cliTruncate(label, maxCells, { ellipsis: '…' });
}
// Cross-platform path splitting
function splitDirPath(dir: string): { name: string; parent: string } {
const trimmed = dir.replace(/[\\/]+$/, '');
const parts = trimmed.split(/[\\/]/);
const name = parts.pop() ?? '';
const parent = parts.join('/');
return { name, parent };
}
Checklist:
.length to decide visual fit in terminal/UI.