Skip to content

Commit dfaac0e

Browse files
committed
feat: add support for Symbol.hasInstance
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 1040808 commit dfaac0e

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

lib/node_modules/@stdlib/assert/instance-of/lib/main.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,16 @@
2020

2121
// MODULES //
2222

23+
var hasHasInstanceSymbolSupport = require( '@stdlib/assert/has-has-instance-symbol-support' ); // eslint-disable-line id-length
24+
var HasInstanceSymbol = require( '@stdlib/symbol/has-instance' );
2325
var format = require( '@stdlib/string/format' );
2426

2527

28+
// VARIABLES //
29+
30+
var hasSupport = hasHasInstanceSymbolSupport();
31+
32+
2633
// MAIN //
2734

2835
/**
@@ -55,7 +62,14 @@ var format = require( '@stdlib/string/format' );
5562
*/
5663
function instanceOf( value, constructor ) {
5764
// TODO: replace with `isCallable` check
58-
if ( typeof constructor !== 'function' ) {
65+
if (
66+
typeof constructor !== 'function' &&
67+
!(
68+
hasSupport &&
69+
typeof constructor === 'object' &&
70+
typeof constructor[ HasInstanceSymbol ] === 'function'
71+
)
72+
) {
5973
throw new TypeError( format( 'invalid argument. Second argument must be callable. Value: `%s`.', constructor ) );
6074
}
6175
return ( value instanceof constructor );

lib/node_modules/@stdlib/assert/instance-of/test/test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
// MODULES //
2222

2323
var tape = require( 'tape' );
24+
var isArray = require( '@stdlib/assert/is-array' );
25+
var hasHasInstanceSymbolSupport = require( '@stdlib/assert/has-has-instance-symbol-support' ); // eslint-disable-line id-length
26+
var HasInstanceSymbol = require( '@stdlib/symbol/has-instance' );
2427
var inherit = require( '@stdlib/utils/inherit' );
2528
var Boolean = require( '@stdlib/boolean/ctor' );
2629
var Function = require( '@stdlib/function/ctor' );
@@ -29,6 +32,13 @@ var Object = require( '@stdlib/object/ctor' );
2932
var instanceOf = require( './../lib' );
3033

3134

35+
// VARIABLES //
36+
37+
var opts = {
38+
'skip': !hasHasInstanceSymbolSupport()
39+
};
40+
41+
3242
// TESTS //
3343

3444
tape( 'main export is a function', function test( t ) {
@@ -176,3 +186,19 @@ tape( 'the function returns `false` if provided primitives and their correspondi
176186

177187
t.end();
178188
});
189+
190+
tape( 'the function supports ES2015+ environments supporting `Symbol.hasInstance`', opts, function test( t ) {
191+
var bool;
192+
var obj;
193+
var x;
194+
195+
x = [ 1, 2, 3 ];
196+
197+
obj = {};
198+
obj[ HasInstanceSymbol ] = isArray;
199+
200+
bool = instanceOf( x, obj );
201+
t.strictEqual( bool, true, 'returns expected value' );
202+
203+
t.end();
204+
});

0 commit comments

Comments
 (0)