decorate.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. 'use strict';
  2. var some = require('core-js/library/fn/array/some');
  3. var map = require('core-js/library/fn/array/map');
  4. function decorate (callSpec, decorator) {
  5. var numArgsToCapture = callSpec.numArgsToCapture;
  6. return function decoratedAssert () {
  7. var context, message, hasMessage = false;
  8. // see: https://github.com/twada/empower-core/pull/8#issue-127859465
  9. // see: https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#32-leaking-arguments
  10. var args = new Array(arguments.length);
  11. for(var i = 0; i < args.length; ++i) {
  12. args[i] = arguments[i];
  13. }
  14. if (numArgsToCapture === (args.length - 1)) {
  15. message = args.pop();
  16. hasMessage = true;
  17. }
  18. var invocation = {
  19. thisObj: this,
  20. values: args,
  21. message: message,
  22. hasMessage: hasMessage
  23. };
  24. if (some(args, isCaptured)) {
  25. invocation.values = map(args.slice(0, numArgsToCapture), function (arg) {
  26. if (isNotCaptured(arg)) {
  27. return arg;
  28. }
  29. if (!context) {
  30. context = {
  31. source: arg.source,
  32. args: []
  33. };
  34. }
  35. context.args.push({
  36. value: arg.powerAssertContext.value,
  37. events: arg.powerAssertContext.events
  38. });
  39. return arg.powerAssertContext.value;
  40. });
  41. return decorator.concreteAssert(callSpec, invocation, context);
  42. } else {
  43. return decorator.concreteAssert(callSpec, invocation);
  44. }
  45. };
  46. }
  47. function isNotCaptured (value) {
  48. return !isCaptured(value);
  49. }
  50. function isCaptured (value) {
  51. return (typeof value === 'object') &&
  52. (value !== null) &&
  53. (typeof value.powerAssertContext !== 'undefined');
  54. }
  55. module.exports = decorate;