
Security News
GitHub Actions Checkout Now Blocks Risky pull_request_target Checkouts
GitHub Actions checkout now blocks risky pull_request_target checkouts by default to help prevent pwn request supply chain attacks.
type-detector-js
Advanced tools
获取和判断数据类型的 JavaScript 工具库
无任何第三方依赖,完全使用原生 JavaScript 开发
体积轻巧,多端兼容,可完美应用于浏览器环境和 Node 环境
CDN 引入
<!-- unpkg CDN -->
<script src="https://unpkg.com/type-detector-js"></script>
<!-- jsdelivr CDN -->
<script src="https://cdn.jsdelivr.net/npm/type-detector-js"></script>
NPM 安装
/** 安装 */
npm i type-detector-js -S
/** 通过 require 引入 */
const TD = require( "type-detector-js" );
/**
* 通过 import 引入
* 注:需要在 package.json 中添加 "type": "module"
*/
import TD from "type-detector-js/dist/type-detector.esm.mjs";
const isString = TD( "abc" ).isString();
const isNumber = TD( "abc" ).isNumber();
const type = TD( "abc" ).type();
/** true */
console.log( isString );
/** false */
console.log( isNumber );
/** string */
console.log( type );
/** true */
TD( function test () {} ).isFunction();
TD( function * test () {} ).isFunction();
TD( () => {} ).isFunction();
默认情况下,不会将 class 类判定为函数。
/** false */
TD( class Test {} ).isFunction();
如果希望 isFunction 能包含 class 类,可传入一个 true 参数:
/** true */
TD( class Test {} ).isFunction( true );
/** true */
TD( "abc" ).isString();
TD( String( "abc" ) ).isString();
/** true */
TD( [ 1, 2 ] ).isArray();
TD( Array( 1, 2 ) ).isArray();
/** true */
TD( true ).isBoolean();
TD( Boolean( true ) ).isBoolean();
Symbol。/** true */
TD( Symbol() ).isSymbol();
TD( Symbol.for( "abc" ) ).isSymbol();
BigInt。/** true */
TD( 10n ).isBigInt();
TD( BigInt( 10 ) ).isBigInt();
undefined。/** true */
TD( undefined ).isUndefined();
null。/** true */
TD( null ).isNull();
null 或 undefined。/** true */
TD( null ).isNil();
TD( undefined ).isNil();
/** true */
TD( 10 ).isNumber();
TD( Number( 20 ) ).isNumber();
/** true */
TD( 10 ).isInt();
TD( 10.00 ).isInt();
TD( Number( 20 ) ).isInt();
/** true */
TD( 10.01 ).isFloat();
TD( Number( -20.2 ) ).isFloat();
NaN。/** true */
TD( NaN ).isNaN();
/** true */
TD( 20 ).isFinite();
/** false */
TD( Infinity ).isFinite();
TD( -Infinity ).isFinite();
TD( NaN ).isFinite();
Set。/** true */
TD( new Set() ).isSet();
WeakSet。/** true */
TD( new WeakSet() ).isWeakSet();
Map。/** true */
TD( new Map() ).isMap();
WeakMap。/** true */
TD( new WeakMap() ).isWeakMap();
注:只有通过 "字面量形式或 Object 构造函数" 创建的对象才会判定为纯对象。
/** true */
TD( {} ).isPlainObject();
TD( Object() ).isPlainObject();
注:只要 typeof 返回 object 的都判定为对象。
/** true */
TD( {} ).isObject();
TD( [] ).isObject();
TD( null ).isObject();
DOM 元素。注:必须是单个且页面中真实存在的元素。
/** true */
TD( document.querySelector( "html" ) ).isElement();
TD( document.getElementById( "target" ) ).isElement();
TD( document.getElementsByTagName( "div" )[ 0 ] ).isElement();
/** false */
TD( document.querySelectorAll( "div" ) ).isElement();
TD( document.getElementsByTagName( "div" ) ).isElement();
window 对象。注:只能用于浏览器环境,在 node 环境中将直接返回 false。
/** true */
TD( window ).isWindow();
Promise。/** true */
TD( new Promise( () => {} ) ).isPromise();
class 类。/** true */
TD( class Test {} ).isClass();
Blob。注:只能用于浏览器环境,在 node 环境中将直接返回 false。
/** true */
TD( new Blob( [ "123" ], { type: "text/html" } ) ).isBlob();
ArrayBuffer。/** true */
TD( new ArrayBuffer( 8 ) ).isArrayBuffer();
Buffer。注:只能用于 node 环境,在浏览器环境中将直接返回 false。
/** true */
TD( Buffer.from( "test" ) ).isBuffer();
/** true */
TD( {} ).isEmptyPlainObject();
/** false */
TD( { a: 1 } ).isEmptyPlainObject();
/** true */
TD( [] ).isEmptyArray();
/** false */
TD( [ 1, 2 ] ).isEmptyArray();
/** true */
TD( "" ).isEmptyString();
/** false */
TD( " " ).isEmptyString();
TD( "test" ).isEmptyString();
/** true */
TD( [ {}, {}, {} ] ).isArrayOnlyIncludePlainObject();
/** false */
TD( [ {}, 1, true ] ).isArrayOnlyIncludePlainObject();
/** array */
TD( [] ).type();
/** object */
TD( {} ).type();
/** string */
TD( "" ).type();
/** number */
TD( 10 ).type();
| Chrome | Firefox | Edge | Safari | IE |
|---|---|---|---|---|
| 110+ | 110+ | 110+ | 15+ | 不支持 |
FAQs
获取和判断数据类型的 JavaScript 工具库
The npm package type-detector-js receives a total of 3 weekly downloads. As such, type-detector-js popularity was classified as not popular.
We found that type-detector-js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
GitHub Actions checkout now blocks risky pull_request_target checkouts by default to help prevent pwn request supply chain attacks.

Product
Socket now supports Custom Roles and Repository Access Permissions so organizations can control who can access specific repositories and actions.

Product
Socket MCP now lets AI assistants review org alerts, investigate threats using the Socket threat feed, and inspect package files in addition to dependency scoring.