Skip to content

Commit fb44606

Browse files
committed
fix(hasProperty): make it work with booleans and symbols
1 parent 7580b7d commit fb44606

File tree

3 files changed

+18
-26
lines changed

3 files changed

+18
-26
lines changed

index.js

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,15 @@
77
* MIT Licensed
88
*/
99

10-
var type = require('type-detect');
11-
1210
/**
1311
* ### .hasProperty(object, name)
1412
*
15-
* This allows checking whether an object has
16-
* named property or numeric array index.
13+
* This allows checking whether an object has own
14+
* or inherited from prototype chain named property.
1715
*
1816
* Basically does the same thing as the `in`
19-
* operator but works properly with natives
20-
* and null/undefined values.
17+
* operator but works properly with null/undefined values
18+
* and other primitives.
2119
*
2220
* var obj = {
2321
* arr: ['a', 'b', 'c']
@@ -39,31 +37,20 @@ var type = require('type-detect');
3937
* hasProperty(obj.arr, 3); // false
4038
*
4139
* @param {Object} object
42-
* @param {String|Number} name
40+
* @param {String|Symbol} name
4341
* @returns {Boolean} whether it exists
4442
* @namespace Utils
4543
* @name hasProperty
4644
* @api public
4745
*/
4846

49-
var literals = {
50-
'number': Number,
51-
'string': String,
52-
};
5347
function hasProperty(obj, name) {
54-
var objType = type(obj);
55-
// Bad Object, obviously no props at all
56-
if (objType === 'null' || objType === 'undefined') {
48+
if (typeof obj === 'undefined' || obj === null) {
5749
return false;
5850
}
5951

60-
// The `in` operator does not work with certain literals
61-
// box these before the check
62-
if (literals[objType] && typeof obj !== 'object') {
63-
obj = new literals[objType](obj);
64-
}
65-
66-
return name in obj;
52+
// The `in` operator does not work with primitives.
53+
return name in Object(obj);
6754
}
6855

6956
/* !

package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@
4949
"max-statements": 0
5050
}
5151
},
52-
"dependencies": {
53-
"type-detect": "^2.0.1"
54-
},
5552
"devDependencies": {
5653
"browserify": "^13.0.0",
5754
"browserify-istanbul": "^1.0.0",

test/index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,23 @@ describe('hasProperty', function () {
99
assert(pathval.hasProperty(arr, 3) === false);
1010
});
1111

12-
it('should handle literal types', function () {
12+
it('should handle primitives', function () {
1313
var exampleString = 'string literal';
1414
assert(pathval.hasProperty(exampleString, 'length') === true);
1515
assert(pathval.hasProperty(exampleString, 3) === true);
1616
assert(pathval.hasProperty(exampleString, 14) === false);
17+
1718
assert(pathval.hasProperty(1, 'foo') === false);
19+
assert(pathval.hasProperty(false, 'bar') === false);
20+
assert(pathval.hasProperty(true, 'toString') === true);
21+
22+
if (typeof Symbol === 'function') {
23+
assert(pathval.hasProperty(Symbol(), 1) === false);
24+
assert(pathval.hasProperty(Symbol.iterator, 'valueOf') === true);
25+
}
1826
});
1927

20-
it('should handle undefined', function () {
28+
it('should handle objects', function () {
2129
var exampleObj = {
2230
foo: 'bar',
2331
};

0 commit comments

Comments
 (0)