Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
@sumor/utils
Advanced tools
轻呈云工具集提供了Node.JS的基础常见工具,方便构建应用。
要求 Node.JS v7.6.0 以上。
npm install @sumor/utils --save
const utils = require("@sumor/utils");
const utils = require("@sumor/utils");//引入utils库
const uuid = utils.basic.uuid();//调用utils库方法
console.log(uuid); //将生成通用唯一识别码
生成通用唯一识别码
const uuid = utils.basic.uuid();
console.log("uuid:",uuid);
MD5消息摘要算法
const md5 = utils.basic.md5("test");
console.log("md5:",md5);
获取时间戳
时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总毫秒数。
const timestamp = utils.basic.getTimestamp();
console.log("timestamp:",timestamp);
延迟事件,将暂停程序执行一段时间,仅供测试使用。
console.log("延迟前");
await utils.basic.delay(1000);
console.log("延迟后");
格式化日期时间,支持不同时区,支持数字格式时间戳和JS Date对象,时区目前仅支持分钟差,不传则使用服务器时区。
utils.basic.formatTime(时间戳,格式[,时区])
const timeForUTC0 = utils.basic.formatTime(timestamp,'yyyy年MM月dd日 hh:mm:ss 星期W');
console.log(`当前英国时间:${timeForUTC0}`);
const timeForUTC8 = utils.basic.formatTime(new Date(),'yyyy年MM月dd日 hh:mm:ss 星期W',8*60);
console.log(`当前北京时间:${timeForUTC8}`);
生成二维码
const qrcode = await utils.basic.qrcode("你好,世界");
console.log("您可拷贝以下黏贴到浏览器地址栏查看二维码");
console.log(qrcode+"\n");
转驼峰写法
utils.basic.toCamelCase(待转文本[,分隔符,是否为小写驼峰])
const upper1 = utils.basic.toCamelCase("say-hello");//SayHello
const lower1 = utils.basic.toCamelCase("say_hello","_",true);//sayHello
驼峰写法转单词切分
utils.basic.fromCamelCase(待转文本[,分隔符])
const upper2 = utils.basic.fromCamelCase("SayHello");//say-hello
const lower2 = utils.basic.fromCamelCase("sayHello","_");//say_hello
生成触发程序
const fn = utils.basic.trigger(程序);
let num = 0;
const fn = utils.basic.trigger(async()=>{
num+=1;
await utils.basic.delay(2000);
console.log(num);
});
fn();fn();fn();//连续触发3次后num = 2
setTimeout(()=>{
fn();
},10000);//延迟触发10秒后num = 3
格式化路径
const normalizePath = utils.file.normalize("C:/level1/level2/../hello.txt");
console.log(normalizePath);
下载文件
await utils.file.download("https://www.baidu.com",__dirname+"/baidu.html");
寻找文件
const fileList = await utils.file.find("*",{cwd:__dirname,ignore:"**/node_modules/**"});
console.log(fileList);
压缩及解压ZIP文件
//创建了一个demo目录,其中包含一个txt文件,一个json文件
const testFolder = __dirname+"/../dist/test";
const demoFolder = __dirname+"/../dist/test/demo";
await utils.file.ensureDir(demoFolder);
await utils.file.writeFile(demoFolder+"/zip.txt","hello zip");
await utils.file.writeFile(demoFolder+"/config.json","{}");
//生成一个压缩包,json文件被忽略,不包含在zip压缩包内
await utils.file.zip(demoFolder,__dirname+"/../dist/test/demo.zip",["*.json*"]);
//解压压缩包至demoUnzip目录
await utils.file.unzip(__dirname+"/../dist/test/demo.zip",testFolder+"/demoUnzip");
检查是否存在文件夹或目录
const existFlag = await utils.file.exist(demoFolder);
console.log("existFlag:",existFlag);
检查路径是否为目录
const isDirectory = await utils.file.isDirectory(demoFolder);
console.log("isDirectory:",isDirectory);
移动文件或目录,也可用于改名
await utils.file.move(__dirname+"/demo.zip",__dirname+"/move/demo_rename.zip");
删除目录或文件
await utils.file.move(__dirname+"/demo.zip");
确保文件存在,如不存在则创建
await utils.file.ensureFile(__dirname+"/demo.zip");
确保目录存在,如不存在则创建
await utils.file.ensureDir(__dirname+"/demo");
复制目录或文件
await utils.file.copy(__dirname+"/demo",__dirname+"/demo_copy");
写入文件
await utils.file.writeFile(__dirname+"/demo.txt","test");
尾部追加写入文件
await utils.file.appendFile(__dirname+"/demo.txt","test");
读取文件,如果不存在则返回undefined 参数2为编码格式,不输入为返回buffer
const fileData = await utils.file.readFile(__dirname+"/demo.txt","utf-8");
console.log(fileData);
读取JSON
const jsonData = await utils.file.readJson(__dirname+"/demo.txt");
console.log(jsonData);
获取后缀名
const extData = await utils.file.getExt(__dirname+"/demo.txt");
console.log(extData);
FAQs
轻呈云Node.JS工具库
The npm package @sumor/utils receives a total of 1 weekly downloads. As such, @sumor/utils popularity was classified as not popular.
We found that @sumor/utils demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.