Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@codernote/utils

Package Overview
Dependencies
Maintainers
2
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@codernote/utils

笔记软件通用工具库

npmnpm
Version
1.9.0
Version published
Weekly downloads
30
-73.91%
Maintainers
2
Weekly downloads
 
Created
Source

工具库

笔记软件通用工具库

安装

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

FAQs

Package last updated on 14 Sep 2024

Did you know?

Socket

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.

Install

Related posts