Coder Social home page Coder Social logo

tonsole's Introduction

Tonsole

Wrapper around Node's console that adds return values to logging calls.

This is useful for logging expressions and arrow functions without having to change any code.

const tonsole = require('tonsole');

let compute = (data) => data ? process(data.value) : data;

// Normally we can't easily log what `compute` does without
// rewriting it to use statements
compute = (data) => {
    if (data) {
        const r = process(data.value);
        console.log('Processed data: ', r);
        return r;
    }
    console.error('Invalid data: ', data);
    return data;
};

// But with tonsole, we can add logging while still using
// expressions, just as the ancient ones of Lisp intended.
compute = (data) =>
    data
        ?tonsole.log('Processed data: ', process(data.value))
        :tonsole.error('Invalid data: ', data);

Usage

$ npm install --save tonsole

Tonsole provides a complete wrapper around the entire Node console api. All functions forward to the wrapped Node console implementation.

tonsole.Console(stdout [, stderr])

Wrapper around console.Console. Logging methods of this class return values besides logging.

tonsole.log(...args)

tonsole.info(...args)

tonsole.warn(...args)

tonsole.error(...args)

Each of these methods logs using the inner console.* method, but instead of returning undefined, they return the last argument value.

tonsole.log("%s : %s", 2, 3);
// Logs: "2 : 3"
// returns 3

tonsole.assert(value, ...args)

Verify that value is truthy using console.assert but returns value. If value is falsy, an AssertionError is thrown.

tonsole.assert(5, "sanity check") === 5

tonsole.assert(false, "should fail") // throws AssertionError 

tonsole.dir(obj, options)

Simply wrapper around console.dir that returns obj.

tonsole.time(label)

tonsole.timeEnd(label)

tonsole.trace(msg, ...)

Forward directly to console.* and continue to return undefined.

tonsole's People

Contributors

mattbierner avatar

Watchers

 avatar  avatar  avatar

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.