libcu
v1.1.5
Published
Nodejs 通用方法库
Downloads
18
Maintainers
Readme
libcu
libcu aim to provide a common use lib for JavaScript
libcu致力于提供一个JS的通用方法库
example
const libcu = require('libcu');
console.log(libcu.tools.sleep(100));
APIS & Usage
const libcu = require('libcu')
I.tools
1.findLackNumArr(arr)
Find the missing number in a contiguous array
在连续的数组里面找到缺少的数字
let a = [1,2,4,6,10]; a = libcu.tools.findLackNumArr(a); console.log(a); '[ 3, 5, 7, 8, 9 ]'
2.sleep(ms)
async sleep
异步睡眠函数
await libcu.tools.sleep(1000);
3.contains(arr, obj)
Judge whether obj is in arr
判断obj是否在arr里面
([1,2,3] , 1) will return true
let a = [1,2,4,6,10]; let b = 3; console.log(libcu.tools.contains(a,b)); 'false'
4.ascii2Hex(buf)
omited
5.hex2Ascii(buf)
omited
6.padZero(num, len)
Left padding zero, len is the number of zeros
左补零,len为补0的个数
let a = 1; let b = libcu.tools.padZero(a,3); console.log(b); '001'
7.padZero(num, len)
Hexadecimal left padding zero
十六进制左补零
let a = 1; let b = libcu.tools.padZero16(a,3); console.log(b); '001'
8.buf2String(buf)
let a = Buffer.alloc(10) a.fill('wtx',0,10); console.log(libcu.tools.buf2String(a)); 'wtxwtxwtxw'
9.readUInt24BE(inBuf, begin, end)
In part, it is similar to readUInt32BE
部分和readUInt32BE类似
let a = Buffer.alloc(10) a.fill('wtx',0,10); let b = libcu.tools.readUInt24BE(a); console.log(libcu.tools.buf2String(b)); '7828600'
10.str2arr(str,flag)
!flag : '[1,2,3]' to [1,2,3]
flag : '["1","2","3"]' to [1,2,3]
let a = '[1,2,3]'; let b = libcu.tools.str2arr(a); console.log(b); `[ '1', '2', '3' ]`
11.acc2Decimal(value)
1.1 to 1.10
1 to 1.00
1.10 to 1.10
let a = 1.2; console.log(libcu.tools.acc2Decimal(a)); '1.20'
12.isNumber(value)
let a = '1.4f' console.log(libcu.tools.isNumber(a)); 'false'
13.safeJsonStringfy(json)
let a = "1" console.log(libcu.tools.safeJsonStringfy(a)); '1'
14.safeJsonParse(json)
let a = "1" console.log(libcu.tools.safeJsonParse(a)); '1'
15.delQuotation(number)
let a = `"1"` console.log(libcu.tools.delQuptation(a)); '1'
16.antiJsonStick(string)
Handle sticky packets, only for JSON type packets.
处理粘包用,仅限于JSON类型报文
let a = '{a:1}{b:2}'; console.log(a); let arr = libcu.tools.antiJsonStick(a); console.log(arr); {a:1}{b:2} [ '{a:1}', '{b:2}' ]
II.cf
The main API is the following
1.async walkFolder(dirname, option)
Get all the files and folders in the directory
获取目录下的所有文件和文件夹
return { fileList : [], dirList : [] }
2.async copyFolder(srcDir, tarDir, filter)
Copy entire directory
复制整个目录
III.mysql
1.init(connect)
Tell MySQL connection information
告诉mysql连接信息
connect : { "host" : "127.0.0.1", "user" : "test", "password":"123456", "database" : "mydb", }
2.async beginTrans()
Start a transaction
开启一段事务
usage:
try { let conn = await beginTrans(); }catch(error) { }
3.async dbop(sql, sqlParam)
try { let res = await dbop('select * from test'); } catch(error) { }
4.async dbOpInTrans(sql, sqlParam, connection)
5.async commit(conn)
6.async rollback(conn)
try { let conn = await beginTrans(); try { await dbOpInTrans(sql,sqlParam,conn); await commit(conn); }catch (error) { await rollback(conn); } }catch(error) { }
IV.cipher
1.getMd5Buffer(data)
let a = 1.2 console.log(libcu.cipher.getMd5Buffer(a)); '<Buffer 56 76 54 72 68 04 01 49 9c 79 73 24 68 ba 43 40>'
2.getMd5Str(data)
let a = 1.2 console.log(libcu.cipher.getMd5Str(a)); '56765472680401499c79732468ba4340'
3.getMd5UpperStr(data)
let a = 1.2 console.log(libcu.cipher.getMd5UpperStr(a)); '56765472680401499C79732468BA4340'
4.setAesKey(key)
defalut is "/GwhjXbE1SCPaIY=" (AvenirLibcu)
5.AesEncode(info)
6.AesDecode(info)
let a = 1.2 let b = libcu.cipher.AesEncode(a); console.log(b); let c = libcu.cipher.AesDecode(b); console.log(c); 'b = rXtFTnkfN1IXmvO94PoeQA==' 'c = 1.2'