Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
The has-ansi npm package is used to check if a string contains ANSI escape codes. These codes are often used to format output in terminal applications, such as changing colors, styles, or other text attributes. This package provides a simple way to detect the presence of these codes in a given string.
Check for ANSI escape codes in a string
This feature allows you to check if a string contains ANSI escape codes. The function `hasAnsi` returns `true` if the string contains any ANSI escape codes, and `false` otherwise. This is useful for determining whether text formatting needs to be stripped or processed differently.
"const hasAnsi = require('has-ansi');\nconsole.log(hasAnsi('\u001B[4mUnicorn\u001B[0m')); // true\nconsole.log(hasAnsi('Unicorn')); // false"
Chalk is a popular npm package for styling terminal text. Unlike has-ansi, which is used to detect ANSI codes, Chalk is used to apply styles such as color, background color, and text formatting (bold, italic) through chainable methods. Chalk focuses on creating styled text, while has-ansi is about detection.
Strip-ansi is a package that removes ANSI escape codes from a string. It complements has-ansi by providing the functionality to clean a string of any ANSI codes that has-ansi can detect. While has-ansi checks for the presence of ANSI codes, strip-ansi is used to remove them.
Ansi-regex is a package that provides a regular expression to match ANSI escape codes. It can be used for a similar purpose as has-ansi, but with a more manual approach, giving developers the flexibility to use the regex in various contexts. Has-ansi offers a simpler and more direct API for detection.
Check if a string has ANSI escape codes
npm install has-ansi
import hasAnsi from 'has-ansi';
hasAnsi('\u001B[4mUnicorn\u001B[0m');
//=> true
hasAnsi('cake');
//=> false
FAQs
Check if a string has ANSI escape codes
We found that has-ansi demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.