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

styled-log

v1.1.0

Published

style your console logs in a familiar way

Downloads

4

Readme

Styled-Logs

This is a basic library for styling console logs.

Play with this on codesandbox.io!

Usage

npm install styled-log

or include it via CDN with

<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/emnudge/Styled-Logs/build/StyledLogs.min.js"></script>

Background

console.log() allows for basic css styling which uses a syntax similar to:

console.log(
  "This is %cstyled-logs%cv0.1.0%c\n\nStyle your %cconsole%clogs%c\nin a familiar way!",
  "color: white; background: linear-gradient(#555, #333); padding: 2px 6px; border-radius: 4px 0 0 4px;",
  "color: white; background: linear-gradient(#E86, #C64); padding: 2px 6px; border-radius: 0 4px 4px 0;",
  "",
  "color: white; background: linear-gradient(#555, #333); padding: 2px 6px; border-radius: 4px 0 0 4px;",
  "color: white; background: linear-gradient(#E86, #C64); padding: 2px 6px; border-radius: 0 4px 4px 0;",
  ""
);

which produces the following:

stylized console log

As you can see, while this is a really cool feature, the syntax makes it fairly hard to read and create. This miniature library creates a much more familiar syntax for styling complex stylized console logs.

Syntax

new StyledLog().html`
  This is <div class="name">styled-logs</div>
  <div class="version">v0.1.0</div>
  <br />
  <br />
  Style your <div class="name">console</div>
  <div class="version">logs</div>
  <br />
  in a familiar way!
`.css`
  .name { 
    color: white;
    background: linear-gradient(#555, #333);
    padding: 2px 6px;
    border-radius: 4px 0 0 4px;
  }
  .version {
    color: white;
    background: linear-gradient(#E86, #C64);
    padding: 2px 6px;
    border-radius: 0 4px 4px 0;
  }
`.log();

.html() and .css() can be chained or kept separately. They are Tagged Template Literals and they can also be called multiple times to change either html or css data.

Syntax Highlighting

As a side note, you may find it hard to read html/css without syntax highlighting. As these are tagged template literals, we can take advantage of some VS Code extensions meant for other frameworks. es6-string-css can highlight the css for us and lit-html can highlight the html for us.

Aliases

The StyleLog class also includes an alias object. This matches up self-closing tags with particular texts. By default it is set to { br: '\n' }, but it can be mutated to allow for custom and dynamic self-closing tags.

example:

// setting up our log, using the custom self-closing tag "score"
const scoreLog = new StyledLog().html`
  High Score: <score class="score" />!
`.css`
  .score {
    color: red;
  }
`;

// setting score's inner text to "45"
scoreLog.alias.score = 45;
scoreLog.log();

// setting score's inner text to "512"
scoreLog.alias.score = 512;
scoreLog.log();

If we set an alias to be a function, we can take in properties as input.

const scoreLog = new StyledLog().html`
  Scores: <score multiplier="1" />, <score multiplier="2" />
`;

let scoreNum = 10;
scoreLog.alias.score = ({ multiplier }) => 
  scoreNum * Number(multiplier);

scoreLog.log(); // Scores: 10, 20

scoreNum = 75;
scoreLog.log(); // Scores: 75, 150

Note

Console logs have minimal and arbitrary styling options. Most styles will not work. Experiment. This library transpiles the pseudo-html and pseudo-css into objects and/or arrays. This is not anywhere close to real html and css. The format is only similar to make creating these stylized logs more intuitive.

Things like event listeners, pseudo-elements (ironic, I know), IDs, css variables, etc will not work.

Syntax Not Supported

  • ids (# selector)
  • nesting (not in stylesheet nor html)
  • glob or sibling css selectors (*, ~, +)
  • unclosed p tags
  • default styling (divs do not have display: block)