a-type-of-js
TypeScript icon, indicating that this package has built-in type declarations

0.2.0 • Public • Published

a-type-of-js

version Coverage Status codecov bug 🙋‍♂️ 提交

一个简单的类型判断工具

使用

import { typeOf } from 'a-type-of-js';

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 'a-type-of-js';

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'

en,且当是脱裤子放屁吧

import { isType } from 'a-type-of-js';

interface Person {
    name: string;
    age: number;
}


function doSomething(value: string | number | boolean | ) {

    /// use judgment as a type judgment
    if (isType<Person>(value, () => (value && value.name === 'earthnut' && value.age === 18))) {
      console.log('value is Person');
      return;
    }

    /// as tisType
    if (isType<string>(value)) {
      value.toLocaleUpperCase();
      return;
    }

    /// use judgment

    if (isType<boolean>(value, Boolean(value) === true )) {
      console.log('value is true');
      return;
    }
}

文档提供类型检测

  • isString 字符串、String 对象构建的字符串
  • isNumber 数字、Number 对象构建的数字
  • isBoolean 布尔值、Boolean 对象构建的布尔值
  • isNull null
  • isUndefined undefined
  • isNaN NaNNaN 是一个特殊的数值 NaN !== NaN 即便 typeof NaN 返回的是 number
  • isFunction 函数
  • isArray 数组、Array 对象构建的数组
  • isEmptyString 空字符串
  • isBusinessEmptyString 空字符串(去除首尾空格)
  • isSymbol symbol
  • isBigInt 大整数
  • isPlainObject 对象(普通对象,非其他内置对象类型)
  • isPromise Promise
  • isAsyncFunction 异步函数
  • isDate 时间
  • isMap Map
  • isSet Set
  • isWeakMap WeakMap
  • isWeakSet WeakSet
  • isGenerator 生成器
  • isGeneratorFunction 生成器函数
  • isBigInt64Array BigInt64Array
  • isBigUint64Array BigUint64Array
  • isDataView DataView
  • isArrayBuffer ArrayBuffer
  • isRegExp 正则、RegExp 对象构建的正则
  • isSharedArrayBuffer SharedArrayBuffer
  • isUint8ClampedArray Uint8ClampedArray
  • isInt8Array Int8Array
  • isUint8Array Uint8Array
  • isTypedArray TypedArray
  • isInt16Array Int16Array
  • isUint16Array Uint16Array
  • isInt32Array Int32Array
  • isUint32Array Uint32Array
  • isFloat32Array Float32Array
  • isFloat64Array Float64Array
  • isIntlCollator Intl.Collator
  • isIntlDateTimeFormat Intl.DateTimeFormat
  • isIntlDisplayNames Intl.DisplayNames
  • isIntlListFormat Intl.ListFormat
  • isIntlLocale Intl.Locale
  • isIntlNumberFormat Intl.NumberFormat
  • isError 错误
  • isEvalError eval 错误
  • isRangeError range 错误
  • isReferenceError reference 错误
  • isSyntaxError syntax 错误
  • isTypeError type 错误
  • isURIError uri 错误
  • isAggregateError aggregate 错误

文档地址

参看 https://earthnut.dev/a-type-of-js/

Readme

Keywords

Package Sidebar

Install

npm i a-type-of-js

Weekly Downloads

238

Version

0.2.0

License

ISC

Unpacked Size

124 kB

Total Files

44

Last publish

Collaborators

  • earthnut