index.js 976 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. 'use strict';
  2. const lazy = (mod, fn, id) => mod === undefined ? fn(id) : mod;
  3. module.exports = fn => {
  4. return id => {
  5. let mod;
  6. return function () {
  7. if (arguments.length === 0) {
  8. mod = lazy(mod, fn, id);
  9. return mod;
  10. }
  11. const ret = {};
  12. [].forEach.call(arguments, prop => {
  13. Object.defineProperty(ret, prop, {
  14. get: () => {
  15. mod = lazy(mod, fn, id);
  16. if (typeof mod[prop] === 'function') {
  17. return function () {
  18. return mod[prop].apply(mod, arguments);
  19. };
  20. }
  21. return mod[prop];
  22. }
  23. });
  24. });
  25. return ret;
  26. };
  27. };
  28. };
  29. module.exports.proxy = fn => {
  30. return id => {
  31. let mod;
  32. const handler = {
  33. get: (target, property) => {
  34. mod = lazy(mod, fn, id);
  35. return Reflect.get(mod, property);
  36. },
  37. apply: (target, thisArg, argumentsList) => {
  38. mod = lazy(mod, fn, id);
  39. return Reflect.apply(mod, thisArg, argumentsList);
  40. }
  41. };
  42. return new Proxy(() => {}, handler);
  43. };
  44. };