Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. export function validateArgs(constructor: Function) {
  2. // copy the methods so that we avoid self referencing.
  3. const original: Function = Object.assign({}, constructor);
  4. // iterate over all the static methods in the class
  5. for (const i in constructor) {
  6. // wrap them in a method that throws an error if args are not valid
  7. constructor[i] = function(...args: any[]) {
  8. if(areArgsValid(args)) {
  9. return original[i].apply(this, args);
  10. }
  11. throw `Args aren't valid`
  12. };
  13. }
  14. }
  15.  
  16. @validateArgs
  17. class Validator {
  18. static myThingIsValid(arg1) {
  19. return !!arg1;
  20. }
  21. myOtherThingIsValid(arg1) {
  22. return !!arg1;
  23. }
  24. }
  25.  
  26. for (const i in constructor) { ... }
  27.  
  28. Object.getOwnPropertyNames(constructor)
  29. .filter(prop => typeof prop === 'function')
  30. .forEach(val => ...)
  31.  
  32. [
  33. ...Object.getOwnPropertyNames(constructor),
  34. ...Object.getOwnPropertyNames(constructor.prototype)
  35. ]
  36. .filter(prop => typeof prop === 'function')
  37. .forEach(val => ...)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement