Skip to content

Commit

Permalink
feat: add support for Deno
Browse files Browse the repository at this point in the history
  • Loading branch information
keithamus committed May 26, 2020
1 parent 54138a7 commit 2ee5488
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 15 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ type-detect.test.js
.nyc_output

index.js
index.d.ts
*.d.ts

test/deno-test.js
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</h1>
<br>
<p align=center>
Improved typeof detection for <a href="http://nodejs.org">node</a> and the browser.
Improved typeof detection for <a href="https://nodejs.org">node</a>, <a href="https://deno.land/">, and the browser.
</p>

<p align=center>
Expand Down Expand Up @@ -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:
Expand Down
26 changes: 13 additions & 13 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

Expand All @@ -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';
}

Expand All @@ -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';
}

Expand All @@ -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';
}

Expand All @@ -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';
}
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 7 additions & 0 deletions test/deno-test.ts
Original file line number Diff line number Diff line change
@@ -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');
});

0 comments on commit 2ee5488

Please sign in to comment.