package
A fast function for calculating where a string should be truncated, given an optional width limit and an ellipsis string.
npm install fast-string-truncated-widthGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
Tagged topics.
Estimated unpacked size across 1 packages.
11.9 kB
Includes dependencies.
A fast function for calculating where a string should be truncated, given a width limit and an ellipsis string.
This is a low-level function that basically calculates the visual width of a string and the index at which it should be truncated once printed to the terminal, but taking into account an optional width limit and an optional ellipsis string, so that the string doesn't have to be processed multiple times to be truncated, and how long the part after the truncation point is doesn't cost us anything because we can just ignore it.
Note that codepoints considered "ambiguous" in Unicode will always use "regularWidth" as their width, if you need to customize this you should use string-width instead.
npm install fast-string-truncated-widthimport fastStringTruncatedWidth from 'fast-string-truncated-width';
// The width of various classes of characters is configurable
const widthOptions = {
controlWidth: 0,
tabWidth: 8,
emojiWidth: 2,
regularWidth: 1,
wideWidth: 2
};
// Retrieving the result for a string that fits within our width limit
const result1 = fastStringTruncatedWidth ( '\x1b[31mhello', { limit: Infinity, ellipsis: '…' }, widthOptions );
result1.truncated; // => false, the string fits within the width limit, it doesn't have to be truncated
result1.ellipsed; // => false, the ellipsis string doesn't need to be appended to the string
result1.width; // => 5, the visual width of the string once printed to the terminal
MIT © Fabio Spampinato