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.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'}
}
let html = await util.http.post(url,postData,opts).catch(err => err);
if (html instanceof Error) {
}else{
}
filesystem
util.fs.mkdir(path)
util.fs.rmrf(path)
util.fs.mv(src, dest)
util.fs.ll(path)
util.fs.readDir(path)
util.fs.stat(path);
util.fs.exist(path)
util.fs.readFile(path)
util.fs.readGzip(path)
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)
util.drop([1, 2, 3, 4, 5])
util.dropRight([1, 2, 3, 4, 5])
util.xor([1, 2, 6], [1, 2, 3, 4, 5])
util.notIn([1, 2, 3, 4, 5],[1, 2, 5, 6])
util.concat([1, 2, 3], [2, 3])
util.compact([0, 1, false, 2, '', 3])
util.flatten([1, [2, [3, [4]], 5]])
util.values([{ 'user': 'barney' }, { 'user': 'fred' }],'user')
util.uniq([{n:1},{n:2},{n:1}] )
util.indexOf([1, 2, 3], 2)
util.findIndex([{ n: 1 }, { n: 2 }, { n: 3 }], { n: 3 })
util.findOne([{ n: 1, b: 1 }, { n: 1, b: 2 }, { n: 3 }], { n: 1 })
util.find([{ n: 1 }, { n: 2, a: 22 }, { n: 3 }], [{ n: 2 }])
util.map([{a:1},{a:2}], 'a')
util.max([1, 2, 3, 4, 5])
util.maxBy([{ n: 1, a: 2 }, { n: 2, a: 5 }, { n: 3, c: 1 }], 'n')
util.min([1, 2, 3, 4, 5])
util.minBy([{ n: 1, a: 2 }, { n: 2, a: 5 }, { n: 3, c: 1 }], 'n')
util.sort([5, 4, 3, 2, 1])
util.sortRandom(arr)
util.kv(arr,key)
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 } })
util.pick({'a': 1, 'b': '2', 'c': 3 }, ['a', 'c'])
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)
string
util.isString(value)
util.toJson(value)
util.uuid()
util.trim(value)
util.formatIp(ip)
util.format(f,...val)
util.querystring(obj)
util.encodeUrl(value)
util.decodeUrl(value)
util.toText(html, length)
util.camelCase(value)
datetime
util.toDate(value)
util.isDate(value)
util.getTimeByObjectId(objectId)
util.formatDate(value)
util.dayTime(date, inc)
util.setDay(date, inc)
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)