Coder Social home page Coder Social logo

ahmedelwerdany / invoke-before-after Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 2.0 1013 KB

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

Home Page: https://www.npmjs.com/package/invoke-before-after

License: MIT License

JavaScript 2.69% TypeScript 97.31%
call class function hooks javascript nodejs npm object proxy

invoke-before-after's Introduction

invoke-before-after's People

Contributors

ahmedelwerdany avatar dependabot[bot] avatar sujalhansda avatar

Stargazers

 avatar  avatar

Watchers

 avatar

invoke-before-after's Issues

Add the possibility to disable the Camel case naming convention

description

the letter after invokeBefore or invokeAfter is now upper case for the naming convention,
but since we added a possibility to change the name, in many cases the upper case letter would look wrong.

and we should add possibility to disable that

example

before

class User {
   sayHi() {}
   // we should write the first letter after "invokeBeforeName" as an upper case letter.
   _SayHi(){}
}

User = invokeMeWrapper(User, {
   invokeBeforeName: '_'
})

after

class User {
   sayHi() {}
   // after we disabled it, we can write the same name as the original method
   _sayHi(){}
}

User = invokeMeWrapper(User, {
   invokeBeforeName: '_',
   disableCamelCase: true
})

add invokeAfter & invokeBefore to invoke before and after every method

description:

it should allow developers to specify some functionally to run every time a method get called

example:

class User {
    invokeAfter() {
         console.log('I will be here every time, after any method get called')
    }

    invokeBefore() {
         console.log('I will be here every time, before any method get called')
    }
   
    sayHi() {
       console.log('hi')
    }

    sayGoodBye() {
       console.log('goodbye')
    }
}

const WrapperUser = invokeMeWrapper(User)
const dev = new WrapperUser()

dev.sayHi()
dev.sayGoodBye()

// I will be here every time, before any method get called
// hi
// I will be here every time, after any method get called
// I will be here every time, before any method get called
// goodbye
// I will be here every time, after any method get called

Add the possibility to specify a custom name instead of invokeAfter or InvokeBefore

it would be useful to give the user the ability to choose which name to use.

invokeMeWrapper(User, {
    invokeAfterName: '_',
    invokeBeforeName: '$',
})

assuming the class would look like this

class User {
    sayHi() {
         console.log('saying hi')
    }

   _SayHi() {
      // should invoke after 'sayHi' method, since we choose '_' for 'invokeAfter' 
   }

   $SayHi() {
      // should invoke before 'sayHi' method, since we choose '$' for 'invokeBefore' 
   }
}

Refactor test files

description

(1) - index.test.ts file has test cases when it tests if a method is invoked or not by using js.fn, but it's better to convert it to jest.spyOn because this the right usage of it. and it will be good in (2)
(2) - use describe.each since all tests are almost the same

add callback in `invokeBefore` to stop the function.

Is your feature request related to a problem? Please describe.
A good usage of invokeBefore is validations, but it's hard to do it, since I can't stop the functions from running by invoking a function or returning a specific value

Describe the solution you'd like
A callback passed to invokeBefore, that when invoked it stops the function from going to the original method.

invokeBeforeCreateUser(name, next) {
  if(!name) {
     // this should throw an error, stops the hook from going to the target method. 
     next(new Error('please enter your name'))
  }
  
  // this should pass the control to the target method
  next()
}

Describe alternatives you've considered
None
Additional context
None

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.