Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including cmild with all npm packages installed. Try it out:

var cmild = require("cmild")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

cmild v1.1.1

Class methods invocation logging decorator

Class methods invocation logging decorator

License Coverage Status

Description

Decorator for simply and colorful (in Chrome, FF and Node) logging class methods invocation, returned results and possible errors. colors.js is using for Node.

Installation

yarn add cmild

Testing

yarn test:jest # Runs Jest with coverage collection
yarn test:coverage # Sends coverage to .coveralls.io
yarn test # yarn test:jest && yarn test:coverage

Usage

You may decorate your class with default styles params, which are:

{
  styles: {
    browser: {
      invocation: 'background: #F2EAFF; color: #03A9F4; font-weight: bold',
      result: 'background: #F2EAFF; color: #4CAF50; font-weight: bold',
      error: 'background: #F2EAFF; color: #F20404; font-weight: bold',
    },
    node: {
      invocation: 'bgBlack.blue.bold',
      result: 'bgBlack.green.bold',
      error: 'bgBlack.red.bold',
    }
  }
}

Or set your own styles by passing object as a first argument of decorator, for example:

@Cmild({
  styles: {
    browser: {
      invocation: 'background: #F2EAFF; color: #FFEF00; font-weight: bold',
    }
  },
})

Also you may disable decorator in specific env/build, for example:

@Cmild({
  enabled: process.env.NODE_ENV !== production,
})

Common usage:

import Cmild from 'cmild'


@Cmild
class TestClass {
  testMethod1 (testArg = '') {
    return testArg
  }
  async testMethod2 (testArg1 = '', testArg2 = '') {
    return `${testArg1} ${testArg2}`
  }
  erroredMethod1 (erroredArg = '') {
    throw new Error(this.testMethod1(erroredArg))
  }
  async erroredMethod2 (erroredArg1 = '', erroredArg2 = '') {
    throw new Error(await this.testMethod2(erroredArg1, erroredArg2))
  }
}

const instance = new TestClass()

// Frist method invocation and result printing
console.log(instance.testMethod1('testArg'))

// Second method invocation and result printing when returned promise is resolved
instance.testMethod2('testArg1', 'testArg2').then(console.log)

// Third method invocation and error catching
try {
  instance.erroredMethod1('erroredArg')
} catch (error) {
  console.error(error)
}

// Fourth method invocation and error cathing when returned promise is rejected
instance.erroredMethod2('erroredArg1', 'erroredArg2').catch(console.error)

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free