mesqueeb / is-what

JS type check (TypeScript supported) functions like `isPlainObject() isArray()` etc. A simple & small integration.
https://mesqueeb.github.io/is-what/
MIT License
179 stars 19 forks source link

Consider isInstanceOf() based on string tags? #36

Closed jcbhmr closed 1 year ago

jcbhmr commented 1 year ago

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();
mesqueeb commented 1 year ago

@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 🎉