Skip to content

Commit

Permalink
fix(hasProperty): make it work with booleans and symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
shvaikalesh committed Oct 5, 2016
1 parent 7580b7d commit fb44606
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 26 deletions.
29 changes: 8 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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);
}

/* !
Expand Down
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@
"max-statements": 0
}
},
"dependencies": {
"type-detect": "^2.0.1"
},
"devDependencies": {
"browserify": "^13.0.0",
"browserify-istanbul": "^1.0.0",
Expand Down
12 changes: 10 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
};
Expand Down

0 comments on commit fb44606

Please sign in to comment.