From 971e507a2559b8b45eb042b549a1bdcca1915926 Mon Sep 17 00:00:00 2001 From: tunnckoCore Date: Sun, 11 Dec 2016 06:55:54 +0200 Subject: [PATCH] feat(promise): add promise kind and tests --- browser.js | 31 +++++++++++++++++++------------ index.js | 3 +++ test/es6/index.js | 11 +++++++++++ 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/browser.js b/browser.js index 76395ea..32414fd 100644 --- a/browser.js +++ b/browser.js @@ -60,6 +60,9 @@ module.exports = function kindOf(val) { if (type === '[object Error]') { return 'error'; } + if (type === '[object Promise]') { + return 'promise'; + } // buffer if (typeof Buffer !== 'undefined' && isBuffer(val)) { @@ -117,22 +120,26 @@ module.exports = function kindOf(val) { }; },{"is-buffer":2}],2:[function(require,module,exports){ -/** - * Determine if an object is Buffer +/*! + * Determine if an object is a Buffer * - * Author: Feross Aboukhadijeh - * License: MIT - * - * `npm install is-buffer` + * @author Feross Aboukhadijeh + * @license MIT */ +// The _isBuffer check is for Safari 5-7 support, because it's missing +// Object.prototype.constructor. Remove this eventually module.exports = function (obj) { - return !!(obj != null && - (obj._isBuffer || // For Safari 5-7 (missing Object.prototype.constructor) - (obj.constructor && - typeof obj.constructor.isBuffer === 'function' && - obj.constructor.isBuffer(obj)) - )) + return obj != null && (isBuffer(obj) || isSlowBuffer(obj) || !!obj._isBuffer) +} + +function isBuffer (obj) { + return !!obj.constructor && typeof obj.constructor.isBuffer === 'function' && obj.constructor.isBuffer(obj) +} + +// For Node v0.10 support. Remove this eventually. +function isSlowBuffer (obj) { + return typeof obj.readFloatLE === 'function' && typeof obj.slice === 'function' && isBuffer(obj.slice(0, 0)) } },{}]},{},[1])(1) diff --git a/index.js b/index.js index 8508386..31d9650 100644 --- a/index.js +++ b/index.js @@ -59,6 +59,9 @@ module.exports = function kindOf(val) { if (type === '[object Error]') { return 'error'; } + if (type === '[object Promise]') { + return 'promise'; + } // buffer if (typeof Buffer !== 'undefined' && isBuffer(val)) { diff --git a/test/es6/index.js b/test/es6/index.js index 96bc93c..b074eec 100644 --- a/test/es6/index.js +++ b/test/es6/index.js @@ -6,6 +6,17 @@ var kindOf = require('../..'); module.exports = function() { describe('es6 features', function() { + it('should work for resolved promises', function() { + var promise = Promise.resolve(123); + assert.strictEqual(kindOf(promise), 'promise'); + }); + + it('should work for rejected promises', function() { + var promise = Promise.reject(new Error('foo bar')); + promise.catch(function() {}) + assert.strictEqual(kindOf(promise), 'promise'); + }); + it('should work for generators', function() { var gen = function * named() {return true;}; assert.equal(kindOf(gen), 'function');