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

yu-util

v1.3.1

Published

前后端通用的函数

Downloads

11

Readme

yu-util

前后端通用的函数封装:用于web前端和nodejs端

安装

npm install yu-util --save

引入

const { throttle, listener, intercept, merge, copy, getType, toJSON } = require('yu-util');
//或
import { throttle, listener, intercept, merge, copy, getType, toJSON } from 'yu-util' 

throttle(callback,ms)防抖节流函数,给高频率事件触发设置的节流阀:

//执行throttle会返回一个闭包函数
let fn = throttle(function () {
	console.log(1)
}, 1000)

//执行fn要间隔1000毫秒
//第一次执行fn:
fn();				//打印1
//第二次执行fn:因为和第一次执行的时间没有间隔1000毫秒,所以第二次无效。
fn();				//没有打印

//1、执行fn就是执行throttle的callback
//2、给fn传参数就是给throttle的callback传参数
//3、throttle的callback里的this指向fn的调用者
//比如:
let fn = throttle(function (obj) {
	console.log(obj)		//打印结果:{ id:1 }
}, 1000)

fn({ id:1 });

listener(obj,attrs,callback):对象属性监听

//listener(对象, 需要监听的对象属性, 监听函数)
obj = listener({},['id'],(key,val)=>{
	console.log(1);
});

o = listener(obj,['id','name'],(key,val)=>{
	console.log(2);
});
o.id = 11; //触发两个监听函数,分别打印1,2
o.name = 'xiao'; //触发一个监听函数,打印2

//监听所有属性
obj = listener({},'*',(key,val)=>{
	console.log(1);
});
obj.age = 22;		//触发监听函数,打印1

intercept(obj,attrs,callback,bool):对象属性拦截

//intercept(对象, 需要拦截的对象属性, 拦截函数, 是否覆盖之前的拦截函数)
obj = intercept({},['id','name'],(key,val)=>{
	//拦截函数
	return val+1;
});
obj.id = 2;   //给obj的id或name赋值时,触发拦截函数
console.log(obj.id);  //3

//拦截所有属性
obj = intercept({},'*',(key,val)=>{
	return val+1;
});
obj.age = 11;
console.log(obj.age);  //12


//第四个参数:bool 是否覆盖之前的拦截函数
o = intercept(obj,['id'],(key,val)=>{
	return val+10;
},true);
//true则会覆盖之前给id设置的拦截函数,false则不能覆盖

o.id = 2;
console.log(o.id)  //12

merge(o1,o2,bool):两个对象合并

let o1 = { id:1,a:{name:'a1',arr:[1,2]},age:22 }
let o2 = { id:2,a:{name:'a2',arr:[2,3],n:11} }
let o = merge(o1,o2);
console.log(o);		//{ id:1,a:{name:'a1',arr:[1,2,3],n:11},age:22 }

//覆盖式合并
let o = merge(o1,o2,true);
console.log(o);		//{ id:2,a:{name:'a2',arr:[1,2,3],n:11},age:22 }

copy(obj):深度复制对象或数组

//深度复制对象:
let obj1 = {n:'a',arr:[1,2,{n:'b'}] };
let obj2 = copy(obj1);
console.log(obj1===obj2); //false
console.log(obj1.arr===obj2.arr); //false
console.log(obj1.arr[2]===obj2.arr[2]); //false

//深度复制数组:
let arr1 = [2,{n:'b'},3,[1,4,5,{n:'a',arr:[1,2,3]}]];
let arr2 = copy(arr1);

getType(variate,type):获取变量的类型

getType([]);  //Array
getType({});  //Object
getType(new Date());  //Date
getType('');  //String
getType(11);  //Number
getType(null);  //Null
getType(undefined);  //Undefined
function fn(){};  getType(fn);  //Function
function fn(){ return getType(arguments); }  fn();  //Arguments

//直接返回判断结果
getType([],'Array');  //true
getType({},'Object');  //true
getType(new Date(),'Date');  //true
getType('','String');  //true
getType(11,'Number');  //true
getType(null,'Null');  //true
getType(undefined,'Undefined');  //true
getType(()=>{},'Function');  //true

遍历对象:each(obj,callback)

let obj = {a:2,b:8};
each(obj,(key,val)=>{
    console.log(key+'-'+val);   //a-2  b-8
});

异步遍历对象:asyncEach(obj,callback,ms)

let obj = {a:2,b:8};
asyncEach(obj,(key,val)=>{
    console.log(key,val);
}, 2000);           //每2000毫秒异步执行一次回调函数

扩展Array静态方法,冒泡排序:

let arr = ['b',1,'c',3,'a',2];
let arr1 = Array.bubble(arr)
console.log(arr1)   //['a','b','c',1,2,3]

遍历数组:Array.prototype.each(callback)

let arr = ['a','b','c'];

//each函数是深度复制本数组,用这个复制品做循环遍历
arr.each((val,i,array)=>{
	arr.shift();  //删除数组的元素,不会影响遍历
	console.log(array);   //array是arr的深度复制品
})

异步遍历数组:Array.prototype.asyncEach(callback, ms)

let arr = [1,2,3,4,5,6];

//asyncEach函数是深度复制本数组,用这个复制品做循环遍历
arr.asyncEach((val,i,array)=>{
    arr.shift();  //删除数组的元素,不会影响遍历
		console.log(array);   //array是arr的深度复制品
}, 2000);       //每隔2000毫秒异步执行一次回调函数

toJSON(str):将对象字符串转为js对象,如果转换失败则返回原字符串

let obj = toJSON('{"name":"xiaoming"}'); //{ name:'xiaoming' }