Skip to content

Commit 2ee5488

Browse files
committed
feat: add support for Deno
1 parent 54138a7 commit 2ee5488

File tree

5 files changed

+33
-15
lines changed

5 files changed

+33
-15
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@ type-detect.test.js
2323
.nyc_output
2424

2525
index.js
26-
index.d.ts
26+
*.d.ts
27+
28+
test/deno-test.js

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</h1>
66
<br>
77
<p align=center>
8-
Improved typeof detection for <a href="http://nodejs.org">node</a> and the browser.
8+
Improved typeof detection for <a href="https://nodejs.org">node</a>, <a href="https://deno.land/">, and the browser.
99
</p>
1010

1111
<p align=center>
@@ -102,6 +102,14 @@ Sadly, `Object.prototype.toString` is slow, and buggy. By slow - we mean it is s
102102

103103
$ npm install type-detect
104104

105+
### Deno
106+
107+
`type-detect` can be imported with the following line:
108+
109+
```js
110+
import typeDetect 'https://raw.githubusercontent.com/chaijs/type-detect/v4.0.8/index.ts'
111+
```
112+
105113
### Browsers
106114

107115
You can also use it within the browser; install via npm and use the `type-detect.js` file found within the download. For example:

index.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export default function typeDetect(obj: unknown): string {
124124
* - IE <=11 === "[object Object]"
125125
* - IE Edge <=13 === "[object Object]"
126126
*/
127-
if (typeof window.location === 'object' && obj === window.location) {
127+
if (typeof (window as any).location === 'object' && obj === (window as any).location) {
128128
return 'Location';
129129
}
130130

@@ -147,19 +147,19 @@ export default function typeDetect(obj: unknown): string {
147147
* - IE 11 === "[object HTMLDocument]"
148148
* - IE Edge <=13 === "[object HTMLDocument]"
149149
*/
150-
if (typeof window.document === 'object' && obj === window.document) {
150+
if (typeof (window as any).document === 'object' && obj === (window as any).document) {
151151
return 'Document';
152152
}
153153

154-
if (typeof window.navigator === 'object') {
154+
if (typeof (window as any).navigator === 'object') {
155155
/* ! Spec Conformance
156156
* (https://html.spec.whatwg.org/multipage/webappapis.html#mimetypearray)
157157
* WhatWG HTML$8.6.1.5 - Plugins - Interface MimeTypeArray
158158
* Test: `Object.prototype.toString.call(navigator.mimeTypes)``
159159
* - IE <=10 === "[object MSMimeTypesCollection]"
160160
*/
161-
if (typeof window.navigator.mimeTypes === 'object' &&
162-
obj === window.navigator.mimeTypes) {
161+
if (typeof (window as any).navigator.mimeTypes === 'object' &&
162+
obj === (window as any).navigator.mimeTypes) {
163163
return 'MimeTypeArray';
164164
}
165165

@@ -169,22 +169,22 @@ export default function typeDetect(obj: unknown): string {
169169
* Test: `Object.prototype.toString.call(navigator.plugins)``
170170
* - IE <=10 === "[object MSPluginsCollection]"
171171
*/
172-
if (typeof window.navigator.plugins === 'object' &&
173-
obj === window.navigator.plugins) {
172+
if (typeof (window as any).navigator.plugins === 'object' &&
173+
obj === (window as any).navigator.plugins) {
174174
return 'PluginArray';
175175
}
176176
}
177177

178-
if ((typeof window.HTMLElement === 'function' ||
179-
typeof window.HTMLElement === 'object') &&
180-
obj instanceof window.HTMLElement) {
178+
if ((typeof (window as any).HTMLElement === 'function' ||
179+
typeof (window as any).HTMLElement === 'object') &&
180+
obj instanceof (window as any).HTMLElement) {
181181
/* ! Spec Conformance
182182
* (https://html.spec.whatwg.org/multipage/webappapis.html#pluginarray)
183183
* WhatWG HTML$4.4.4 - The `blockquote` element - Interface `HTMLQuoteElement`
184184
* Test: `Object.prototype.toString.call(document.createElement('blockquote'))``
185185
* - IE <=10 === "[object HTMLBlockElement]"
186186
*/
187-
if (obj.tagName === 'BLOCKQUOTE') {
187+
if ((obj as any).tagName === 'BLOCKQUOTE') {
188188
return 'HTMLQuoteElement';
189189
}
190190

@@ -200,7 +200,7 @@ export default function typeDetect(obj: unknown): string {
200200
* - Firefox === "[object HTMLTableCellElement]"
201201
* - Safari === "[object HTMLTableCellElement]"
202202
*/
203-
if (obj.tagName === 'TD') {
203+
if ((obj as any).tagName === 'TD') {
204204
return 'HTMLTableDataCellElement';
205205
}
206206

@@ -216,7 +216,7 @@ export default function typeDetect(obj: unknown): string {
216216
* - Firefox === "[object HTMLTableCellElement]"
217217
* - Safari === "[object HTMLTableCellElement]"
218218
*/
219-
if (obj.tagName === 'TH') {
219+
if ((obj as any).tagName === 'TH') {
220220
return 'HTMLTableHeaderCellElement';
221221
}
222222
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"test": "npm run test:node && npm run test:browser",
4545
"test:browser": "karma start --singleRun=true",
4646
"test:node": "nyc mocha type-detect.test.js",
47+
"test:deno": "deno test test/deno-test.ts",
4748
"posttest:node": "nyc report --report-dir \"coverage/node-$(node --version)\" --reporter=lcovonly && npm run upload-coverage",
4849
"posttest:browser": "npm run upload-coverage",
4950
"upload-coverage": "codecov"

test/deno-test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* global Deno:readonly */
2+
// @ts-nocheck
3+
import { assertEquals } from 'https://deno.land/std/testing/asserts.ts';
4+
import typeDetect from '../index.ts';
5+
Deno.test('type detect works', () => {
6+
assertEquals(typeDetect('hello'), 'string');
7+
});

0 commit comments

Comments
 (0)