Package detail

node-style-text

fisker20.2kMIT2.1.2

Chainable util.styleText().

readme

node-style-text

Npm Version MIT License Coverage

Chainable util.styleText().

Install

yarn add node-style-text

Usage

import styleText from 'node-style-text'

console.log(styleText.blue.underline('Hello world!'))
console.log(styleText.blue.underline`Hello ${'world'}!`))

Use process.stderr to validate colors.

import {stdout, stderr} from 'node-style-text'

// Validate `process.stderr` if it can be colored instead of `process.stdout`.
console.log(stderr.underline('Hello world!'))

Check options.stream for util.styleText for details.

Motivation

Provide convenience API.

  1. Chainable

    - import {styleText} from 'node:utils'
    + import styleText from 'node-style-text'
    
    console.log(
    -   styleText('blue', 'Hello world!')
    +   styleText.blue('Hello world!')
    )
    
    console.log(
    -   styleText(['blue', 'underline'], 'Hello world!')
    +   styleText.blue.underline('Hello world!')
    )
    
  2. Support tagged templates

    - import {styleText} from 'node:utils'
    + import styleText from 'node-style-text'
    
    console.log(
    -   styleText(['blue', 'underline'], `Hello ${'world'}!`)
    +   styleText.blue.underline`Hello ${'world'}!`
    )
    

API

styleText.<format>[.<format>...](string)

Example: styleText.red.bold.underline('Hello, world!');

Available formats: util.inspect.colors

Style

This module also provide an ESLint config to enforce use tagged templates when possible

// ✅
styleText.red(`foo`)

// ❌
styleText.red`foo`

Add this to you eslint.config.js

import eslintConfigNodeStyleText from 'node-style-text/eslint-config'

export default [
  // ... Your other eslint configs
  eslintConfigNodeStyleText,
]