Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider isInstanceOf() based on string tags? #36

Closed
jcbhmr opened this issue May 26, 2023 · 1 comment · Fixed by #38
Closed

Consider isInstanceOf() based on string tags? #36

jcbhmr opened this issue May 26, 2023 · 1 comment · Fixed by #38

Comments

@jcbhmr
Copy link
Collaborator

jcbhmr commented May 26, 2023

A reasonably popular npm package is https://github.com/lamansky/is-instance-of (1k downloads last week), but it's severely outdated. I think this package would be a great place for such a function? Or maybe there already is one that does what I'm looking for and I couldn't find it?

Example use case:

// Covers ServiceWorkerGlobalScope, DedicatedWorkerGlobalScope,
// plain WorkerGlobalScope from web-worker polyfill, SharedWorkerGlobalScope, etc.
if (isInstanceOf(globalThis, "WorkerGlobalScope")) {
  onmessage = (e) => console.log(e.data);
  doThingInWorker(self);
}
doNormalThing();

vs the old way

for (let p = globalThis; p; p = Object.getPrototypeOf(p)) {
  if (Object.prototype.toString.call(p).slice(8, -1) === "WorkerGlobalScope") {
    onmessage = (e) => console.log(e.data);
    doThingInWorker(self);
    break;
  }
}
doNormalThing();
@jcbhmr jcbhmr changed the title Consider isInstanceOf()? Consider isInstanceOf() based on string tags? May 26, 2023
@mesqueeb
Copy link
Owner

mesqueeb commented May 26, 2023

@jcbhmr since is-what is fully tree-shakable, there's no downside on adding new functions! I feel this also is a great addition! : )
PRs welcome

PS: is-what is currently at 4.5 million / week : D 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants