Skip to content

Commit

Permalink
Merge pull request #142 from chaijs/feat-add-support-for-deno
Browse files Browse the repository at this point in the history
Switch to TypeScript, add support for Deno
  • Loading branch information
keithamus authored May 27, 2020
2 parents c91fb30 + 9042823 commit f9693aa
Show file tree
Hide file tree
Showing 7 changed files with 3,873 additions and 2,216 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ coverage/
type-detect.js
type-detect.test.js
.nyc_output

index.js
*.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 @@ -97,6 +97,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 type from 'https://deno.land/x/type_detect@v4.1.0/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
49 changes: 32 additions & 17 deletions index.js → index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,22 @@
*/
const promiseExists = typeof Promise === 'function';

/* eslint-disable no-undef */
const globalObject = typeof self === 'object' ? self : global; // eslint-disable-line id-blacklist
const globalObject = ((Obj) => {
if (typeof globalThis === 'object') {
return globalThis; // eslint-disable-line
}
Object.defineProperty(Obj, 'typeDetectGlobalObject', {
get() {
return this;
},
configurable: true,
});
// @ts-ignore
const global = typeDetectGlobalObject; // eslint-disable-line
// @ts-ignore
delete Obj.typeDetectGlobalObject;
return global;
})(Object.prototype);

const symbolExists = typeof Symbol !== 'undefined';
const mapExists = typeof Map !== 'undefined';
Expand All @@ -26,6 +40,7 @@ const stringIteratorExists = symbolIteratorExists && typeof String.prototype[Sym
const stringIteratorPrototype = stringIteratorExists && Object.getPrototypeOf(''[Symbol.iterator]());
const toStringLeftSliceLength = 8;
const toStringRightSliceLength = -1;

/**
* ### typeOf (obj)
*
Expand All @@ -36,7 +51,7 @@ const toStringRightSliceLength = -1;
* @return {String} object type
* @api public
*/
export default function typeDetect(obj) {
export default function typeDetect(obj: unknown): string {
/* ! Speed optimisation
* Pre:
* string literal x 3,039,035 ops/sec ±1.62% (78 runs sampled)
Expand Down Expand Up @@ -109,7 +124,7 @@ export default function typeDetect(obj) {
* - 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 @@ -132,19 +147,19 @@ export default function typeDetect(obj) {
* - 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 @@ -154,22 +169,22 @@ export default function typeDetect(obj) {
* 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 @@ -185,7 +200,7 @@ export default function typeDetect(obj) {
* - Firefox === "[object HTMLTableCellElement]"
* - Safari === "[object HTMLTableCellElement]"
*/
if (obj.tagName === 'TD') {
if ((obj as any).tagName === 'TD') {
return 'HTMLTableDataCellElement';
}

Expand All @@ -201,7 +216,7 @@ export default function typeDetect(obj) {
* - Firefox === "[object HTMLTableCellElement]"
* - Safari === "[object HTMLTableCellElement]"
*/
if (obj.tagName === 'TH') {
if ((obj as any).tagName === 'TH') {
return 'HTMLTableHeaderCellElement';
}
}
Expand Down Expand Up @@ -229,7 +244,7 @@ export default function typeDetect(obj) {
* Int8Array x 6,606,078 ops/sec ±1.74% (81 runs sampled)
* Uint8ClampedArray x 6,602,224 ops/sec ±1.77% (83 runs sampled)
*/
const stringTag = (symbolToStringTagExists && obj[Symbol.toStringTag]);
const stringTag = (symbolToStringTagExists && (obj as any)[Symbol.toStringTag]);
if (typeof stringTag === 'string') {
return stringTag;
}
Expand Down
Loading

0 comments on commit f9693aa

Please sign in to comment.