1.6.0 • Published 2 days ago
@codernote/utils v1.6.0
工具库
笔记软件通用工具库
安装
npm install @codernote/utils
工具函数
uuid: 生成 uuid
import { uuid } from "@codernote/utils";
const id = uuid();
parseMimeType: 解析 mimeType
import { parseMimeType } from "@codernote/utils";
const mimeType = 'video/mp4; codec="avc1.42E01E"';
const parsed = parseMimeType(mimeType);
console.log(parsed);
// 输出: { mainType: 'video', subType: 'mp4', params: ['codec="avc1.42E01E"'] }
判断设备类型
import { detectDeviceType } from "@codernote/utils";
const type = detectDeviceType();
console.log(type);
// 输出: mobile | desktop
获取 url 参数
import { getQueryParam } from "@codernote/utils";
const a = getQueryParam("a");
优化字节大小展示
import { formatBytes } from "@codernote/utils";
const a = formatBytes(1024);
console.log(a);
// 1KB
截取字符串
import { truncateString } from "@codernote/utils";
const a = truncateString("xxxxxx", 3);
console.log(a);
// xxx...
根据 File 获取图片或视频宽高
import { getVideoSize, getImageSize } from "@codernote/utils";
const { width, height } = getVideoSize(fileVideo);
// 或
const { width, height } = getImageSize(fileImage);
根据最大宽高比例,重设宽高
import { resizeResource } from "@codernote/utils";
const width = 600;
const height = 400;
// 最大限制宽300, 限高300(均为默认值)
const size = resizeResource(width, height, 300, 300);
console.log(size);
// {width: 300, height: 200}
oss
上传文件到 CDN
import { oss } from "@codernote/utils";
oss.uploadFile(files, (err, resArr) => {
if (!err) {
console.log(resArr);
}
});
删除上传的文件
import { oss } from "@codernote/utils";
oss.removeFile(filePath);
事件
长按事件
import { addLongPressEventListener } from "@codernote/utils";
// 绑定domEl上,长按事件(500ms默认值)
addLongPressEventListener(domEl, callback, 1000);
icons
一组常用 icons
import { icons } from "@codernote/utils";
console.log(icons);
/*
[
{ name: "add.svg", value: base64 }
...
]
*/
byNameFindIcon: 根据名称找 icon
import { byNameFindIcon } from "@codernote/utils";
byNameFindIcon("clear.svg");
发布
npm publish --access public