Skip to content

Instantly share code, notes, and snippets.

@KooiInc
Last active February 11, 2023 08:03
Show Gist options
  • Save KooiInc/2d58266e590052ab9d2042a79ffebb7c to your computer and use it in GitHub Desktop.
Save KooiInc/2d58266e590052ab9d2042a79ffebb7c to your computer and use it in GitHub Desktop.
Check all js type
const ISOneOf = (obj, ...params) => !!params.find( param => IS(obj, param) );
function IS(obj, ...shouldBe) {
if (shouldBe.length > 1) { return ISOneOf(obj, ...shouldBe); }
shouldBe = shouldBe.shift();
const invalid = `Invalid parameter(s)`;
const self = obj === 0 ? Number : obj === `` ? String :
!obj ? {name: invalid} :
Object.getPrototypeOf(obj)?.constructor;
return shouldBe ? shouldBe === self?.__proto__ || shouldBe === self :
self?.name ?? invalid;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment