npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

debug-color-formatter

v0.1.1

Published

A `%c` formatter for [debug][] to unify rich text formatting on the server with [how it’s done in the browser console][browser]. Result: the same formatting and **no additional code is sent to the browser!**

Downloads

37

Readme

debug-color-formatter

A %c formatter for debug to unify rich text formatting on the server with how it’s done in the browser console. Result: the same formatting and no additional code is sent to the browser!

screenshot

Usage

Install

Add to your project with your choice of package manager:

$ yarn add debug-color-formatter
$ npm install debug-color-formatter

Setup

All you need to do is add this function to debug’s formatters object:

import createDebug from 'debug';
import colorFormatter from 'debug-color-formatter';

createDebug.formatters.c = colorFormatter;

Formatting

Use %c and CSS style rules to format text in your debug messages:

const debug = createDebug('demo');

debug(
  'Fancy mode is disabled, set %coptions.fancy.enabled%c to change.',
  'color: cyan',
  ''
);
debug(
  '%c!%c 429 Too Many Requests. The request will be retried.',
  'color: red',
  ''
);
debug(
  '%cWARNING%c This feature will be deprecated in the next release.',
  'font-weight: bold; background-color: #ffcf00; color: black',
  ''
);

Almost any formatting that can be accomplished in a standard terminal with ANSI escape codes will be translated. Others that may work in the browser console (e.g. text-shadow) will be ignored.

To reset all formatting, render %c with an empty string value. It is good practice to reset all formatting at the end of your message. To reset specific properties, use their standard initial CSS value or inherit, e.g.

debug(
  '%cUnderlined text!%c Still bold here.%c',
  'font-weight: bold; text-decoration: underline',
  'text-decoration: none',
  ''
);

Motivation

When you want your debug logging code to be “universal” or “isomorphic” (the same code runs on the server and web browser), the fantastic debug library already has you covered there.

But what if you want some simple text formatting in your debug messages, like colors and bold text? This can further aid in debugging by rendering certain errors in red, warnings in yellow, emphasizing variable names or config options, etc.

Both the server and (most) browser consoles have the ability to render such formatted text. But they do this very differently: on the server, you must use ANSI escape codes, while browsers use a %c substitution directive with styles specified via CSS strings.

You have two options for reconciling this difference: either you ship extra code to the browser to translate ANSI codes to the %c style substitution, or you do the reverse for the server. Unfortunately, the former option adds to your bundle size. If we instead make %c formatting work on the server, then we don’t have to send any new code to the browser at all! And thankfully, debug allows you to add new formatting directives.

Bundling

Your bundler should understand the browser field in package.json in order to point to the correct code for the browser. webpack does this by default.

Note that the correct code for the browser is a completely empty file – the point of this library is to bring a browser feature to the server, not the other way around! 🙂