soft-method-override
v1.0.0
Published
handy way to override an internal method from an Object in runtime when you need to execute a different flow for that method
Downloads
2
Maintainers
Readme
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
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)
})