Skip to content

joaquimserafim/soft-method-override

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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)

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published