diff --git a/index.js b/index.js index 5de4402..ea716c8 100644 --- a/index.js +++ b/index.js @@ -7,17 +7,15 @@ * MIT Licensed */ -var type = require('type-detect'); - /** * ### .hasProperty(object, name) * - * This allows checking whether an object has - * named property or numeric array index. + * This allows checking whether an object has own + * or inherited from prototype chain named property. * * Basically does the same thing as the `in` - * operator but works properly with natives - * and null/undefined values. + * operator but works properly with null/undefined values + * and other primitives. * * var obj = { * arr: ['a', 'b', 'c'] @@ -39,31 +37,20 @@ var type = require('type-detect'); * hasProperty(obj.arr, 3); // false * * @param {Object} object - * @param {String|Number} name + * @param {String|Symbol} name * @returns {Boolean} whether it exists * @namespace Utils * @name hasProperty * @api public */ -var literals = { - 'number': Number, - 'string': String, -}; function hasProperty(obj, name) { - var objType = type(obj); - // Bad Object, obviously no props at all - if (objType === 'null' || objType === 'undefined') { + if (typeof obj === 'undefined' || obj === null) { return false; } - // The `in` operator does not work with certain literals - // box these before the check - if (literals[objType] && typeof obj !== 'object') { - obj = new literals[objType](obj); - } - - return name in obj; + // The `in` operator does not work with primitives. + return name in Object(obj); } /* ! diff --git a/package.json b/package.json index 83c045d..4db4386 100644 --- a/package.json +++ b/package.json @@ -49,9 +49,6 @@ "max-statements": 0 } }, - "dependencies": { - "type-detect": "^2.0.1" - }, "devDependencies": { "browserify": "^13.0.0", "browserify-istanbul": "^1.0.0", diff --git a/test/index.js b/test/index.js index b8dea8e..c24f668 100644 --- a/test/index.js +++ b/test/index.js @@ -9,15 +9,23 @@ describe('hasProperty', function () { assert(pathval.hasProperty(arr, 3) === false); }); - it('should handle literal types', function () { + it('should handle primitives', function () { var exampleString = 'string literal'; assert(pathval.hasProperty(exampleString, 'length') === true); assert(pathval.hasProperty(exampleString, 3) === true); assert(pathval.hasProperty(exampleString, 14) === false); + assert(pathval.hasProperty(1, 'foo') === false); + assert(pathval.hasProperty(false, 'bar') === false); + assert(pathval.hasProperty(true, 'toString') === true); + + if (typeof Symbol === 'function') { + assert(pathval.hasProperty(Symbol(), 1) === false); + assert(pathval.hasProperty(Symbol.iterator, 'valueOf') === true); + } }); - it('should handle undefined', function () { + it('should handle objects', function () { var exampleObj = { foo: 'bar', };