{"version":3,"file":"type-detect.mjs","names":[],"sources":["../src/type-detect.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n                       🗲 Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website:                  https://stormsoftware.com\n Repository:               https://github.com/storm-software/stryke\n Documentation:            https://docs.stormsoftware.com/projects/stryke\n Contact:                  https://stormsoftware.com/contact\n\n SPDX-License-Identifier:  Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { isBuffer } from \"./is-buffer\";\n\nconst globalObject = (obj => {\n  if (typeof globalThis === \"object\") {\n    return globalThis;\n  }\n  Object.defineProperty(obj, \"typeDetectGlobalObject\", {\n    get() {\n      return this;\n    },\n    configurable: true\n  });\n\n  // // biome-ignore lint/correctness/noUndeclaredVariables: <explanation>\n  // const global = typeDetectGlobalObject;\n\n  // // biome-ignore lint/performance/noDelete: <explanation>\n  // delete obj.typeDetectGlobalObject;\n  return globalThis;\n})(Object.prototype);\n\nexport function typeDetect(obj: unknown): string {\n  // NOTE: isBuffer must execute before type-detect,\n  // because type-detect returns 'Uint8Array'.\n  if (isBuffer(obj)) {\n    return \"Buffer\";\n  }\n\n  const typeofObj = typeof obj;\n  if (typeofObj !== \"object\") {\n    return typeofObj;\n  }\n\n  if (obj === null) {\n    return \"null\";\n  }\n\n  if (obj === globalObject) {\n    return \"global\";\n  }\n\n  if (\n    Array.isArray(obj) &&\n    (Symbol.toStringTag === undefined || !(Symbol.toStringTag in obj))\n  ) {\n    return \"Array\";\n  }\n\n  // https://html.spec.whatwg.org/multipage/browsers.html#location\n  if (typeof globalThis === \"object\" && globalThis !== null) {\n    if (\n      typeof (globalThis as any).location === \"object\" &&\n      obj === (globalThis as any).location\n    ) {\n      return \"Location\";\n    }\n\n    // https://html.spec.whatwg.org/#document\n    if (\n      typeof (globalThis as any).document === \"object\" &&\n      obj === (globalThis as any).document\n    ) {\n      return \"Document\";\n    }\n\n    // https://html.spec.whatwg.org/multipage/webappapis.html#mimetypearray\n    if (typeof (globalThis as any).navigator === \"object\") {\n      if (\n        typeof (globalThis as any).navigator.mimeTypes === \"object\" &&\n        obj === (globalThis as any).navigator.mimeTypes\n      ) {\n        return \"MimeTypeArray\";\n      }\n\n      // https://html.spec.whatwg.org/multipage/webappapis.html#pluginarray\n      if (\n        typeof (globalThis as any).navigator.plugins === \"object\" &&\n        obj === (globalThis as any).navigator.plugins\n      ) {\n        return \"PluginArray\";\n      }\n    }\n\n    // https://html.spec.whatwg.org/multipage/webappapis.html#pluginarray\n    if (\n      (typeof (globalThis as any).HTMLElement === \"function\" ||\n        typeof (globalThis as any).HTMLElement === \"object\") &&\n      obj instanceof (globalThis as any).HTMLElement\n    ) {\n      if ((obj as any).tagName === \"BLOCKQUOTE\") {\n        return \"HTMLQuoteElement\";\n      }\n\n      // https://html.spec.whatwg.org/#htmltabledatacellelement\n      if ((obj as any).tagName === \"TD\") {\n        return \"HTMLTableDataCellElement\";\n      }\n\n      // https://html.spec.whatwg.org/#htmltableheadercellelement\n      if ((obj as any).tagName === \"TH\") {\n        return \"HTMLTableHeaderCellElement\";\n      }\n    }\n  }\n\n  const stringTag =\n    Symbol.toStringTag !== undefined && (obj as any)[Symbol.toStringTag];\n  if (typeof stringTag === \"string\") {\n    return stringTag;\n  }\n\n  const objPrototype = Object.getPrototypeOf(obj);\n  if (objPrototype === RegExp.prototype) {\n    return \"RegExp\";\n  }\n\n  if (objPrototype === Date.prototype) {\n    return \"Date\";\n  }\n\n  // http://www.ecma-international.org/ecma-262/6.0/index.html#sec-promise.prototype-@@tostringtag\n  if (typeof Promise !== \"undefined\" && objPrototype === Promise.prototype) {\n    return \"Promise\";\n  }\n\n  if (typeof Set !== \"undefined\" && objPrototype === Set.prototype) {\n    return \"Set\";\n  }\n\n  if (typeof Map !== \"undefined\" && objPrototype === Map.prototype) {\n    return \"Map\";\n  }\n\n  if (typeof WeakSet !== \"undefined\" && objPrototype === WeakSet.prototype) {\n    return \"WeakSet\";\n  }\n\n  if (typeof WeakMap !== \"undefined\" && objPrototype === WeakMap.prototype) {\n    return \"WeakMap\";\n  }\n\n  // http://www.ecma-international.org/ecma-262/6.0/index.html#sec-dataview.prototype-@@tostringtag\n  if (typeof DataView !== \"undefined\" && objPrototype === DataView.prototype) {\n    return \"DataView\";\n  }\n\n  // http://www.ecma-international.org/ecma-262/6.0/index.html#sec-%mapiteratorprototype%-@@tostringtag\n  if (\n    typeof Map !== \"undefined\" &&\n    objPrototype === Object.getPrototypeOf(new Map().entries())\n  ) {\n    return \"Map Iterator\";\n  }\n\n  // http://www.ecma-international.org/ecma-262/6.0/index.html#sec-%setiteratorprototype%-@@tostringtag\n  if (\n    typeof Set !== \"undefined\" &&\n    objPrototype === Object.getPrototypeOf(new Set().entries())\n  ) {\n    return \"Set Iterator\";\n  }\n\n  // http://www.ecma-international.org/ecma-262/6.0/index.html#sec-%arrayiteratorprototype%-@@tostringtag\n  if (\n    typeof Array.prototype[Symbol.iterator] === \"function\" &&\n    objPrototype === Object.getPrototypeOf([][Symbol.iterator]())\n  ) {\n    return \"Array Iterator\";\n  }\n\n  // http://www.ecma-international.org/ecma-262/6.0/index.html#sec-%stringiteratorprototype%-@@tostringtag\n  if (\n    Symbol.iterator !== undefined &&\n    typeof String.prototype[Symbol.iterator] === \"function\" &&\n    Object.getPrototypeOf(\"\"[Symbol.iterator]()) &&\n    objPrototype === Object.getPrototypeOf(\"\"[Symbol.iterator]())\n  ) {\n    return \"String Iterator\";\n  }\n\n  if (objPrototype === null) {\n    return \"Object\";\n  }\n\n  return Object.prototype.toString.call(obj).slice(8, -1);\n}\n"],"mappings":";;;AAoBA,MAAM,iBAAgB,QAAO;AAC3B,KAAI,OAAO,eAAe,SACxB,QAAO;AAET,QAAO,eAAe,KAAK,0BAA0B;EACnD,MAAM;AACJ,UAAO;;EAET,cAAc;EACf,CAAC;AAOF,QAAO;GACN,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