Skip to content

Commit b0ed45b

Browse files
author
C J Silverio
committed
util: fixed behavior of isObject()
This fixes issue nodejs#743. Brought the behavior of util.isObject() in line with expected behavior, inspired by utility libraries like underscore & lodash. In particular, it now returns true for functions, as expected. Added eight tests for isObject() to ensure it behaves as expected for a wide variety of input, including cases where it is expected to return false.
1 parent 982b143 commit b0ed45b

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

lib/util.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,8 @@ function isRegExp(re) {
543543
exports.isRegExp = isRegExp;
544544

545545
function isObject(arg) {
546-
return arg !== null && typeof arg === 'object';
546+
var type = typeof arg;
547+
return type === 'function' || type === 'object' && !!arg;
547548
}
548549
exports.isObject = isObject;
549550

test/parallel/test-util.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ assert.equal(true, util.isError(Object.create(Error.prototype)));
5050

5151
// isObject
5252
assert.ok(util.isObject({}) === true);
53+
assert.ok(util.isObject(function() {}) === true);
54+
assert.ok(util.isObject([1, 2, 3]) === true);
55+
assert.ok(util.isObject(new String('string')) === true);
56+
assert.ok(util.isObject(null) === false);
57+
assert.ok(util.isObject(undefined) === false);
58+
assert.ok(util.isObject('string') === false);
59+
assert.ok(util.isObject(42) === false);
60+
assert.ok(util.isObject(true) === false);
5361

5462
// isPrimitive
5563
assert.equal(false, util.isPrimitive({}));

0 commit comments

Comments
 (0)