npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

invoke-before-after

v1.3.9

Published

Tell your methods when to invoke by just naming them that way

Downloads

60

Readme

Codecov Coverage

All Contributors

npm GitHub Repo stars

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:

npm install invoke-before-after

Usage

// CommonJS
const {invokeMeWrapper} = require('invoke-before-after')

// ES modules
import {invokeMeWrapper} from 'invoke-before-after'

API

invokeMeWrapper(target, [options])

wraps a target (class/object) with a special proxy/wrapper and provid 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 :

  • invokeAfterName

    • 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.

  • disableCamelCase

    • 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 :

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 = invokeMeWrapper(User)
const newUser = new UserWrapper('Mark')

newUser.updateName()
// output : 
// "name" updated
// "updatedAt" updated

With objects :

const developer = invokeMeWrapper({
    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 :

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 = invokeMeWrapper(User, {
    invokeAfterName: '_',
    invokeBeforeName: '$',
})

const dev = new UserWrapper('Peter')
dev.updateName('Mark')

// output: 
// new name : Peter
// "name" Updated
// "updatedAt" updated

Disable camelCase :


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 = invokeMeWrapper(User, {
   invokeAfterName: '_',
   disableCamelCase: true
})

const dev = new User()
dev.sayHi()

// output: 
// I'm gonna say hi
// saying hi
// I said hi :)

License

MIT

Contributors ✨

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind welcome!