Browse Source

util: simplify `isPrimitive`

Everything that is not an object should be a primitive.

PR-URL: https://github.com/iojs/io.js/pull/739

Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
v1.8.0-commit
Vladimir Kurchatkin 10 years ago
parent
commit
b9d3928f80
  1. 6
      lib/util.js
  2. 19
      test/parallel/test-util.js

6
lib/util.js

@ -566,11 +566,7 @@ exports.isFunction = isFunction;
function isPrimitive(arg) { function isPrimitive(arg) {
return arg === null || return arg === null ||
typeof arg === 'boolean' || typeof arg !== 'object' && typeof arg !== 'function';
typeof arg === 'number' ||
typeof arg === 'string' ||
typeof arg === 'symbol' || // ES6 symbol
typeof arg === 'undefined';
} }
exports.isPrimitive = isPrimitive; exports.isPrimitive = isPrimitive;

19
test/parallel/test-util.js

@ -51,6 +51,25 @@ assert.equal(true, util.isError(Object.create(Error.prototype)));
// isObject // isObject
assert.ok(util.isObject({}) === true); 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 // _extend
assert.deepEqual(util._extend({a:1}), {a:1}); assert.deepEqual(util._extend({a:1}), {a:1});
assert.deepEqual(util._extend({a:1}, []), {a:1}); assert.deepEqual(util._extend({a:1}, []), {a:1});

Loading…
Cancel
Save