From 9e7b1b59933cb5777152252b80b9fd5c0403d178 Mon Sep 17 00:00:00 2001 From: Nick Uraltsev Date: Tue, 12 Apr 2016 19:49:12 -0700 Subject: [PATCH] Add isFunction and isStream helpers --- lib/utils.js | 22 ++++++++++++++++++++++ test/specs/utils/isX.spec.js | 12 +++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/utils.js b/lib/utils.js index 0d418ae..33f2d4d 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -122,6 +122,26 @@ function isBlob(val) { return toString.call(val) === '[object Blob]'; } +/** + * Determine if a value is a Function + * + * @param {Object} val The value to test + * @returns {boolean} True if value is a Function, otherwise false + */ +function isFunction(val) { + return toString.call(val) === '[object Function]'; +} + +/** + * Determine if a value is a Stream + * + * @param {Object} val The value to test + * @returns {boolean} True if value is a Stream, otherwise false + */ +function isStream(val) { + return isObject(val) && isFunction(val.pipe); +} + /** * Trim excess whitespace off the beginning and end of a string * @@ -237,6 +257,8 @@ module.exports = { isDate: isDate, isFile: isFile, isBlob: isBlob, + isFunction: isFunction, + isStream: isStream, isStandardBrowserEnv: isStandardBrowserEnv, forEach: forEach, merge: merge, diff --git a/test/specs/utils/isX.spec.js b/test/specs/utils/isX.spec.js index 88a0f2b..d58d113 100644 --- a/test/specs/utils/isX.spec.js +++ b/test/specs/utils/isX.spec.js @@ -1,4 +1,5 @@ var utils = require('../../../lib/utils'); +var Stream = require('stream'); describe('utils::isX', function () { it('should validate Array', function () { @@ -67,5 +68,14 @@ describe('utils::isX', function () { expect(utils.isDate(new Date())).toEqual(true); expect(utils.isDate(Date.now())).toEqual(false); }); -}); + it('should validate Function', function () { + expect(utils.isFunction(function () {})).toEqual(true); + expect(utils.isFunction('function')).toEqual(false); + }); + + it('should validate Stream', function () { + expect(utils.isStream(new Stream.Readable())).toEqual(true); + expect(utils.isStream({ foo: 'bar' })).toEqual(false); + }); +});