1.0.0 • Published 7 years ago

soft-method-override v1.0.0

Weekly downloads
4
License
ISC
Repository
github
Last release
7 years ago

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)

1.0.0

7 years ago

0.0.0

7 years ago