Git Product home page Git Product logo

dbg-expr's Introduction

Debug

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: browser console example



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!

dbg-expr's People

Contributors

dependabot[bot] avatar parker-codes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

dbg-expr's Issues

Some questions about using this in production

Well first of all, is this project still maintained? Because I see that the last commit was 4 years ago.

Secondly, is it possible to use this in production and configure it to log to some external service (e.g. Sentry) and only send the log maybe when some error has occurred? Maybe as part of the stack trace if that's possible?

Thirdly, is it possible to use minify in production but to then use source maps in order to still figure out what the correct filename, line number, and maybe even variable names in the expression would have been in the original source code?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.