# libcu **Repository Path**: onlyyyy/libcu ## Basic Information - **Project Name**: libcu - **Description**: 各语言常用方法基本库 library for common use - **Primary Language**: Unknown - **License**: GPL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 7 - **Forks**: 1 - **Created**: 2020-06-04 - **Last Updated**: 2025-04-16 ## Categories & Tags **Categories**: utils **Tags**: None ## README # libcu #### 介绍 用各种语言实现的基本库 library for common use # Nodejs部分 ## libcu libcu aim to provide a common use lib for JavaScript libcu致力于提供一个JS的通用方法库 ------ ## example ```js const libcu = require('libcu'); console.log(libcu.tools.sleep(100)); ``` ## APIS & Usage ```js const libcu = require('libcu') ``` ### I.tools - 1.findLackNumArr(arr) Find the missing number in a contiguous array 在连续的数组里面找到缺少的数字 ```js 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 异步睡眠函数 ```js await libcu.tools.sleep(1000); ``` - 3.contains(arr, obj) Judge whether obj is in arr 判断obj是否在arr里面 ([1,2,3] , 1) will return true ```js 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的个数 ```js let a = 1; let b = libcu.tools.padZero(a,3); console.log(b); '001' ``` - 7.padZero(num, len) Hexadecimal left padding zero 十六进制左补零 ```js let a = 1; let b = libcu.tools.padZero16(a,3); console.log(b); '001' ``` - 8.buf2String(buf) ```js 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类似 ```js 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] ```js 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 ```js let a = 1.2; console.log(libcu.tools.acc2Decimal(a)); '1.20' ``` - 12.isNumber(value) ```js let a = '1.4f' console.log(libcu.tools.isNumber(a)); 'false' ``` - 13.safeJsonStringfy(json) ```js let a = "1" console.log(libcu.tools.safeJsonStringfy(a)); '1' ``` - 14.safeJsonParse(json) ```js let a = "1" console.log(libcu.tools.safeJsonParse(a)); '1' ``` - 15.delQuotation(number) ```js let a = `"1"` console.log(libcu.tools.delQuptation(a)); '1' ``` ### II.cf **The main API is the following** - 1.async walkFolder(dirname, option) Get all the files and folders in the directory 获取目录下的所有文件和文件夹 ```js return { fileList : [], dirList : [] } ``` - 2.async copyFolder(srcDir, tarDir, filter) Copy entire directory 复制整个目录 ### III.mysql - 1.init(connect) Tell MySQL connection information 告诉mysql连接信息 ```json connect : { "host" : "127.0.0.1", "user" : "test", "password":"123456", "database" : "mydb", } ``` - 2.async beginTrans() Start a transaction 开启一段事务 usage: ```js try { let conn = await beginTrans(); }catch(error) { } ``` - 3.async dbop(sql, sqlParam) ```js try { let res = await dbop('select * from test'); } catch(error) { } ``` - 4.async dbOpInTrans(sql, sqlParam, connection) - 5.async commit(conn) - 6.async rollback(conn) ```js 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) ```js let a = 1.2 console.log(libcu.cipher.getMd5Buffer(a)); '' ``` - 2.getMd5Str(data) ```js let a = 1.2 console.log(libcu.cipher.getMd5Str(a)); '56765472680401499c79732468ba4340' ``` - 3.getMd5UpperStr(data) ```js let a = 1.2 console.log(libcu.cipher.getMd5UpperStr(a)); '56765472680401499C79732468BA4340' ``` - 4.setAesKey(key) defalut is "/GwhjXbE1SCPaIY=" (AvenirLibcu) - 5.AesEncode(info) - 6.AesDecode(info) ```js 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' ```