A JavaScript library that accurately calculates string length by handling astral symbols and ANSI escape codes.
string-length is a JavaScript library that calculates the accurate character length of a string by correctly handling Unicode astral symbols and ANSI escape codes. It solves the problem where native JavaScript string length methods miscount certain characters, such as emojis or terminal color codes, which is crucial for applications involving text rendering, terminal interfaces, or internationalization.
JavaScript and Node.js developers who need precise string length calculations for tasks like terminal output formatting, text layout, or handling multilingual content with complex Unicode characters.
Developers choose string-length because it provides a reliable, lightweight solution for accurate string length measurement, addressing specific edge cases that built-in methods fail to handle, without unnecessary dependencies or complexity.
Get the real length of a string - by correctly counting astral symbols and ignoring ansi escape codes
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Correctly counts astral symbols like emojis as single characters, fixing the native String#length issue where they are erroneously counted as two, as shown in the README example.
By default ignores ANSI escape codes, ensuring visible text length is measured for terminal applications, which is essential for formatting output without styling interference.
Offers a countAnsiEscapeCodes option to include ANSI codes in length calculation, providing flexibility for different use cases where escape codes might be relevant.
A dependency-free module that efficiently solves specific edge cases without bloat, aligning with its philosophy of minimalism and reliability.
Does not handle grapheme clusters or combining characters, which are common in languages with complex scripts, potentially leading to inaccurate counts in full text rendering scenarios.
Only calculates string length; developers must integrate other libraries like string-width for related tasks, increasing toolchain complexity for comprehensive text processing.
For strings composed solely of basic Latin characters, using this library adds unnecessary overhead compared to the native String#length property, making it inefficient in such contexts.