diff --git a/.gitignore b/.gitignore index eea1ef6..a19dcbb 100644 --- a/.gitignore +++ b/.gitignore @@ -23,4 +23,6 @@ type-detect.test.js .nyc_output index.js -index.d.ts +*.d.ts + +test/deno-test.js diff --git a/README.md b/README.md index 0a58456..c11808f 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@

- Improved typeof detection for node and the browser. + Improved typeof detection for node, , and the browser.

@@ -102,6 +102,14 @@ Sadly, `Object.prototype.toString` is slow, and buggy. By slow - we mean it is s $ npm install type-detect +### Deno + +`type-detect` can be imported with the following line: + +```js +import typeDetect 'https://raw.githubusercontent.com/chaijs/type-detect/v4.0.8/index.ts' +``` + ### Browsers You can also use it within the browser; install via npm and use the `type-detect.js` file found within the download. For example: diff --git a/index.ts b/index.ts index 1f3ca4b..d257582 100644 --- a/index.ts +++ b/index.ts @@ -124,7 +124,7 @@ export default function typeDetect(obj: unknown): string { * - IE <=11 === "[object Object]" * - IE Edge <=13 === "[object Object]" */ - if (typeof window.location === 'object' && obj === window.location) { + if (typeof (window as any).location === 'object' && obj === (window as any).location) { return 'Location'; } @@ -147,19 +147,19 @@ export default function typeDetect(obj: unknown): string { * - IE 11 === "[object HTMLDocument]" * - IE Edge <=13 === "[object HTMLDocument]" */ - if (typeof window.document === 'object' && obj === window.document) { + if (typeof (window as any).document === 'object' && obj === (window as any).document) { return 'Document'; } - if (typeof window.navigator === 'object') { + if (typeof (window as any).navigator === 'object') { /* ! Spec Conformance * (https://html.spec.whatwg.org/multipage/webappapis.html#mimetypearray) * WhatWG HTML$8.6.1.5 - Plugins - Interface MimeTypeArray * Test: `Object.prototype.toString.call(navigator.mimeTypes)`` * - IE <=10 === "[object MSMimeTypesCollection]" */ - if (typeof window.navigator.mimeTypes === 'object' && - obj === window.navigator.mimeTypes) { + if (typeof (window as any).navigator.mimeTypes === 'object' && + obj === (window as any).navigator.mimeTypes) { return 'MimeTypeArray'; } @@ -169,22 +169,22 @@ export default function typeDetect(obj: unknown): string { * Test: `Object.prototype.toString.call(navigator.plugins)`` * - IE <=10 === "[object MSPluginsCollection]" */ - if (typeof window.navigator.plugins === 'object' && - obj === window.navigator.plugins) { + if (typeof (window as any).navigator.plugins === 'object' && + obj === (window as any).navigator.plugins) { return 'PluginArray'; } } - if ((typeof window.HTMLElement === 'function' || - typeof window.HTMLElement === 'object') && - obj instanceof window.HTMLElement) { + if ((typeof (window as any).HTMLElement === 'function' || + typeof (window as any).HTMLElement === 'object') && + obj instanceof (window as any).HTMLElement) { /* ! Spec Conformance * (https://html.spec.whatwg.org/multipage/webappapis.html#pluginarray) * WhatWG HTML$4.4.4 - The `blockquote` element - Interface `HTMLQuoteElement` * Test: `Object.prototype.toString.call(document.createElement('blockquote'))`` * - IE <=10 === "[object HTMLBlockElement]" */ - if (obj.tagName === 'BLOCKQUOTE') { + if ((obj as any).tagName === 'BLOCKQUOTE') { return 'HTMLQuoteElement'; } @@ -200,7 +200,7 @@ export default function typeDetect(obj: unknown): string { * - Firefox === "[object HTMLTableCellElement]" * - Safari === "[object HTMLTableCellElement]" */ - if (obj.tagName === 'TD') { + if ((obj as any).tagName === 'TD') { return 'HTMLTableDataCellElement'; } @@ -216,7 +216,7 @@ export default function typeDetect(obj: unknown): string { * - Firefox === "[object HTMLTableCellElement]" * - Safari === "[object HTMLTableCellElement]" */ - if (obj.tagName === 'TH') { + if ((obj as any).tagName === 'TH') { return 'HTMLTableHeaderCellElement'; } } diff --git a/package.json b/package.json index ea5b27b..c27f69b 100644 --- a/package.json +++ b/package.json @@ -44,6 +44,7 @@ "test": "npm run test:node && npm run test:browser", "test:browser": "karma start --singleRun=true", "test:node": "nyc mocha type-detect.test.js", + "test:deno": "deno test test/deno-test.ts", "posttest:node": "nyc report --report-dir \"coverage/node-$(node --version)\" --reporter=lcovonly && npm run upload-coverage", "posttest:browser": "npm run upload-coverage", "upload-coverage": "codecov" diff --git a/test/deno-test.ts b/test/deno-test.ts new file mode 100644 index 0000000..b2666d0 --- /dev/null +++ b/test/deno-test.ts @@ -0,0 +1,7 @@ +/* global Deno:readonly */ +// @ts-nocheck +import { assertEquals } from 'https://deno.land/std/testing/asserts.ts'; +import typeDetect from '../index.ts'; +Deno.test('type detect works', () => { + assertEquals(typeDetect('hello'), 'string'); +});