@storm-stack/types
Version:
⚡ The storm-stack monorepo contains utility applications, tools, and various libraries to create modern and scalable web applications.
104 lines (103 loc) • 3.43 kB
JavaScript
import { isBuffer } from "./is-buffer.mjs";
const globalObject = ((Obj) => {
if (typeof globalThis === "object") {
return globalThis;
}
Object.defineProperty(Obj, "typeDetectGlobalObject", {
get() {
return this;
},
configurable: true
});
return global;
})(Object.prototype);
export function typeDetect(obj) {
if (isBuffer(obj)) {
return "Buffer";
}
const typeofObj = typeof obj;
if (typeofObj !== "object") {
return typeofObj;
}
if (obj === null) {
return "null";
}
if (obj === globalObject) {
return "global";
}
if (Array.isArray(obj) && (Symbol.toStringTag === void 0 || !(Symbol.toStringTag in obj))) {
return "Array";
}
if (typeof window === "object" && window !== null) {
if (typeof window.location === "object" && obj === window.location) {
return "Location";
}
if (typeof window.document === "object" && obj === window.document) {
return "Document";
}
if (typeof window.navigator === "object") {
if (typeof window.navigator.mimeTypes === "object" && obj === window.navigator.mimeTypes) {
return "MimeTypeArray";
}
if (typeof window.navigator.plugins === "object" && obj === window.navigator.plugins) {
return "PluginArray";
}
}
if ((typeof window.HTMLElement === "function" || typeof window.HTMLElement === "object") && obj instanceof window.HTMLElement) {
if (obj.tagName === "BLOCKQUOTE") {
return "HTMLQuoteElement";
}
if (obj.tagName === "TD") {
return "HTMLTableDataCellElement";
}
if (obj.tagName === "TH") {
return "HTMLTableHeaderCellElement";
}
}
}
const stringTag = Symbol.toStringTag !== void 0 && obj[Symbol.toStringTag];
if (typeof stringTag === "string") {
return stringTag;
}
const objPrototype = Object.getPrototypeOf(obj);
if (objPrototype === RegExp.prototype) {
return "RegExp";
}
if (objPrototype === Date.prototype) {
return "Date";
}
if (typeof Promise !== "undefined" && objPrototype === Promise.prototype) {
return "Promise";
}
if (typeof Set !== "undefined" && objPrototype === Set.prototype) {
return "Set";
}
if (typeof Map !== "undefined" && objPrototype === Map.prototype) {
return "Map";
}
if (typeof WeakSet !== "undefined" && objPrototype === WeakSet.prototype) {
return "WeakSet";
}
if (typeof WeakMap !== "undefined" && objPrototype === WeakMap.prototype) {
return "WeakMap";
}
if (typeof DataView !== "undefined" && objPrototype === DataView.prototype) {
return "DataView";
}
if (typeof Map !== "undefined" && objPrototype === Object.getPrototypeOf((/* @__PURE__ */ new Map()).entries())) {
return "Map Iterator";
}
if (typeof Set !== "undefined" && objPrototype === Object.getPrototypeOf((/* @__PURE__ */ new Set()).entries())) {
return "Set Iterator";
}
if (typeof Array.prototype[Symbol.iterator] === "function" && objPrototype === Object.getPrototypeOf([][Symbol.iterator]())) {
return "Array Iterator";
}
if (Symbol.iterator !== void 0 && typeof String.prototype[Symbol.iterator] === "function" && Object.getPrototypeOf(""[Symbol.iterator]()) && objPrototype === Object.getPrototypeOf(""[Symbol.iterator]())) {
return "String Iterator";
}
if (objPrototype === null) {
return "Object";
}
return Object.prototype.toString.call(obj).slice(8, -1);
}