Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@poppinss/colors

Package Overview
Dependencies
Maintainers
1
Versions
35
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@poppinss/colors

A wrapper on top of kleur with ability to write test against the color functions

  • 2.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
39K
decreased by-18.49%
Maintainers
1
Weekly downloads
 
Created
Source

Colors

Wrapper on top of Kleur with support for testing color calls.

circleci-image typescript-image npm-image license-image

This module is a wrapper on top of Kleur to make it easier to test the output generated using the kleur API. The API exposed is 100% the same as kleur.

Table of contents

Why use this module?

Have you ever wonder, how to test the output of function calls like the following?

import { bgRed, white } from 'kleur'
assert.equal(bgRed().white('Error'), 'Error') // fails

Well, you can make use of modules like strip-ansi to strip the ansi codes and get back the plain string.

import { bgRed, white } from 'kleur'
import stripAnsi from 'strip-ansi'

assert.equal(stripAnsi(bgRed().white('Error')), 'Error') // passes

However, this module takes a step forward with a fake colors API, that you can use during testing to reliably test the output.

import { FakeColors } from '@poppinss/colors'
const colors = new FakeColors()

assert.equal(colors.bgRed().white('Error'), 'bgRed(white(Error))') // passes

Usage

Install the package from npm registry as follows:

npm i @poppinss/colors

# yarn
yarn add @poppinss/colors

and then use it as follows:

import { Colors } from '@poppinss/colors'
const colors = new Colors()

// API same as kleur from here

When wring tests, you can make your code rely on FakeColors object instead of the Colors object. For example:

import { FakeColors } from '@poppinss/colors'
const colors = new FakeColors()

// API same as kleur from here

Raw Implementation

The Raw implementation exposes the same API as kleur, but does not apply any formatting/colors to the output string. You can use the raw implementation when the terminal does not support colors. For example:

import { Colors, Raw } from '@poppinss/colors'
import { level } from 'color-support'

const colors = level > 0 ? new Colors() : new Raw()
colors.red('hello world')

Finally, there is a method called getBest to get the best available implementation instance for your runtime.

import { getBest } from '@poppinss/colors'
const colors = getBest(false)

The getBest method returns following outputs

  • An instance of Raw when the terminal does not supports colors.
  • An instance of FakeColors when the first argument passed to the method is true.
  • Otherwise returns an instance of Colors.

Keywords

FAQs

Package last updated on 01 Jul 2020

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc