🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

type-detector-js

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

type-detector-js

获取和判断数据类型的 JavaScript 工具库

latest
npmnpm
Version
1.0.1
Version published
Weekly downloads
4
100%
Maintainers
1
Weekly downloads
 
Created
Source

TypeDetector

获取和判断数据类型的 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 );

方法列表

方法详述

isFunction

功能:判断是否为函数。

/** 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 );

isString

功能:判断是否为字符串。

/** true */
TD( "abc" ).isString();
TD( String( "abc" ) ).isString();

isArray

功能:判断是否为数组。

/** true */
TD( [ 1, 2 ] ).isArray();
TD( Array( 1, 2 ) ).isArray();

isBoolean

功能:判断是否为布尔值。

/** true */
TD( true ).isBoolean();
TD( Boolean( true ) ).isBoolean();

isSymbol

功能:判断是否为 Symbol

/** true */
TD( Symbol() ).isSymbol();
TD( Symbol.for( "abc" ) ).isSymbol();

isBigInt

功能:判断是否为 BigInt

/** true */
TD( 10n ).isBigInt();
TD( BigInt( 10 ) ).isBigInt();

isUndefined

功能:判断是否为 undefined

/** true */
TD( undefined ).isUndefined();

isNull

功能:判断是否为 null

/** true */
TD( null ).isNull();

isNil

功能:判断是否为 nullundefined

/** true */
TD( null ).isNil();
TD( undefined ).isNil();

isNumber

功能:判断是否为数字。

/** true */
TD( 10 ).isNumber();
TD( Number( 20 ) ).isNumber();

isInt

功能:判断是否为整数( 必须是安全整数,即在 -2^53 到 2^53 之间的整数 )。

/** true */
TD( 10 ).isInt();
TD( 10.00 ).isInt();
TD( Number( 20 ) ).isInt();

isFloat

功能:判断是否为浮点数( 必须在安全整数范围内,即在 -2^53 到 2^53 之间的数值 )。

/** true */
TD( 10.01 ).isFloat();
TD( Number( -20.2 ) ).isFloat();

isNaN

功能:判断是否为 NaN

/** true */
TD( NaN ).isNaN();

isFinite

功能:判断是否为一个有限数。

/** true */
TD( 20 ).isFinite();

/** false */
TD( Infinity ).isFinite();
TD( -Infinity ).isFinite();
TD( NaN ).isFinite();

isSet

功能:判断是否为 Set

/** true */
TD( new Set() ).isSet();

isWeakSet

功能:判断是否为 WeakSet

/** true */
TD( new WeakSet() ).isWeakSet();

isMap

功能:判断是否为 Map

/** true */
TD( new Map() ).isMap();

isWeakMap

功能:判断是否为 WeakMap

/** true */
TD( new WeakMap() ).isWeakMap();

isPlainObject

功能:判断是否为纯对象。

注:只有通过 "字面量形式或 Object 构造函数" 创建的对象才会判定为纯对象。

/** true */
TD( {} ).isPlainObject();
TD( Object() ).isPlainObject();

isObject

功能:判断是否为对象。

注:只要 typeof 返回 object 的都判定为对象。

/** true */
TD( {} ).isObject();
TD( [] ).isObject();
TD( null ).isObject();

isElement

功能:判断是否为 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();

isWindow

功能:判断是否为 window 对象。

注:只能用于浏览器环境,在 node 环境中将直接返回 false

/** true */
TD( window ).isWindow();

isPromise

功能:判断是否为 Promise

/** true */
TD( new Promise( () => {} ) ).isPromise();

isClass

功能:判断是否为 class 类。

/** true */
TD( class Test {} ).isClass();

isBlob

功能:判断是否为 Blob

注:只能用于浏览器环境,在 node 环境中将直接返回 false

/** true */
TD( new Blob( [ "123" ], { type: "text/html" } ) ).isBlob();

isArrayBuffer

功能:判断是否为 ArrayBuffer

/** true */
TD( new ArrayBuffer( 8 ) ).isArrayBuffer();

isBuffer

功能:判断是否为 Buffer

注:只能用于 node 环境,在浏览器环境中将直接返回 false

/** true */
TD( Buffer.from( "test" ) ).isBuffer();

isEmptyPlainObject

功能:判断是否为空的纯对象。

/** true */
TD( {} ).isEmptyPlainObject();

/** false */
TD( { a: 1 } ).isEmptyPlainObject();

isEmptyArray

功能:判断是否为空数组。

/** true */
TD( [] ).isEmptyArray();

/** false */
TD( [ 1, 2 ] ).isEmptyArray();

isEmptyString

功能:判断是否为空字符串。

/** true */
TD( "" ).isEmptyString();

/** false */
TD( " " ).isEmptyString();
TD( "test" ).isEmptyString();

isArrayOnlyIncludePlainObject

功能:判断是否为只包含纯对象的数组。

/** true */
TD( [ {}, {}, {} ] ).isArrayOnlyIncludePlainObject();

/** false */
TD( [ {}, 1, true ] ).isArrayOnlyIncludePlainObject();

type

功能:获取数据类型。

/** array */
TD( [] ).type();

/** object */
TD( {} ).type();

/** string */
TD( "" ).type();

/** number */
TD( 10 ).type();

浏览器兼容

ChromeFirefoxEdgeSafariIE
110+110+110+15+不支持

Keywords

数据类型

FAQs

Package last updated on 26 Mar 2024

Did you know?

Socket

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.

Install

Related posts