0 Star 0 Fork 0

FeCoder / type-detector

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

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();

浏览器兼容

Chrome Firefox Edge Safari IE
110+ 110+ 110+ 15+ 不支持
MIT License Copyright (c) 2024-present ZG 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: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 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.

简介

获取和判断数据类型的 JavaScript 工具库 展开 收起
JavaScript 等 3 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/dreamer365/type-detector.git
git@gitee.com:dreamer365/type-detector.git
dreamer365
type-detector
type-detector
master

搜索帮助