/dodollar

Method Chaining, Lifecycle Hooks, Extended, Lightweight Output CONSOLE

Primary LanguageTypeScript

🌟 DoDollar 🌟


Method Chaining, Lifecycle Hooks, Extended, Lightweight Output CONSOLE


boy

Table of Contents

Docs

Installation

  1. npm
npm i dodollar
  1. yarn
yarn add dodollar
  1. bun
bun add dodollar
// or
bun a dodollar

Basic Usage

Act like console.log.

import $$ from 'dodollar';

$$.log('foo');
//> foo

DoDollar compatible with commonly used several methods:

  1. log
  2. info
  3. debug
  4. warn
  5. error
  6. time
  7. timeLog
  8. timeEnd

Method Chaining

$$.log('foo').warn('baz').error('xyzzy');

Output:

method chaining

Extended Utilities

There are some extended utilities for convenience.

$$.separate().title('Debug point').blankLine().log('foo').separate();

Output: extended utilities

title method

Output a title.

$$.title('Hello DoDollar');

// Output
//> # Hello DoDollar

blankLine method

Output blank lines. accept a number to indicate count of blank line.

start and end methods

Same as console.group() and console.groupEnd() make the multiply output line becomes a group and you can easily collapse it:

$$.start('Start').title('Hello DoDollar').separate().end();

Output:

start and end

separate

Output one line separator, dash( - ) as separator character and maximum number is 80 by default.

$$.separate().separate('*').separate('_-', 150);

Output:

separate

Lifecycle Hooks

Concept

DoDollar support adding custom lifecycle hooks into one method.

Normal method invoke:

Method invoke with hooks:

You can add three types of hook:

  1. intercept: intercept the method execution and over it if hook function return true.
  2. before: execute hook function before method execution.
  3. after: same as before, execute hook function after method is executed.

before and after didn't effect-side for the method.

Usage

With help of lifecycle hook, you can build custom console output for your page or whole project:

// myDoDollar.ts

import { DoDollar } from 'dodollar';
import $$ from 'dodollar';

const myDoDollar = new DoDollar({
  beforeLog: () => {
    $$.log('before log...');
  },
  afterError: () => {
    $$.log('Report error to server...');
  },
  interceptInfo: () => {
    $$.log('Intercept execution in production environment.');
    return true;
  },
});

export default myDoDollar;

Deliver custom hooks into DoDollar constructor. Import your custom dodollar:

import $$ from './myDodollar';

$$.log('I own beforeLog()')
  .blankLine()
  .error('I own afterError()')
  .blankLine()
  .info("I own interceptInfo() and you can't see me.");

Road Map

The list below should give some indication of my plans for the next major release, and for the future.

  • Setting hook in batches according to different environment.
  • Chinese version docs.

Author

Pandy
Pandy

License

MIT © Pandy