2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-20 20:00:40 +03:00

Add isFunction and isStream helpers

This commit is contained in:
Nick Uraltsev
2016-04-12 19:49:12 -07:00
parent 4de17bca09
commit 9e7b1b5993
2 changed files with 33 additions and 1 deletions
+22
View File
@@ -122,6 +122,26 @@ function isBlob(val) {
return toString.call(val) === '[object Blob]'; 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 * Trim excess whitespace off the beginning and end of a string
* *
@@ -237,6 +257,8 @@ module.exports = {
isDate: isDate, isDate: isDate,
isFile: isFile, isFile: isFile,
isBlob: isBlob, isBlob: isBlob,
isFunction: isFunction,
isStream: isStream,
isStandardBrowserEnv: isStandardBrowserEnv, isStandardBrowserEnv: isStandardBrowserEnv,
forEach: forEach, forEach: forEach,
merge: merge, merge: merge,
+11 -1
View File
@@ -1,4 +1,5 @@
var utils = require('../../../lib/utils'); var utils = require('../../../lib/utils');
var Stream = require('stream');
describe('utils::isX', function () { describe('utils::isX', function () {
it('should validate Array', function () { it('should validate Array', function () {
@@ -67,5 +68,14 @@ describe('utils::isX', function () {
expect(utils.isDate(new Date())).toEqual(true); expect(utils.isDate(new Date())).toEqual(true);
expect(utils.isDate(Date.now())).toEqual(false); 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);
});
});