Skip to content

Commit dbfef8f

Browse files
committed
[Fix] do not be tricked by fake Booleans
Fixes #25
1 parent b19953f commit dbfef8f

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ module.exports = function isBoolean(value) {
2424
if (value === null || typeof value !== 'object') {
2525
return false;
2626
}
27-
return hasToStringTag && Symbol.toStringTag in value ? tryBooleanObject(value) : $toString(value) === boolClass;
27+
return hasToStringTag ? tryBooleanObject(value) : $toString(value) === boolClass;
2828
};

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"@ljharb/eslint-config": "^21.1.1",
4848
"@ljharb/tsconfig": "^0.2.3",
4949
"@types/core-js": "^2.5.8",
50+
"@types/object-inspect": "^1.13.0",
5051
"@types/tape": "^5.8.1",
5152
"auto-changelog": "^2.5.0",
5253
"core-js": "^3.40.0",
@@ -56,6 +57,7 @@
5657
"in-publish": "^2.0.1",
5758
"npmignore": "^0.3.1",
5859
"nyc": "^10.3.2",
60+
"object-inspect": "^1.13.4",
5961
"safe-publish-latest": "^2.0.0",
6062
"tape": "^5.9.0",
6163
"typescript": "next"

test/index.js

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
'use strict';
22

33
var test = require('tape');
4-
var isBoolean = require('../');
54
var hasToStringTag = require('has-tostringtag/shams')();
5+
var inspect = require('object-inspect');
6+
7+
var isBoolean = require('../');
68

79
test('not Booleans', function (t) {
810
t.test('primitives', function (st) {
@@ -48,3 +50,24 @@ test('Booleans', function (t) {
4850
t.ok(isBoolean(Object(false)), 'Object(false) is Boolean');
4951
t.end();
5052
});
53+
54+
test('Proxy', { skip: typeof Proxy !== 'function' || !hasToStringTag }, function (t) {
55+
/** @type {Record<PropertyKey, unknown>} */
56+
var target = {};
57+
target[Symbol.toStringTag] = 'Boolean';
58+
var fake = new Proxy(target, { has: function () { return false; } });
59+
60+
t.equal(
61+
isBoolean(target),
62+
false,
63+
inspect(target) + ' is not a Boolean'
64+
);
65+
66+
t.equal(
67+
isBoolean(fake),
68+
false,
69+
inspect(fake) + ' is not a Boolean'
70+
);
71+
72+
t.end();
73+
});

0 commit comments

Comments
 (0)