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

xiaoxiong-tool

v1.0.7

Published

这个库提供了前端开发常用的工具函数

Downloads

190

Readme

这个库提供了前端开发常用的工具函数

index.js文件是入口文件

安装

npm命令:npm install xiaoxiong-tool
yarn命令:yarn add xiaoxiong-tool

使用例子

先在代码里引入

import {isGreaterThan} from 'xiaoxiong-tool'  

调用

isGreaterThan(2, 1);  // 返回true  

注意事项

优先使用import方式引入,require方式没测过

api介绍

EventBus类

用于消息通信,通常用单个实例即可。
创建实例

const eventBus = new EventBus();

方法如下:

on方法

为一个事件添加一个回调函数,定义如下:

on(eventName, callback)

参数介绍:
eventName 事件名称,必填
callback 回调函数,必填

使用示例:

eventBus.on("event", (name) => {
  console.log('event触发', name);
})

off方法

为一个事件移除一个回调函数,定义如下:

off(eventName, callback)

参数介绍:
eventName 事件名称,必填
callback 要移除的回调函数的引用,选填。如果不填,则移除该事件所有的回调函数。

使用示例:

function myCallback(name) {
  console.log(name);
}
eventBus.on('event', myCallback);
eventBus.off("event", myCallback);

emit方法

抛出一个事件,即顺序执行该事件的回调函数,定义如下:

emit(eventName, ...args)

参数介绍:
eventName 事件名称,必填
args 打包参数,包含所抛出事件的所有参数

使用示例:

eventBus.emit('event', 'Hello World!');

防抖节流

debounce函数

用于限制函数执行的频率。要执行一个函数时,并不会立刻执行,而是延迟时间n后才执行。若定时器存在期间要再次执行,则重置定时器。实际执行完成后会清空定时器。函数定义如下:

function debounce(func, delay)

参数介绍:
func 要执行的函数
delay 延迟时间,单位毫秒
返回值:
一个新函数

使用示例:

function sayHello(name) {
  console.log("Hello, " + name);
}
const newFunc = debounce(sayHello, 500);
newFunc('张三');    // 不会打印
newFunc('李四');    // 会打印"李四"

throttle函数

用于限制函数执行的频率。执行一个函数后,则在规定时间内不能再次执行该函数。函数定义如下:

function throttle(func, wait)

参数介绍:
func 要执行的函数
wait 规定的不能再次执行函数的时间,单位毫秒
返回值:
一个新函数

使用示例:

function sayHello(name) {
  console.log("Hello, " + name);
}
const newFunc = throttle(sayHello, 500);
newFunc('张三');    // 会打印"张三"
newFunc('李四');    // 不会打印"李四"

深拷贝

deepCopy函数

用于深度复制并返回一个对象。函数定义如下:

function deepCopy(obj)

参数介绍:
obj 要复制的对象
返回值:
一个新对象

使用示例:

let obj = {
  name: '张三',
  getName() {
    return this.name;
  }
}
const objCopy = deepCopy(obj);
console.log(obj === objCopy);   // 输出false
console.log(obj.name === objCopy.name);   // 输出true
console.log(obj.getName === objCopy.getName);   // 输出false

其他

这是一些零碎的工具函数。

isEmpty函数

判断一个变量是否为空,空值包括undefined、null、空字符串。方法定义如下:

function isEmpty(variable)

参数介绍:
variable 变量名称,也可以是js表达式
返回值:
返回true或者false

使用示例:

let a = null;
isEmpty(a);   // 返回true