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

co-util

v2.1.15

Published

A collection of utilities for nodejs.

Downloads

326

Readme

co-util

A collection of utilities for nodejs.

Install

npm i co-util

Usage

const util=require('co-util');

http

util.http.setProxy('http://ip:port')

设置全局代理

util.http.defaults.headers.common['Authorization'] = AUTH_TOKEN

设置全局配置

util.http.get(url[,data][,options])

建立一个http get请求,url为请求地址,data为请求数据,可省略 The optional options object may contain:

  • headers (object?, default 'null'): header头,如:{Authorization:'Bearer ${token}'}
  • httpAgent (object?, default 'null'): define a custom agent to be used when performing http and https requests, respectively, in node.js. This allows options to be added like keepAlive that are not enabled by default.
  • timeout (number?, default 0): header头,如:{'Content-Type':'application/x-www-form-urlencoded'}

util.http.post(url,data[,options])

建立一个http post请求,url为请求地址,data为请求数据,可省略,options 同上,默认为application/json

let html = await util.http.get(url).catch(err => err);

let opts={
    headers:{'Content-Type':'application/x-www-form-urlencoded'}
    //默认为application/application/json
}

let html = await util.http.post(url,postData,opts).catch(err => err);
if (html instanceof Error) {
    //出错
}else{
    //To Do...
}

filesystem

util.fs.mkdir(path) //创建文件夹,同时创建上层目录
util.fs.rmrf(path) //删除整个目录包括子目录
util.fs.mv(src, dest) //移动文件
util.fs.ll(path) //获取所有文件和目录 返回 [{fullname:'',filename:'',stat:{}}]
util.fs.readDir(path) //获取当前目录下的文件夹和文件
util.fs.stat(path); //获取文件状态
util.fs.exist(path) //文件是否存在
util.fs.readFile(path) //读文件
util.fs.readGzip(path) //读gzip文件
util.fs.writeFile(path, body); //写入文件
util.fs.writeFile(path, str, { flag: 'a' }); //追加写入
util.fs.cp(src,dist,mode=0); //追加写入

array

util.chunk(['a', 'b', 'c', 'd'],2) //[2, 3, 4, 5]
util.drop([1, 2, 3, 4, 5]) //[2, 3, 4, 5]
util.dropRight([1, 2, 3, 4, 5]) //[1, 2, 3, 4]
util.xor([1, 2, 6], [1, 2, 3, 4, 5])//[6, 3, 4, 5] 异或,去除相同的,然后去重,所有的比较仅和第一个传参比较
util.notIn([1, 2, 3, 4, 5],[1, 2, 5, 6])//[3, 4] 不包含,第一个参数里不包含之后所有参数的数组
util.concat([1, 2, 3], [2, 3]) //[1, 2, 3, 2, 3]
util.compact([0, 1, false, 2, '', 3]) //[1, 2, 3] 去除数组里空值
util.flatten([1, [2, [3, [4]], 5]]) //[1, 2, [3, [4]], 5] 把数组变平
util.values([{ 'user': 'barney' }, { 'user': 'fred' }],'user') //['barney', 'fred']获取集合里某个key对应的value数组
util.uniq([{n:1},{n:2},{n:1}] ) //[{n:1},{n:2}]去重
util.indexOf([1, 2, 3], 2) //1
util.findIndex([{ n: 1 }, { n: 2 }, { n: 3 }], { n: 3 }) //2
util.findOne([{ n: 1, b: 1 }, { n: 1, b: 2 }, { n: 3 }], { n: 1 }) //{ n: 1, b: 1 }
util.find([{ n: 1 }, { n: 2, a: 22 }, { n: 3 }], [{ n: 2 }])  //[{ n: 2, a: 22 }]
util.map([{a:1},{a:2}], 'a') //[1,2]
util.max([1, 2, 3, 4, 5])   //5
util.maxBy([{ n: 1, a: 2 }, { n: 2, a: 5 }, { n: 3, c: 1 }], 'n')  //{ n: 3, c: 1 }
util.min([1, 2, 3, 4, 5]) //1
util.minBy([{ n: 1, a: 2 }, { n: 2, a: 5 }, { n: 3, c: 1 }], 'n') //{ n: 1, a: 2 })
util.sort([5, 4, 3, 2, 1]) //[1, 2, 3, 4, 5]
util.sortRandom(arr) //数组随机,不改变arr的值
util.kv(arr,key) //把集合转化成以key为主键的对象,kv键值对,[{id:"key1",b:2},{id:"key2",b:3}]=>{"key1":{id:"key1",b:2},"key2":{id:"key2",b:3}}

object

util.is(value,other) //两个对象或者数组是否相等
util.isObject(value)
util.isPlainObject(value)
util.isJson(str)
util.isNullObj(obj)
util.extend(obj,[obj2...])
util.merge(obj,[obj2...])
util.formatObj(obj)
util.sortObj(obj)
util.sign(obj, key) //获取签名
util.default({ 'a': { 'b': 2 } }, { 'a': { 'b': 1, 'c': 3 } }) // { 'a': { 'b': 2, 'c': 3 } } 设置默认值,对没有设置过的option,给一个默认值,深度设置
util.pick({'a': 1, 'b': '2', 'c': 3 }, ['a', 'c']) //返回指定属性的对象 { 'a': 1, 'c': 3 }

number

util.isInt(value)
util.isNumber(value)
util.random(start,end)
util.round(value)
util.toInt(str)
util.toNumber(value)
util.formatMoney(value)
util.toFixed(num, bit) //0=>0,10.22=>10.2,9=>9

string

util.isString(value) //是否字符串类型
util.toJson(value) //字符串转化为json,如果转换失败则为空
util.uuid() //生成uuid
util.trim(value) //去除两边空格
util.formatIp(ip) //ip格式化
util.format(f,...val) //字符格式化
util.querystring(obj) //query string,value值进行encodeUrl
util.encodeUrl(value) //url encode
util.decodeUrl(value) //url decode
util.toText(html, length) //html转成text
util.camelCase(value) //返回驼峰命名

datetime

util.toDate(value)
util.isDate(value)
util.getTimeByObjectId(objectId) //把mongo的Object转成datetime
util.formatDate(value)
util.dayTime(date, inc) //保持到00:00:00
util.setDay(date, inc) //弃用,请使用dayTime
util.addDate(date, type, inc)
util.addSecond(date, inc)
util.addMinute(date, inc)
util.addHour(date, inc)
util.addDay(date, inc)
util.fromNow(start) //时差 (之前,现在为基准)

zip

util.zip(src, dist, opts) 压缩文件

Options相关参数

  • 'src' - string类型,源文件
  • 'dist' - string类型,目标路径
  • 'opts' - object类型,选项
    • 'ignore' - array<string>忽略文件夹或者文件 如:["/node_modules/", "**/ .log", "* /tmp/ ", "/*.zip"]

util.unzip(src, dist) 解压覆盖文件

other

await util.sleep(100) //等待100ms