1.9.1 • Published 9 months ago

@codernote/utils v1.9.1

Weekly downloads
-
License
-
Repository
-
Last release
9 months ago

工具库

笔记软件通用工具库

安装

npm install @codernote/utils

公共环境变量

import { ENV, setEnv } from "@codernote/utils";

// 手动指定环境
// 不手动指定则会根据域名自动识别
setEnv("dev" | "test" | "prod");
// 可以单独直接设定env值
setEnv({
  URL_SERVER_HTTP: "http://localhost:7000",
});

console.log(ENV);
/*
ENV{
  // 当前环境
  ENV: "TEST",

  // 后端服务域名
  URL_DOMAIN,

  // 后端HTTP服务地址
  URL_SERVER_HTTP: "",

  // 后端websocket服务地址
  URL_SERVER_WSS: "",

  // media服务地址
  URL_SERVER_MEDIA: "",

  // 公共登录页面地址
  URL_PAGE_LOGIN_PC: "",

  // 公共用户信息页面地址
  URL_PAGE_USER_PC: "",
}
*/

工具函数

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
1.9.1

9 months ago

1.9.0

9 months ago

1.8.1

11 months ago

1.8.0

12 months ago

1.6.2

1 year ago

1.7.0

1 year ago

1.6.1

1 year ago

1.8.0-beta.1

12 months ago

1.8.0-beta.0

12 months ago

1.8.0-beta.3

12 months ago

1.8.0-beta.2

12 months ago

1.8.0-beta.4

12 months ago

1.6.0

1 year ago

1.5.0

1 year ago

1.4.0

1 year ago

1.3.1

1 year ago

1.3.0

1 year ago

1.2.0

1 year ago

1.1.0

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago

0.0.1

1 year ago