UNPKG

vike

Version:

(Replaces Next.js/Nuxt) 🔨 Composable framework to build advanced applications with flexibility and stability.

19 lines (18 loc) • 609 B
export { truncateString }; import pc from '@brillout/picocolors'; import { assert } from './assert.js'; function truncateString(str, lenMax) { const lenMaxReal = lenMax - 3; assert(lenMaxReal >= 1); // Show at least one character before the ellipsis if (str.length < lenMax) { return str; } else { // Breaks ANSI codes. // - So far, the `str` we pass to truncateString(str) is always expected to not contain any ANSI code str = str.substring(0, lenMaxReal); const ellipsis = pc.dim('...'); str = str + ellipsis; return str; } }