soft-method-override

1.0.0 • Public • Published

soft-method-override

handy way to override an internal method from an Object in runtime when you need to execute a different flow for that method


Build StatusCoverage StatusISC LicenseNodeJS

JavaScript Style Guide

api

const softMethodOverride = require('soft-method-override')

softMethodOverride(
    `this - value of the enclosing execution context`,
    `the method name to be override - string`,
    `the new method - function`
  )

example

const softMethodOverride = require('soft-method-override')
 
class Hitman {
 
  constructor () {
    this._hitmans = []
  }
 
  add (name) {
    if (typeof name !== 'string' || !name.length) {
      softMethodOverride(this, 'contract', (cb) => {
        cb(new Error('no name no contract'))
      })
    } else {
      this._hitmans.push(name)
    }
 
    return this
  }
 
  contract (cb) {
    cb(null, 'yay!! a new Chapter')
  }
}
 
//
//
//
 
const john = new Hitman()
 
john
  .add()
  .contract((err, res) => {
    assert.equal(err.message, 'no name no contract')
    assert.deepEqual(res, undefined)
  })
 

ISC License (ISC)

Package Sidebar

Install

npm i soft-method-override

Weekly Downloads

0

Version

1.0.0

License

ISC

Last publish

Collaborators

  • quim