├── .editorconfig ├── .gitattributes ├── .github ├── security.md └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── index.d.ts ├── index.js ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 20 14 | - 18 15 | steps: 16 | - uses: actions/checkout@v4 17 | - uses: actions/setup-node@v4 18 | with: 19 | node-version: ${{ matrix.node-version }} 20 | - run: npm install 21 | - run: npm test 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | Determine the type of the given value. 3 | */ 4 | declare function type(value: unknown): string; 5 | 6 | export = type; 7 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const {toString} = Object.prototype; 2 | 3 | const NODE_TYPE_ELEMENT = 1; 4 | 5 | const DOM_PROPERTIES_TO_CHECK = [ 6 | 'innerHTML', 7 | 'ownerDocument', 8 | 'style', 9 | 'attributes', 10 | 'nodeValue', 11 | ]; 12 | 13 | function isObject(value) { 14 | return value !== null && typeof value === 'object'; 15 | } 16 | 17 | function isHtmlElement(value) { 18 | return isObject(value) 19 | && value.nodeType === NODE_TYPE_ELEMENT 20 | && typeof value.nodeName === 'string' 21 | && DOM_PROPERTIES_TO_CHECK.every(property => property in value); 22 | } 23 | 24 | module.exports = function (value) { 25 | if (value === undefined) { 26 | return 'undefined'; 27 | } 28 | 29 | if (value === null) { 30 | return 'null'; 31 | } 32 | 33 | if (Number.isNaN(value)) { 34 | return 'nan'; 35 | } 36 | 37 | if (Array.isArray(value)) { 38 | return 'array'; 39 | } 40 | 41 | switch (toString.call(value)) { 42 | case '[object Date]': { 43 | return 'date'; 44 | } 45 | 46 | case '[object RegExp]': { 47 | return 'regexp'; 48 | } 49 | 50 | case '[object Arguments]': { 51 | return 'arguments'; 52 | } 53 | 54 | case '[object Error]': { 55 | return 'error'; 56 | } 57 | 58 | // No default 59 | } 60 | 61 | if (value && isHtmlElement(value)) { 62 | return 'element'; 63 | } 64 | 65 | if (value && value instanceof Uint8Array && value.constructor.name === 'Buffer') { 66 | return 'buffer'; 67 | } 68 | 69 | if (typeof value.valueOf === 'function') { 70 | value = value.valueOf?.() ?? Object.prototype.valueOf.apply(value); 71 | } 72 | 73 | return typeof value; 74 | }; 75 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) TJ Holowaychuk 4 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 7 | 8 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 9 | 10 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 11 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "component-type", 3 | "version": "2.0.0", 4 | "description": "Type assertions aka less-broken `typeof`", 5 | "license": "MIT", 6 | "repository": "sindresorhus/component-type", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "exports": { 9 | "types": "./index.d.ts", 10 | "default": "./index.js" 11 | }, 12 | "main": "./index.js", 13 | "types": "./index.d.ts", 14 | "sideEffects": false, 15 | "engines": { 16 | "node": ">=18" 17 | }, 18 | "scripts": { 19 | "test": "xo && ava" 20 | }, 21 | "files": [ 22 | "index.js", 23 | "index.d.ts" 24 | ], 25 | "keywords": [ 26 | "typeof", 27 | "type", 28 | "types", 29 | "check", 30 | "utility" 31 | ], 32 | "devDependencies": { 33 | "ava": "^5.3.1", 34 | "xo": "^0.56.0" 35 | }, 36 | "xo": { 37 | "rules": { 38 | "unicorn/prefer-module": "off" 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # component-type 2 | 3 | > Type assertions aka less-broken `typeof` 4 | 5 | ## Install 6 | 7 | ```sh 8 | npm install component-type 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | import type from 'component-type'; 15 | 16 | const date = new Date(); 17 | 18 | console.log(type(date)); 19 | //=> 'date' 20 | ``` 21 | 22 | ## API 23 | 24 | ```js 25 | type(new Date) === 'date' 26 | type({}) === 'object' 27 | type(null) === 'null' 28 | type(undefined) === 'undefined' 29 | type('hey') === 'string' 30 | type(true) === 'boolean' 31 | type(false) === 'boolean' 32 | type(12) === 'number' 33 | type(type) === 'function' 34 | type(/asdf/) === 'regexp' 35 | type((function(){ return arguments })()) === 'arguments' 36 | type([]) === 'array' 37 | type(document.createElement('div')) === 'element' 38 | type(NaN) === 'nan' 39 | type(new Error('Oh noes')) === 'error' 40 | type(new Buffer) === 'buffer' 41 | ``` 42 | 43 | It makes no guarantees about the correctness when fed untrusted user-input. 44 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | /* eslint-disable no-new-wrappers, unicorn/new-for-builtins, no-array-constructor, prefer-rest-params, n/prefer-global/buffer */ 2 | const test = require('ava'); 3 | const type = require('./index.js'); 4 | 5 | test('objects', t => { 6 | function Foo() {} 7 | t.is(type({}), 'object'); 8 | t.is(type(new Foo()), 'object'); 9 | }); 10 | 11 | test('numbers', t => { 12 | t.is(type(12), 'number'); 13 | t.is(type(1), 'number'); 14 | t.is(type(-5), 'number'); 15 | t.is(type(new Number(123)), 'number'); 16 | t.is(type(Number.POSITIVE_INFINITY), 'number'); 17 | }); 18 | 19 | test('NaN', t => { 20 | t.is(type(Number.NaN), 'nan'); 21 | }); 22 | 23 | test('strings', t => { 24 | t.is(type('test'), 'string'); 25 | t.is(type(new String('whoop')), 'string'); 26 | }); 27 | 28 | test('dates', t => { 29 | t.is(type(new Date()), 'date'); 30 | }); 31 | 32 | test('booleans', t => { 33 | t.is(type(true), 'boolean'); 34 | t.is(type(false), 'boolean'); 35 | t.is(type(new Boolean(true)), 'boolean'); 36 | }); 37 | 38 | test('null', t => { 39 | t.is(type(null), 'null'); 40 | }); 41 | 42 | test('undefined', t => { 43 | t.is(type(), 'undefined'); 44 | t.is(type(undefined), 'undefined'); 45 | }); 46 | 47 | test('arrays', t => { 48 | t.is(type([]), 'array'); 49 | t.is(type(new Array()), 'array'); 50 | }); 51 | 52 | test('regexps', t => { 53 | t.is(type(/asdf/), 'regexp'); 54 | }); 55 | 56 | test('functions', t => { 57 | t.is(type(() => {}), 'function'); 58 | }); 59 | 60 | test('arguments', t => { 61 | (function () { 62 | t.is(type(arguments), 'arguments'); 63 | })(); 64 | }); 65 | 66 | test('elements', t => { 67 | const foo = { 68 | key: 'OK', 69 | nodeType: 1, 70 | }; 71 | 72 | t.not(type(foo), 'element'); 73 | }); 74 | 75 | test('errors', t => { 76 | t.is(type(new Error('Ups!')), 'error'); 77 | }); 78 | 79 | test('buffers', t => { 80 | t.is(type(Buffer.alloc(4)), 'buffer'); 81 | }); 82 | --------------------------------------------------------------------------------