npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

cc-web-utils

v1.0.1

Published

A collection of utility functions for frontend development

Readme

cc-web-utils

一个实用的前端工具函数库,包含剪贴板、文件处理、下载、图片处理等常用工具函数。

安装

npm install cc-web-utils
# 或
yarn add cc-web-utils
# 或
pnpm add cc-web-utils

可选依赖

某些功能需要安装额外的依赖:

  • downloadImagesAsZip 需要 jszipantd
  • download 需要 axiosantd
# 如果需要使用批量下载图片功能
npm install jszip antd

# 如果需要使用文件下载功能
npm install axios antd

使用

ES Module

import { copyToClipboard, formatStorage, getImageDimensions } from 'cc-web-utils';

CommonJS

const { copyToClipboard, formatStorage } = require('cc-web-utils');

API 文档

剪贴板

copyToClipboard(text: string): void

复制文本到剪贴板,支持 HTTPS 和非安全环境。

import { copyToClipboard } from 'cc-web-utils';

copyToClipboard('要复制的文本');

文件处理

createFormData(data: Record<string, any>): FormData

将对象转换为 FormData,支持嵌套对象和文件数组。

import { createFormData } from 'cc-web-utils';

const formData = createFormData({
  name: '张三',
  files: [file1, file2],
  nested: {
    key: 'value'
  }
});

appendFormData(formData: FormData, data: any, parentKey?: string): void

向现有 FormData 追加数据,支持嵌套对象。

import { appendFormData } from 'cc-web-utils';

const formData = new FormData();
appendFormData(formData, { name: '张三', age: 18 });

dealFileKey(obj: Record<string, any>, key: string): FormData

处理文件字段,将文件数组转换为 FormData。

import { dealFileKey } from 'cc-web-utils';

const formData = dealFileKey({ images: [file1, file2] }, 'images');

下载

download(downloadFn, fileName?: string): Promise<void>

通用文件下载函数,支持从 Axios 响应中下载文件。

import { download } from 'cc-web-utils';

await download(
  (config) => axios.get('/api/download', config),
  'filename.pdf'
);

downloadImg(url: string, filename: string): Promise<void>

下载单张图片。

import { downloadImg } from 'cc-web-utils';

await downloadImg('https://example.com/image.png', 'my-image.png');

downloadImagesAsZip(images: ImageItem[], zipFileName?: string): Promise<void>

批量下载图片并打包为 ZIP 文件。

import { downloadImagesAsZip } from 'cc-web-utils';

await downloadImagesAsZip(
  [
    { url: 'https://example.com/1.png', name: 'image1.png' },
    { url: 'https://example.com/2.png', name: 'image2.png' }
  ],
  'images.zip'
);

图片处理

getImageDimensions(src: string): Promise<{ width: number; height: number }>

获取图片的原始尺寸。

import { getImageDimensions } from 'cc-web-utils';

const { width, height } = await getImageDimensions('https://example.com/image.png');
console.log(`图片尺寸: ${width} x ${height}`);

格式化

formatStorage(bytes: number): string

格式化存储大小,返回 MB 或 GB 单位的字符串。

import { formatStorage } from 'cc-web-utils';

formatStorage(1024 * 1024);      // '1MB'
formatStorage(1024 * 1024 * 1024); // '1GB'

数学工具

radiansToDegrees(radians: number): number

将弧度转换为角度。

import { radiansToDegrees } from 'cc-web-utils';

radiansToDegrees(Math.PI); // 180

degreesToRadians(degrees: number): number

将角度转换为弧度。

import { degreesToRadians } from 'cc-web-utils';

degreesToRadians(180); // Math.PI

类型定义

本库使用 TypeScript 编写,包含完整的类型定义。

浏览器兼容性

本库中的大部分功能依赖于浏览器 API(如 navigator.clipboardcanvasBlob 等),仅适用于浏览器环境。

License

ISC