order-by-first-call.js 817 B

123456789101112131415161718192021222324252627282930313233343536
  1. "use strict";
  2. var sort = require("./prototypes/array").sort;
  3. var slice = require("./prototypes/array").slice;
  4. /**
  5. * @private
  6. */
  7. function comparator(a, b) {
  8. // uuid, won't ever be equal
  9. var aCall = a.getCall(0);
  10. var bCall = b.getCall(0);
  11. var aId = (aCall && aCall.callId) || -1;
  12. var bId = (bCall && bCall.callId) || -1;
  13. return aId < bId ? -1 : 1;
  14. }
  15. /**
  16. * A Sinon proxy object (fake, spy, stub)
  17. *
  18. * @typedef {object} SinonProxy
  19. * @property {Function} getCall - A method that can return the first call
  20. */
  21. /**
  22. * Sorts an array of SinonProxy instances (fake, spy, stub) by their first call
  23. *
  24. * @param {SinonProxy[] | SinonProxy} spies
  25. * @returns {SinonProxy[]}
  26. */
  27. function orderByFirstCall(spies) {
  28. return sort(slice(spies), comparator);
  29. }
  30. module.exports = orderByFirstCall;