项目作者: gamtiq

项目描述 :
Functions to wrap other functions and fields/methods and to change/enhance their behavior, functionality or usage
高级语言: TypeScript
项目地址: git://github.com/gamtiq/wrapme.git
创建时间: 2020-02-26T20:37:53Z
项目社区:https://github.com/gamtiq/wrapme

开源协议:MIT License

下载


" class="reference-link">wrapme

NPM version

Functions to wrap other functions and fields/methods and to change/enhance their behavior, functionality or usage.
Can be used for Aspect-oriented programming.

Features

  • Wrap a single function/field/method (by wrap) or several fields and methods at once (by intercept).
  • Wrap only field’s get operation (get option) or set operation (set option), or both (by default).
  • Provide special getter and/or setter for wrapped field if it is necessary.
  • Call original function/method or field’s operation before (use before or listen option),
    after (use after option) and/or inside handler (use run() or runApply()).
  • Totally control calling of original function/method or field’s operation inside handler:
    call depending on condition, filter/validate/convert passed arguments and/or provide another arguments.
  • Return result of original function/method or field’s operation, or any other value from handler.
  • Save necessary data between handler calls.
  • Restore original fields/methods when it is needed.
  • Does not have dependencies and can be used in ECMAScript 5+ environment.
  • Small size.
  1. import { intercept } from 'wrapme';
  2. const api = {
  3. sum(...numList) {
  4. let result = 0;
  5. for (let value of numList) {
  6. result += value;
  7. }
  8. return result;
  9. },
  10. // Other methods
  11. // ...
  12. };
  13. // Logging
  14. const log = [];
  15. function logger(callData) {
  16. log.push({
  17. name: callData.field,
  18. args: callData.arg,
  19. result: callData.result,
  20. callNum: callData.number,
  21. time: new Date().getTime()
  22. });
  23. }
  24. const unwrap = intercept(api, 'sum', logger, {listen: true});
  25. api.sum(1, 2, 3, 4); // Returns 10, adds item to log
  26. api.sum(1, -1, 2, -2, 3); // Returns 3, adds item to log
  27. // Restore original method
  28. unwrap();

See more examples below.

Table of contents

Installation

Node

  1. npm install wrapme

AMD,