Skip to content

Commit

Permalink
util: simplify isPrimitive
Browse files Browse the repository at this point in the history
Everything that is not an object should be a primitive.

PR-URL: #739

Reviewed-By: Trevor Norris <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
  • Loading branch information
vkurchatkin committed Feb 7, 2015
1 parent 2c3121c commit b9d3928
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
6 changes: 1 addition & 5 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -566,11 +566,7 @@ exports.isFunction = isFunction;

function isPrimitive(arg) {
return arg === null ||
typeof arg === 'boolean' ||
typeof arg === 'number' ||
typeof arg === 'string' ||
typeof arg === 'symbol' || // ES6 symbol
typeof arg === 'undefined';
typeof arg !== 'object' && typeof arg !== 'function';
}
exports.isPrimitive = isPrimitive;

Expand Down
19 changes: 19 additions & 0 deletions test/parallel/test-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,25 @@ assert.equal(true, util.isError(Object.create(Error.prototype)));
// isObject
assert.ok(util.isObject({}) === true);

// isPrimitive
assert.equal(false, util.isPrimitive({}));
assert.equal(false, util.isPrimitive(new Error));
assert.equal(false, util.isPrimitive(new Date()));
assert.equal(false, util.isPrimitive([]));
assert.equal(false, util.isPrimitive(/regexp/));
assert.equal(false, util.isPrimitive(function(){}));
assert.equal(false, util.isPrimitive(new Number(1)));
assert.equal(false, util.isPrimitive(new String('bla')));
assert.equal(false, util.isPrimitive(new Boolean(true)));
assert.equal(true, util.isPrimitive(1));
assert.equal(true, util.isPrimitive('bla'));
assert.equal(true, util.isPrimitive(true));
assert.equal(true, util.isPrimitive(undefined));
assert.equal(true, util.isPrimitive(null));
assert.equal(true, util.isPrimitive(Infinity));
assert.equal(true, util.isPrimitive(NaN));
assert.equal(true, util.isPrimitive(Symbol('symbol')));

// _extend
assert.deepEqual(util._extend({a:1}), {a:1});
assert.deepEqual(util._extend({a:1}, []), {a:1});
Expand Down

0 comments on commit b9d3928

Please sign in to comment.