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

utils-h

v1.1.4

Published

utils-package for front-end

Downloads

5

Readme

utils-h

utils-h 是一个前端工具包, 包括 qs , debounce ,throttle , LazyLoad , getTime , 等函数或类等 API

download

> yarn add utils-h

import

可以通过此,全部引入

import * as UtilsH from 'utils-h'

console.log(utilsH.throttle)

或者仅引入你需要使用的

import {debounce} from 'utils-h'

let f = debounce(100,cb,true)

use tools

🌀debounce

防抖函数

function debounce(wait: number, fun: Function, immediate?: boolean): Function

  • wait (ms)
  • fun
  • immediate

如果 immediate 是 true , 防抖函数会立即调用 ,默认 false

import { debounce } from 'utils-h'

let cb = function(){
  //...
}

let cbWithDebounce = debounce(200,cb,true)

ele.onclick = cbWithDebounce

🌈throttle

节流函数 function throttle(dur: number, fun: Function, endCall?: boolean): Function

  • dur(ms)
  • fun
  • endCall

如果 endCall 是 true , 讲在末尾再执行一次回调,假如 2s 执行一次,但是你 1 秒时停止了操作,但会执行2次 , 默认 true

import { throttle } from 'utils-h'

let cb = function(){
  //...
}

let cbWithThrottle = throttle(100,cb)

ele.onsroll = cbWithThrottle

🦀LazyLoad

图片懒加载类 未加载真正图片的 img 元素增加 data-lazy 属性 和 data-lazy-src 属性 data-lazy 为 true , data-lazy-src 为图片真正的 src 在 vue 和 react 中,请勿动态添加 data-lazy

constructor

  • constructor(root?:string) root 是 rootEle 的 id 或者 class , 默认值是 document 。rootEle 和 IntersectionObserver.root 是同一个意思

instance methods

  • observe() 在有新的 img 元素增加时,增加新的监听元素。 如果你使用 vue 或者 react , 请确保 img 元素已经渲染

disconnect

停止监听所有 img 元素

<script setup lang="ts">
import { LazyLoad } from 'utils-h4'
import { ref, onMounted } from 'vue'

const lazyload = ref<LazyLoad>(new LazyLoad('.container'))

onMounted(() => {
  lazyload.value.observe()
})

const imgList = ref<string[]>([])
imgList.value = [
  'src1',
  'src2',
  'src3'
]

</script>

<template>
  <div class="container">
    <img v-for="v in imgList" src="./loading.png" alt="" :key="v" data-lazy :data-lazy-src="v">
  </div>
</template>

<style>
.container {
  margin: 100px auto;
  width: 200px;
  height: 250px;
  overflow-x: hidden;
  overflow-y: scroll;
}

img {
  width: 200px;
  height: 200px;
}
</style>

🐻qs

提取 url 中的数据或者设置 search 参数

let {qs} from 'utils-h'

methods

  • parse(url:string) 获取 url 各个部分数据,返回对象
 let parseRes = qs.parse('https://abc.com:80/a/b/?a=1#efg')
 console.log(parseRes)
 /* 
  {
      url:'https://abc.com:80/?a=1#efg'
      scheme: https,
      domain: abc.com,
      port: 80,
      path: /a/b,
      query: a=1,
      hash: efg,
  }
 */
  • getQuery(url:string) 获取 search (query) 参数
let qo = qs.getQuery('http://abc.com?a=1&b=2')
console.log(qo)
/* 
  {
    a:1,
    b:2
  }
*/
  • setQuery(url:string,query:object) 设置 query (search) 参数
  let qo = {
    a:1,
    b:2
  }

  let url = qs.setQuery('http://abc.com',qo)
  console.log(url)
  // http://abc.com?a=1&b=2

🕐getTime

获取时间参数 根据你的格式,返回对应格式的时间

const time = getTime(format)

getTime('yyyy')  // 2023

getTime('mm')  // 03   (month
getTime('m')  // 3   (month

getTime('dd')  // 21   (day
getTime('d')  // 21   (day

getTime('hh:mimi:ss')  // 08:06:36
geTime('h:mi:s') // 8:6:36

getTime('yy/m/d hh:mimi:ss')  // 23/3/21 08:06:36

🐞Validator

校验器,用于表单数据的校验,

const validator = new Validator()

实例方法

  • noEmpty(text) 如果 text 是空字符串或者仅空格,则返回 false ,否则返回 true

  • equal(text1,text2) 严格比较 text1 和 text2

  • objEqual(obj1,ibj2,keys?) 根据 keys 指定比较的对象属性,比较对象是否相等 如果 keys 没有指定,则 keys = Object.keys(obj1)

不指定 keys

let obj1 = {
  a:1,
  b:2
},
obj2 = {
  a:1,
  b:2
}
validator.checkObjEqual(obj1,obj2) // true

指定 keys

let obj1 = {
  a:1,
  b:'2',
  c:{
    d:3
  },
  e:false
},
obj2 = {
  a:1,
  b:false,
  c:{
    d:3
  }
}
// 比较 a 和 c 下的 d 属性
validator.checkObjEqual(obj1,obj2,['a','c.d']) // true
  • len(text,minL,maxL) text 的长度是否在 [minL,maxL] 的范围内,如果在即 true ,否则 false

  • range(num,min,max,loose = false) 检测 num 数值的范围是否在 [min,max] 内 , 如果 loose 为 true ,则 num 可以为一个字符串数值,比如 '123'

validator.range('12',0,12) // false  '12' is string
validator.range('12',0,12,true) // true  '12' will translate to 12
  • isNum(num,loose = false,canbeNaN = false) 检测 num 是否是一个数值,loose 为 true1 ,表示字符串数值('12') 也认为是数值 canbeNaN 如果为 true ,则 NaN 也返回 true 其他情况非数值返回 false

  • phone(text) 根据传入的数值或字符串,检测是否为号码

  • mail(text) 根据传入的字符串,检测是否为邮箱

  • nameCh(text) 根据传入的字符串,检测是否为中文的名字,包括 XXX·XXX·XXX 的格式

  • pw(text,strength = 1) 检测密码的强度 strength = 1 时 , 限制长度不小于 6 strength = 2 时 , 限制长度不小于 8,且必须包含数值和英文 strength = 1 时 , 限制长度不小于 6 ,且必须包含数值,英文和特殊字符(部分特殊字符不支持)

  • addRule(rule:function) 传入一个自定义检测函数 , 返回一个 id

  • clear() 清空所有自定义函数

  • checkByRule(rule,...args:any) 传入 rule , 使用 rule 对应的方法对数据进行检测 rule 可以是自定义函数的 id ,也可以是 isNum ,mail 等字符串

let id = addRule((text)=>{
  return !!text
})
checkByRule(id,'abc') // true

checkByRule('phone','12312312312') // true
  • checkByRules(rules) 对于大量数据需要校验时,rules 只是一个配置,checkByRules 函数会根据配置,主动进行检测 返回一个 promise , 如果所有检测都是 true ,则 resolve 为 true 否则 resolve 为第一个 false 的检测的错误提示

rules 是对象数组,对象包括三个元素 rule , errText , args rule 可以是自定义检测函数的 id ,也可以是 isNum ,phone 等字符串 errText 是抛出的错误提示文字 args 是检测函数的参数数组

const rules = [
      {
        rule: "noEmpty",
        errText: "名字不能为空",
        args: [' asd  ']
      },
      {
        rule: "phone",
        errText: "号码不正确",
        args: ['123asd']
      },
      {
        rule: 'len',
        errText: "长度不符合",
        args: ['123', 2, 4]
      }
    ]

validator.checkByRules(rules).then((r) => {
      // all true
    }, err => {
      // first err
      // err === 号码不正确
    })