npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@iri-huabei/common-utils

v1.0.1

Published

This is a common utils library

Downloads

4

Readme

公共方法库(维护项目工具方法)

基于rollup+ts搭建的方法库,抽离常用方法封装,方便在项目中导入使用

如何使用?

  • 在html中直接引入的方式
    <script src="./dist/iri-common-utils.min.js"></script>
  • 在工程项目中引入
    1. 在iri-common-utils目录下执行命令:
      npm link
    2. 在工程项目主文件夹下执行命令:
      npm link iri-common-utils
    3. 项目文件中导入相关方法:
      //es6导入
      import {...} from 'iri-common-utils' 
      
      //在nodejs中使用
      const {...} = require('iri-common-utils')

方法目录

方法使用

getType (获取参数类型)

  • 参数

    • item <any> 传入的参数
  • 返回值

    • data <string>
  • example
    input:

        import { getType } from 'iri-common-utils'
        const s = 'hello world'
        getType(s)

    output:

    string

diffTime (计算时间间隔,并返回字符串格式)

  • 参数

    • duration <number> 时间间隔单位ms
  • 返回值

    • data <object>
      • dur <number> 剩余毫秒数
      • strTime <string> 字符串形式间隔时间
  • example
    input:

        import { diffTime } from 'iri-common-utils'
        const duration = 100000000
        diffTime(duration)

    output:

    { dur: 0, strTime: '1天3时46分40秒' }

deepClone (数据深拷贝,一般用于拷贝引用类型数据)

  • 参数

    • target <any> 原始数据对象
  • 返回值

    • data <any>
  • example
    input:

        import { deepClone } from 'iri-common-utils'
        const obj ={a:'1', b:'2'}
        deepClone(obj)

    output:

    {a:'1', b:'2'}

debounce (用于函数防抖,一般用于搜索防止多次请求)

  • 参数

    • fn <function> 执行函数
    • delay <number> 间隔时间, 非必传,默认 500ms
    • immediate <boolean> 是否立即执行, 非必传, 默认 false
  • 返回值

    • func <function> 返回一个闭包,用于调用执行及传参
  • example
    input:

        import { debounce } from 'iri-common-utils'
        const fn = () => {console.log('aaaaa')}
        debounce(fn,1000)()

    output:

    .....

throttle (用于函数节流,规定时间内执行一次)

  • 参数

    • fn <function> 执行函数
    • delay <number>间隔时间, 非必传,默认 500ms
  • 返回值

    • func <function> 返回一个闭包,用于调用执行及传参
  • example
    input:

        import { throttle } from 'iri-common-utils'
        const fn = () => {console.log('aaaaa')}
        throttle(fn,1000)()

    output:

    .....

transformValue (转化服务器返回值)

  • 参数

    • oldData <array> 服务器返回数据
    • mapField <object>
      • labelField <string> 转化label所需要的字段
      • valueField <string> 转化value所需要的字段
  • 返回值

    • newData <array>
  • example
    input:

        import { transformValue } from 'iri-common-utils'
        const arr = [
              { name: 'aa', age: 18 },
              { name: 'bb', age: 19 },
              { name: 'cc', age: 20 },
              { name: 'dd', age: 21 },
        ]
        transformValue(arr,{ labelField: 'name', valueField: 'age' })

    output:

    [
    { label: 'aa', value: 18 },
    { label: 'bb', value: 19 },
    { label: 'cc', value: 20 },
    { label: 'dd', value: 21 },
    ]

transformArraytoTree (转化服务器返回值)

  • 参数

    • data <array> 原始数据
    • mapField <object>
      • childFiled <string> 子节点字段名称,非必传,默认值 children
      • parentField <string> 父节点字段名称,非必传,默认值 pid
      • idField <string> 当前节点的id,非必传,默认值 id
      • defaultNoParentId <string> 默认无父级的id,非必传,默认值 0
  • 返回值

    • newData <array>
  • example
    input:

        import { transformArraytoTree } from 'iri-common-utils'
        const arr = [
              { title: 'aaaa', id: 1, pid: 0 },
              { title: 'aaaa', id: 2, pid: 3 },
              { title: 'aaaa', id: 3, pid: 0 },
              { title: 'aaaa', id: 4, pid: 3 },
              { title: 'aaaa', id: 5, pid: 4 },
              { title: 'aaaa', id: 6, pid: 5 },
        ]
        transformArraytoTree(arr)

    output:

    [{"title":"aaaa","id":1,"pid":0},{"title":"aaaa","id":3,"pid":0,"children":[{"title":"aaaa","id":2,"pid":3},{"title":"aaaa","id":4,"pid":3,"children":[{"title":"aaaa","id":5,"pid":4,"children":[{"title":"aaaa","id":6,"pid":5}]}]}]}]

transformTreeToArray (转化服务器返回值)

  • 参数

    • data <array> 原始数据
    • defaultChildField <string> 子节点字段名称,非必传,默认值 children
  • 返回值

    • newData <array>
  • example
    input:

        import { transformTreeToArray } from 'iri-common-utils'
        const arr = [{"title":"aaaa","id":1,"pid":0},{"title":"aaaa","id":3,"pid":0,"children":[{"title":"aaaa","id":2,"pid":3},{"title":"aaaa","id":4,"pid":3,"children":[{"title":"aaaa","id":5,"pid":4,"children":[{"title":"aaaa","id":6,"pid":5}]}]}]}]
        transformTreeToArray(arr)

    output:

    [
    { title: 'aaaa', id: 1, pid: 0 },
    { title: 'aaaa', id: 2, pid: 3 },
    { title: 'aaaa', id: 3, pid: 0 },
    { title: 'aaaa', id: 4, pid: 3 },
    { title: 'aaaa', id: 5, pid: 4 },
    { title: 'aaaa', id: 6, pid: 5 },
    ]

validateField (用于验证 身份证,护照,数字,手机号,邮箱等,可自定义)

  • 参数

    • data <string> 验证字符串
    • options <object>
      • dType <string> 非必传,'number' | 'IDCard' | 'mobile' | 'passport' | 'email'
      • regex <Regex> 自定义正则表达式,非必传,优先级比 dType 更高
  • 返回值

    • value <boolean> 返回布尔值
  • example
    input:

        import { validate } from 'iri-common-utils'
        const str = '13065218789'
        console.log(validate(str,{dType: 'mobile'})) 

    output:

    true

isEmptyArray (验证是否非空数组)

  • 参数

    • data <array> 数据
  • 返回值

    • value <boolean> 返回布尔值
  • example
    input:

        import { isEmptyArray } from 'iri-common-utils'
        const arr = []
        console.log(isEmptyArray(arr) 

    output:

    true

反馈 & 建议

待反馈......