0.1.0 • Published 2 years ago

@naturefw/nf-tool v0.1.0

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

nf-typeof-验证 JavaScript 的类型

源码目录:https://naturefw.gitee.io/

介绍

使用 Object.prototype.toString.call(val) 验证 JavaScript 的各种数据类型。
封装一下 console.log,console.time、console.timeEnd

虽然采用了 Typescript,但是基本上是 anyscript。

目录结构

  • lib 源码
  • src 使用demo
  • distp 在线演示的代码

安装教程

  1. npm i @naturefw/nf-tool 或者
  2. yarn add @naturefw/nf-tool

CDN 的使用方法

引入

  <script src="https://unpkg.com/@naturefw/nf-tool"></script>

然后

nfTool.isPromise(()=>{})

这样就可以访问到了。

使用说明

import tool from '@naturefw/nf-tool'

const {
  log, // 打印调试信息
  logTime, // 计时
  toTypeString, // Object.prototype.toString.call(val)
  typeName, // 获取可以识别的名称

  hasOwnProperty,
  hasOwn,

  isFunction, // 验证普通函数
  isAsync, // 验证 async 的函数
  isPromise, // 验证 Promise
  isObject, // 验证 Object
  isArray, // 验证数组
  isString, // 验证字符串
  isNumber, // 验证 number
  isBigInt, // 验证 BigInt
  isBoolean, // 验证 布尔
  isRegExp, // 验证正则类型
  isDate, // 验证日期
  isMap, // 验证 map
  isSet, // 验证 set
  isSymbol, // 验证 Symbol
 
  isNullOrUndefined // null 或者 undefined 返回 true
} = tool

async function testAsyncFun(params) {
  
}

function myPromise(params) {
  return new Promise((f1, f2) => {
    f1()
  })
}

window.__showlog = 2 // 设置后才会打印调试信息

// 开始测试
const start = () => {

  log(' ============ 验证数据类型 ============')

  let t = logTime('验证 Function 用时')
  log('\n() => {}:', isFunction(() => {}))
  t.end()

  t = logTime('验证 Async 用时')
  log('\nasync () => {}:', isAsync(async () => {}))
  log('async function () {}:', isAsync(testAsyncFun))
  t.end()
  
  t = logTime('验证 isPromise 用时')
  log('\nPromise:', isPromise(myPromise))
  log('Promise:', isPromise(myPromise()))
  t.end()

  t = logTime('验证 Object 用时')
  log('\n对象:', isObject({}))
  t.end()

  t = logTime('验证 Array 用时:')
  log('\n数组:', isArray([]))
  t.end()

  t = logTime('验证 isString 用时:')
  log('\n‘123’:', isString('123'))
  t.end()

  t = logTime('验证 isNumber 用时:')
  log('\n123:', isNumber(123))
  t.end()
  
  t = logTime('验证 isBigInt 用时:')
  log('\n123:', isBigInt(123))
  t.end()
  
  t = logTime('验证 isBoolean 用时:')
  log('\ntrue:', isBoolean(true))
  t.end()
  
  t = logTime('验证 isRegExp 用时:')
  log('\nRegExp:', isRegExp(new RegExp(/123/g)))
  t.end()
  
  t = logTime('验证 isDate 用时:')
  log('\nDate:', isDate(new Date()))
  t.end()
  
  t = logTime('验证 isMap 用时:')
  log('\nMap:', isMap(new Map()))
  t.end()
  
  t = logTime('验证 isSet 用时:')
  log('\nSet:', isSet(new Set()))
  t.end()
  

  t = logTime('验证 isPromise 用时:')
  // log('\nPromise:', isPromise(() => {return new Promise(()=>{},()=>{}))})
  t.end()

  t = logTime('验证 isSymbol 用时:')
  log('\nSymbol(‘123’):', isSymbol(Symbol('123')))
  t.end()
  
  var un
  t = logTime('验证 isNullOrUndefined 用时:')
  log('\nnull:', isNullOrUndefined(null))
  log('未定义:', isNullOrUndefined(un))
  log('字符串:', isNullOrUndefined('qwe'))
  t.end()
  
  const arr = [
    '字符串',
    123,
    ['数组', 123],
    new Date(),
    new Date().valueOf(),
    new Map(),
    new Set()
  ]

  t = logTime('typeName 用时:')
  log('\n typeName 的测试')
  arr.forEach(element => {
    log(typeName(element), ':', element.toString())
  })
  t.end()

源码

自然框架源码/nf-tool- JavaScript 常用函数库

在线演示

https://naturefw-code.gitee.io/nf-rollup-tool

参与贡献

  1. Fork 本仓库
  2. 新建 Feat_xxx 分支
  3. 提交代码
  4. 新建 Pull Request

特技

  1. 使用 Readme_XXX.md 来支持不同的语言,例如 Readme_en.md, Readme_zh.md
  2. Gitee 官方博客 blog.gitee.com
  3. 你可以 https://gitee.com/explore 这个地址来了解 Gitee 上的优秀开源项目
  4. GVP 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目
  5. Gitee 官方提供的使用手册 https://gitee.com/help
  6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 https://gitee.com/gitee-stars/