@vvi/is
v2.0.5
Published
JavaScript/TypeScript 的类型检测工具,支持 TypeScript 类型收缩
Downloads
171
Maintainers
Readme
is
一个简单的类型判断工具
使用
import { typeOf } from '@vvi/is';
const num = 1;
if (typeOf(num) === 'number') {
console.log('num is number');
} else {
console.log('num is not number');
}使用 ts 中的 is 类型判断来进行类型安全收缩
import { isString, isNumber } from '@vvi/is';
function doSomething(value: string | number) {
if (isString(value)) {
value.toLocaleUpperCase();
} else if (isNumber(value)) {
value.toFixed(2);
}
}
doSomething('hello'); // HELLO
doSomething(1); // 1.00向 as 说 'no'
vitest
import { isType } from '@vvi/is';
interface Person {
name: string;
age: number;
}
function doSomething(value: string | number | boolean | ) {
// 将判断用作类型判断
if (isType<Person>(value, () => (value && value.name === 'Tom' && value.age === 18))) {
console.log('value is Person');
return;
}
/// 作为类型判定
if (isType<string>(value)) {
value.toLocaleUpperCase();
return;
}
/// 使用判断力
if (isType<boolean>(value, Boolean(value) === true )) {
console.log('value is true');
return;
}
}文档提供类型检测
isString字符串、String对象构建的字符串isEmptyString空字符串isBusinessEmptyString空字符串(去除首尾空格)isNumber数字、Number对象构建的数字isBoolean布尔值、Boolean对象构建的布尔值isTrue是否为 trueisFalse是否为 falseisNullnullisUndefinedundefinedisNaNNaN(NaN是一个特殊的数值NaN !== NaN即便typeof NaN返回的是number)isFunction函数isArray数组、Array对象构建的数组isEmptyArray是否是空数组isSymbolsymbolisBigInt大整数isPlainObject对象(普通对象,非其他内置对象类型)isEmptyObject是否为空对象(没有私有键的对象)isPromisePromiseisAsyncFunction异步函数isDate时间isMapMapisSetSetisWeakMapWeakMapisWeakSetWeakSetisGenerator生成器isGeneratorFunction生成器函数isBigInt64ArrayBigInt64ArrayisBigUint64ArrayBigUint64ArrayisDataViewDataViewisArrayBufferArrayBufferisRegExp正则、RegExp对象构建的正则isSharedArrayBufferSharedArrayBufferisUint8ClampedArrayUint8ClampedArrayisInt8ArrayInt8ArrayisUint8ArrayUint8ArrayisTypedArrayTypedArrayisInt16ArrayInt16ArrayisUint16ArrayUint16ArrayisInt32ArrayInt32ArrayisUint32ArrayUint32ArrayisFloat32ArrayFloat32ArrayisFloat64ArrayFloat64ArrayisIntlCollatorIntl.CollatorisIntlDateTimeFormatIntl.DateTimeFormatisIntlDisplayNamesIntl.DisplayNamesisIntlListFormatIntl.ListFormatisIntlLocaleIntl.LocaleisIntlNumberFormatIntl.NumberFormatisError错误isEvalErroreval 错误isRangeErrorrange 错误isReferenceErrorreference 错误isSyntaxErrorsyntax 错误isTypeErrortype 错误isURIErroruri 错误isAggregateErroraggregate 错误
状态
此软件包是 MrMudBean 生态系统的一部分。
它使用严格的 TypeScript 编写,并通过 Rollup 构建进行验证。
虽然单元测试较少,但 API 稳定,并在生产环境中大量使用。
