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