├── .github └── workflows │ └── nodejs.yml ├── .gitignore ├── .npmrc ├── CODEOWNERS ├── LICENSE ├── README.md ├── appveyor.yml ├── bench ├── .eslintrc └── index.js ├── index.ts ├── karma.conf.js ├── package-lock.json ├── package.json ├── rollup.conf.js ├── test ├── .eslintrc ├── deno-test.ts ├── dom.js ├── index.js ├── new-ecmascript-types.js ├── node.js └── tostringtag-extras.js ├── tsconfig.json └── type-detect-logo.svg /.github/workflows/nodejs.yml: -------------------------------------------------------------------------------- 1 | name: Build 2 | 3 | on: 4 | pull_request: 5 | push: 6 | branches: 7 | - master 8 | 9 | jobs: 10 | build: 11 | name: Build 12 | runs-on: ubuntu-latest 13 | steps: 14 | - name: Get sources 15 | uses: actions/checkout@v1 16 | 17 | - name: Restore npm cache 18 | uses: actions/cache@v2 19 | with: 20 | path: ~/.npm 21 | key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} 22 | restore-keys: | 23 | ${{ runner.os }}-node- 24 | - uses: actions/cache@v2 25 | with: 26 | path: node_modules 27 | key: ${{ runner.os }}-node-modules-${{ hashFiles('**/package-lock.json') }} 28 | restore-keys: | 29 | ${{ runner.os }}-node-modules- 30 | - name: Use Node.js 14 31 | uses: actions/setup-node@v2 32 | with: 33 | node-version: '14' 34 | 35 | - name: Install dependencies 36 | run: npm install 37 | 38 | - name: Build 39 | run: npm run build 40 | 41 | - name: Test 42 | run: npm run test 43 | env: 44 | CI: true 45 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | lib-cov 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.pid 8 | *.gz 9 | 10 | pids 11 | logs 12 | results 13 | build 14 | components 15 | 16 | node_modules 17 | npm-debug.log 18 | 19 | coverage/ 20 | 21 | type-detect.js 22 | type-detect.test.js 23 | .nyc_output 24 | 25 | index.js 26 | *.d.ts 27 | 28 | test/deno-test.js 29 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | registry=https://registry.npmjs.org/ 2 | -------------------------------------------------------------------------------- /CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @chaijs/type-detect 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 Jake Luer (http://alogicalparadox.com) 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 | type-detect 4 | 5 |

6 |
7 |

8 | Improved typeof detection for node, Deno, and the browser. 9 |

10 | 11 |

12 | 13 | license:mit 17 | 18 | 19 | npm:? 23 | 24 | 25 | build:? 29 | 30 | 31 | coverage:? 35 | 36 | 37 | dependencies:? 41 | 42 | 43 | devDependencies:? 47 | 48 |
49 | 50 | Join the Slack chat 54 | 55 | 56 | Join the Gitter chat 60 | 61 |

62 |
63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 |
Supported Browsers
Chrome Edge Firefox Safari IE
9, 10, 11
78 |
79 | 80 | ## What is Type-Detect? 81 | 82 | Type Detect is a module which you can use to detect the type of a given object. It returns a string representation of the object's type, either using [`typeof`](http://www.ecma-international.org/ecma-262/6.0/index.html#sec-typeof-operator) or [`@@toStringTag`](http://www.ecma-international.org/ecma-262/6.0/index.html#sec-symbol.tostringtag). It also normalizes some object names for consistency among browsers. 83 | 84 | ## Why? 85 | 86 | The `typeof` operator will only specify primitive values; everything else is `"object"` (including `null`, arrays, regexps, etc). Many developers use `Object.prototype.toString()` - which is a fine alternative and returns many more types (null returns `[object Null]`, Arrays as `[object Array]`, regexps as `[object RegExp]` etc). 87 | 88 | Sadly, `Object.prototype.toString` is slow, and buggy. By slow - we mean it is slower than `typeof`. By buggy - we mean that some values (like Promises, the global object, iterators, dataviews, a bunch of HTML elements) all report different things in different browsers. 89 | 90 | `type-detect` fixes all of the shortcomings with `Object.prototype.toString`. We have extra code to speed up checks of JS and DOM objects, as much as 20-30x faster for some values. `type-detect` also fixes any consistencies with these objects. 91 | 92 | ## Installation 93 | 94 | ### Node.js 95 | 96 | `type-detect` is available on [npm](http://npmjs.org). To install it, type: 97 | 98 | $ npm install type-detect 99 | 100 | ### Deno 101 | 102 | `type-detect` can be imported with the following line: 103 | 104 | ```js 105 | import type from 'https://deno.land/x/type_detect@v4.1.0/index.ts' 106 | ``` 107 | 108 | ### Browsers 109 | 110 | You can also use it within the browser; install via npm and use the `type-detect.js` file found within the download. For example: 111 | 112 | ```html 113 | 114 | ``` 115 | 116 | ## Usage 117 | 118 | The primary export of `type-detect` is function that can serve as a replacement for `typeof`. The results of this function will be more specific than that of native `typeof`. 119 | 120 | ```js 121 | var type = require('type-detect'); 122 | ``` 123 | Or, in the browser use case, after the