index.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // --------------------
  2. // is-bluebird module
  3. // --------------------
  4. // exports
  5. /**
  6. * Identifies whether input is a bluebird promise.
  7. * @param {*} promise - Input to be tested
  8. * @returns {boolean} - true if is a bluebird promise, false if not
  9. */
  10. var isBluebird = function(promise) {
  11. return isObject(promise) && isBluebird.ctor(promise.constructor);
  12. };
  13. /**
  14. * Identifies whether input is a bluebird promise constructor.
  15. * @param {*} Promise - Input to be tested
  16. * @returns {boolean} - true if is bluebird promise constructor, false if not
  17. */
  18. isBluebird.ctor = function(Promise) {
  19. return typeof Promise == 'function' && !!Promise.prototype && typeof Promise.prototype._addCallbacks == 'function';
  20. };
  21. /**
  22. * Identifies whether input is a bluebird v2 promise.
  23. * @param {*} promise - Input to be tested
  24. * @returns {boolean} - true if is a bluebird v2 promise, false if not
  25. */
  26. isBluebird.v2 = function(promise) {
  27. return isObject(promise) && isBluebird.v2.ctor(promise.constructor);
  28. };
  29. /**
  30. * Identifies whether input is bluebird v2 promise constructor.
  31. * @alias isBluebird.ctor.v2
  32. *
  33. * @param {*} promise - Input to be tested
  34. * @returns {boolean} - true if is a bluebird v2 promise, false if not
  35. */
  36. isBluebird.v2.ctor = function(Promise) {
  37. return isBluebird.ctor(Promise) && Promise.prototype._addCallbacks.length == 6;
  38. };
  39. isBluebird.ctor.v2 = isBluebird.v2.ctor;
  40. /**
  41. * Identifies whether input is a bluebird v3 promise.
  42. * @param {*} promise - Input to be tested
  43. * @returns {boolean} - true if is a bluebird v3 promise, false if not
  44. */
  45. isBluebird.v3 = function(promise) {
  46. return isObject(promise) && isBluebird.v3.ctor(promise.constructor);
  47. };
  48. /**
  49. * Identifies whether input is bluebird v3 promise constructor.
  50. * @alias isBluebird.ctor.v3
  51. *
  52. * @param {*} promise - Input to be tested
  53. * @returns {boolean} - true if is a bluebird v3 promise, false if not
  54. */
  55. isBluebird.v3.ctor = function(Promise) {
  56. return isBluebird.ctor(Promise) && Promise.prototype._addCallbacks.length == 5;
  57. };
  58. isBluebird.ctor.v3 = isBluebird.v3.ctor;
  59. /**
  60. * Check if input is an object.
  61. * @param {*} obj - Input to be tested
  62. * @returns {boolean} - true if is an object, false if not
  63. */
  64. function isObject(obj) {
  65. return !!obj && typeof obj == 'object';
  66. }
  67. // export isBluebird
  68. module.exports = isBluebird;