https://github.com/ahmedelwerdany/invoke-before-after
Tell your methods when to invoke by just naming them that way.
https://github.com/ahmedelwerdany/invoke-before-after
call class function hooks javascript nodejs npm object proxy
Last synced: about 1 year ago
JSON representation
Tell your methods when to invoke by just naming them that way.
- Host: GitHub
- URL: https://github.com/ahmedelwerdany/invoke-before-after
- Owner: AhmedElwerdany
- License: mit
- Created: 2022-02-25T21:22:58.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-10-24T08:20:33.000Z (over 3 years ago)
- Last Synced: 2025-04-07T02:38:46.976Z (about 1 year ago)
- Topics: call, class, function, hooks, javascript, nodejs, npm, object, proxy
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/invoke-before-after
- Size: 989 KB
- Stars: 3
- Watchers: 1
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README



# invoke-before-after
Tell your methods when to invoke by just naming them that way.
## why?
- You want your methods to do only one thing.
- Small bundle size (1.4kB MINIFIED) - (648B MINIFIED + GZIPPED).
## Installing
Using npm:
```bash
npm install invoke-before-after
```
## Usage
```js
// CommonJS
const invokeWrapper = require('invoke-before-after')
// ES modules
import invokeWrapper from 'invoke-before-after'
```
## API
### invokeWrapper(target, [options])
Wraps a target (class/object) with a special proxy/wrapper and provides it back to you as a class/object. the method inside your object or class can invoke dynamically (without you calling them).
#### target :
A class or object that its methods should be proxied.
#### options :
- Type: `string`
- Default: `invokeAfter`
A prefix for methods' name that should invoke **after** the targeted method.
Assuming the targeted method is `getUsers`, and the **prefix** is `invokeAfter`.
Then a method called `invokeAfterGetUsers` will invoke automatically after `getUsers` get invoked.
- `invokeBeforeName`
- Type: `string`
- Default: `invokeBefore`
A prefix for methods' name that should invoke **before** the targeted method.
Assuming the targeted method is `getUsers`, and the **prefix** is `invokeBefore`.
Then a method called `invokeBeforeGetUsers` will invoke automatically before `getUsers` get invoked.
- Type: `boolean`
- Default: `false`
By default, the letter after the prefix should be uppercase.
Set this to `true` will add the name of the targeted method as it is after the prefix.
Assuming the targeted method is `getUsers`, and the **prefix** of `invokeAfterName` is `invokeAfter`.
Set `disableCamelCase` to true will make `invokeAftergetUsers` the right function to invoke after `getUsers` instead of `invokeAfterGetUsers`.
## Example
With classes :
```javascript
class User {
constructor(name) {
this.name = name;
this.updatedAt = new Date().toLocaleDateString()
}
updateName(name) {
this.name = name;
console.log('"name" updated')
}
// this method will be invoked after "updateName" method,
// since it has "invokeAfter" then the name of the method
// as the first letter of that method is uppercased
invokeAfterUpdateName(){
this.updatedAt = new Date().toLocaleDateString()
console.log('"updatedAt" updated')
}
}
const UserWrapper = invokeWrapper(User)
const newUser = new UserWrapper('Mark')
newUser.updateName()
// output :
// "name" updated
// "updatedAt" updated
```
With objects :
```javascript
const developer = invokeWrapper({
sleep: function () {
console.log('**sleeping**')
},
// this method will be invoked after "sleep" method,
// since it has "invokeBefore" then the name of the method
// as the first letter of that method is uppercased
invokeBeforeSleep: function() {
console.log('**yawning**')
}
})
developer.sleep()
// output :
// **yawning**
// **sleeping**
```
### with options
custom names :
```javascript
class User {
constructor(name) {
this.name = name
this.updatedAt = new Date().toLocaleDateString()
}
updateName(name) {
this.name = name
console.log('"name" Updated')
}
// should invoke before 'updateName' method,
// since we chose '$' for 'invokeBeforeName'
$UpdateName(name) {
console.log(`new name : ${name}`)
}
// should invoke after 'updateName' method,
// since we chose '_' for 'invokeAfterName'
_UpdateName(name) {
this.updatedAt = new Date().toLocaleDateString()
console.log('"updatedAt" updated')
}
}
const UserWrapper = invokeWrapper(User, {
invokeAfterName: '_',
invokeBeforeName: '$',
})
const dev = new UserWrapper('Peter')
dev.updateName('Mark')
// output:
// new name : Peter
// "name" Updated
// "updatedAt" updated
```
Disable camelCase :
```javascript
class User {
sayHi() {
console.log('saying hi')
}
// should invoke before 'sayHi' method,
// since we chose '_' for 'invokeAfterName'
// and disable the camelcase naming convention
_sayHi(){
console.log('I said hi :)')
}
}
User = invokeWrapper(User, {
invokeAfterName: '_',
disableCamelCase: true
})
const dev = new User()
dev.sayHi()
// output:
// I'm gonna say hi
// saying hi
// I said hi :)
```
## License
[MIT](LICENSE)
## Contributors ✨
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):

Ahmed Elwerdany
💻 🐛 🚧 👀 ⚠️

EnigmaDamn
📖

Hritik R
🤔
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!