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

union-util

v1.0.63

Published

the util in node in union data center

Downloads

2

Readme

union-util

一个工具库,包含有DateArrayFilePath等不同类型的一些处理,便于开发。例如对于Array有不同的排序、异步遍历等操作。

使用

npm install union-util --save

union-util/array

walk(arr:any[],cal: Function)

遍历一个数组,并返回值给对应的回调函数进行处理,这里的回调函数可以是异步的Promise(async))

walkcb(arr:any[],cal:Function)

同walk 不过,,可能存在的情况是回调函数嵌套回调函数而不是(async),这里相当于将回调函数的回调使用上了

let arr = [1,2,3,4];
walkcb(arr,async function(value,index,cb){
    await someasync(param1,async function(){
        await cb();
    })
})

join(arr:any[],symbol:string,begin:number,end: number)

同array的join,这里可以指定位置

reduce(arr : any[], func : (value? : any, index? : number, v? : number) => any)

一个归并操作,同Array.reduce,这里不同的是,Function可以是一个一步操作

map(arr : any[], func : (value? : any, index? : number) => any

遍历操作,同Array.map,不同的是这里支持异步的遍历方法

find(arr : any[], func : (value? : any, index? : number) => any)

查找操作,同Array.find,同样的,这里也是支持异步操作

findPro(arr:any[],func:(value?:any,index?:number,extra?: FindPExtraValue)=>any)

同上find,不过,有的时候可能在查找的时候需要自定义一些中间值进行存储与共享,因此在这里加入了extra选项

extra的定义

export interface FindPExtraValue {
    findIndex?: number; 
    index: number;
    [prop: string]: any;
};
  1. findIndex 表示的是实际find最后找到的那个下标
  2. 其他的都可以用户进行自己定义,比如index可以表示实际的需要的位置

主要就是一个简单的存储,可以让每个遍历中共享数据。

union-util/file

walk(src:string,onFile:Function,onDirectory:Function)

遍历某个文件夹,并根据对应的文件和文件夹触发对应的事件或者方法

exists(filename : string): Promise<fs.Stats>

判断某个文件是否存在,并返回最终的文件状态,可以根据文件状态查看是否是文件还是文件夹。

existsSync(filename : string) : fs.Stats

exists,不过这里是异步的

fileExists(filename : string): Promise

判断文件是否存在(一定得是文件)

fileExistsSync(filename : string): boolean

以同步的方式判断是否存在文件

directoryExists(filename : string): Promise

判断对应文件夹是否存在,返回一个promise

directoryExistsSync(filename : string): boolean

以同步的方式判断文件夹是否存在

union-util/interface

getAllIps():Array

返回本机所有的IP地址

union-util/merge

normal(obj1 : any, obj2 : any)

浅复制

deep(obj1:any,obj2:any)

深度复制(支持循环引用的复制)

union-util/sort

exchange(res:any[],i:number,j:number)

交换一个数组的i,j位置的值

order(arr:any[],ifasyn:boolean = true,ifnew: boolean = false,getValue?: Function)

默认的一个排序,默认采用的是归并排序

orderByKey(arr : any[], key : string, ifasync : boolean,ifnew:boolean)

通过数组内的对象的某个键值去排序

quick(arr : any[], ifasync : boolean = true, ifUseNew : boolean = false, getValue?: Function)

快速排序,从左向右的方式遍历

quickPro(arr : any[], ifasync : boolean = true, ifUseNew : boolean = false, getValue?: Function)

快速排序,同前面的不同的是,这里是从右边向左边遍历,速度会更快,效率也更高

heap(arr : any[], ifasync : boolean = true, ifUseNew : boolean = false, getValue?: Function)

堆排序

merge(arr : any[], ifasync : boolean = true, ifUseNew : boolean = false, getValue?: Function)

归并排序

bubble(arr : any[], ifUseNew : boolean = false, getValue?: Function, ifasync : boolean = true)

冒泡排序

union-util/promise

all(arr:any[])

Promise.all

allSync(arr:any[])

Promise.all是异步,这里是同步,就是按顺序执行