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

dbg-expr

v1.0.2

Published

A helper for evalutating and logging an expression at the same time.

Downloads

5

Readme

A helper for evalutating and logging an expression at the same time. Idea taken from Rust dbg! macro.

npm npm type definitions npm bundle size

Instead of separating "working" logic in order to log out individual pieces, use this function to log the expression and value in place!


Features

  • display called location (file and line number)
  • show unevaluated expression (with variable names intact!)
  • value of expression result
  • returns value so it can be used in place! (very useful - see examples)

Installation

# yarn
$ yarn add -D dbg-expr

# npm
$ npm install --save-dev dbg-expr

Please note that the $ character above is to denote use in a terminal. The command will fail if you enter that character when installing.


Usage

Just import the function and call it around an expression, either on its own line or an existing line.

For example:

import dbg from 'dbg-expr';

// on its own line (like console.log())
dbg(() => 4 - 1); // [/src/index.js:16] 4 - 1 = 3

// on an existing line
const value = 8 * dbg(() => 1 + 1); // [/src/index.js:44] 1 + 1 = 2

// using named variables
const age = api.getUser().age;
dbg(() => age); // [/models/user.js:25] age = 30

That last one is easier to type than:

console.log('age', age);


Log Format

The output format depends on whether you are logging to a terminal in Node vs the browser console via client-side JavaScript.

Node: [location] expression = value

Client-side:



FAQ

But why??

Well, I believe this has multiple benefits. An incomplete list might be:

  1. It's less to type 😊.
  2. If you want to log the output of a function, you would normally have to run the function twice: once to log the value and another to return the value. This could have adverse affects (ie: a non-immutable action like dispatching);
  3. Logging part of an expression. What if you want to see the value of the whole object, but then return only a property? This would mean you have to separate it into a variable, log the variable, and then continue on - like returning it; logging in-place solves this! Here's an example that illustrates how easy this package makes it!
// data coming from somewhere else
const people = [
  { first: 'Andy', last: 'Taylor' },
  { first: 'Opie', last: 'Taylor' },
  { first: 'Barney', last: 'Fife' },
  { first: 'Otis', last: 'Campbell' },
];

// original code
const uppercaseFirstNames = people.map(person => person.first.toUpperCase());

// verbose console.log() code
const uppercaseFirstNames = people.map(person => {
  const firstName = person.first;
  console.log('firstName', firstName);
  return firstName.toUpperCase();
});

// concise code by just wrapping in dbg()
const uppercaseFirstNames = people.map(person => dbg(() => person.first).toUpperCase());

Why do I need to pass in a function?

You'll notice that the expression is wrapped in a function. This is the only way to keep the expression unevaluated and logged out for you! If you pass only the expression, you'll still see the location and value, but not the stringified expression.

Why does the logged expression look so much different than my code? / Why am I not seeing a file location and line number?

You may have uglify/minify enabled in your bundler or framework which alters the output significantly. See the section below for help on this.

Do I have to use ES6 arrow functions?

You may have noticed that all of the examples above are using ES6 arrow functions. You may also use regular anonymous functions with the function keyword, like so:

var variable = 98;

// [/src/index.js:8] variable + 1 = 99
dbg(function() {
  return variable + 1;
});

Can I globally define this?

Currently you could attach this function to whatever long-lived object you wish, such as the client-side window object. Perhaps this is done in a Nuxt client-side plugin.



Disable Uglify / Minify in Development

This is the key to making this work well in some cases.. Below is a list of popular frameworks and setups and how you can disable these features in development. If you figure it out for one that is not listed, please submit a PR so others can benefit!