Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Monster
Manage
Activity
Members
Plan
Jira
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OSS
Libraries
Javascript
Monster
Commits
c8c8ce0b
Verified
Commit
c8c8ce0b
authored
5 months ago
by
Volker Schukai
Browse files
Options
Downloads
Patches
Plain Diff
fix: optimize instanceof operator
parent
072af45d
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
source/types/is.mjs
+9
-1
9 additions, 1 deletion
source/types/is.mjs
test/cases/types/is.mjs
+59
-1
59 additions, 1 deletion
test/cases/types/is.mjs
with
68 additions
and
2 deletions
source/types/is.mjs
+
9
−
1
View file @
c8c8ce0b
...
...
@@ -173,7 +173,15 @@ function isInstance(value, instance) {
if
(
!
isObject
(
value
))
return
false
;
if
(
!
isFunction
(
instance
))
return
false
;
if
(
!
instance
.
hasOwnProperty
(
"
prototype
"
))
return
false
;
return
value
instanceof
instance
?
true
:
false
;
if
(
value
instanceof
instance
)
return
true
;
let
proto
=
Object
.
getPrototypeOf
(
value
);
while
(
proto
!=
null
)
{
if
(
proto
===
instance
.
prototype
)
return
true
;
proto
=
Object
.
getPrototypeOf
(
proto
);
}
return
false
;
}
/**
...
...
This diff is collapsed.
Click to expand it.
test/cases/types/is.mjs
+
59
−
1
View file @
c8c8ce0b
...
...
@@ -238,7 +238,65 @@ describe('Is', function () {
});
});
});
describe
(
'
.isInstanceExtended()
'
,
function
()
{
class
Base
{}
class
Derived
extends
Base
{}
function
AnotherClass
()
{}
let
baseInstance
=
new
Base
();
let
derivedInstance
=
new
Derived
();
// Test cases
const
cases
=
[
[()
=>
{},
undefined
,
false
,
"
function vs undefined
"
],
[
new
ID
(),
()
=>
{},
false
,
"
ID instance vs function
"
],
[
new
ID
(),
ID
,
true
,
"
ID instance vs ID
"
],
[
'
test1
'
,
undefined
,
false
,
"
string vs undefined
"
],
[
undefined
,
undefined
,
false
,
"
undefined vs undefined
"
],
[
null
,
undefined
,
false
,
"
null vs undefined
"
],
[
2
,
undefined
,
false
,
"
number vs undefined
"
],
[
false
,
undefined
,
false
,
"
false vs undefined
"
],
[
parseInt
(
"
a
"
),
undefined
,
false
,
"
NaN vs undefined
"
],
[
true
,
undefined
,
false
,
"
true vs undefined
"
],
[
4.5
,
undefined
,
false
,
"
float vs undefined
"
],
[{},
undefined
,
false
,
"
object vs undefined
"
],
[[
1
,
2
,
3
],
undefined
,
false
,
"
array vs undefined
"
],
[
Symbol
(
"
foo
"
),
undefined
,
false
,
"
symbol vs undefined
"
],
[
baseInstance
,
Base
,
true
,
"
Base instance vs Base
"
],
[
derivedInstance
,
Base
,
true
,
"
Derived instance vs Base
"
],
[
derivedInstance
,
Derived
,
true
,
"
Derived instance vs Derived
"
],
[
baseInstance
,
Derived
,
false
,
"
Base instance vs Derived
"
],
[
baseInstance
,
AnotherClass
,
false
,
"
Base instance vs AnotherClass
"
],
[
derivedInstance
,
()
=>
{},
false
,
"
Derived instance vs function
"
],
[
new
AnotherClass
(),
AnotherClass
,
true
,
"
AnotherClass instance vs AnotherClass
"
],
];
// Adding prototype modification test
let
protoModifiedInstance
=
new
Base
();
Object
.
setPrototypeOf
(
protoModifiedInstance
,
Derived
.
prototype
);
cases
.
push
(
[
protoModifiedInstance
,
Base
,
true
,
"
Proto modified Base instance vs Base
"
],
[
protoModifiedInstance
,
Derived
,
true
,
"
Proto modified Base instance vs Derived
"
]
);
// Running the tests
cases
.
forEach
(
function
(
data
)
{
const
a
=
data
.
shift
();
const
b
=
data
.
shift
();
const
c
=
data
.
shift
();
const
d
=
data
.
shift
();
it
(
'
isInstance(
'
+
JSON
.
stringify
(
a
)
+
'
, [Function]) should return
'
+
c
,
function
()
{
if
(
isInstance
(
a
,
b
)
!==
c
)
{
console
.
log
(
d
)
}
expect
(
isInstance
(
a
,
b
)).
to
.
equal
(
c
);
});
});
});
describe
(
'
.isObject()
'
,
function
()
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment