pryprtkits
v0.1.0-beta
Published
PryprtKits - 前端通用工具箱库,包含 150+ 个实用工具函数
Maintainers
Readme
PryprtKits
前端通用工具箱库,包含 150+ 个实用工具函数。
特性
- 📦 150+ 工具函数 - 覆盖文件转换、加密解密、图片处理三大模块
- 🚀 纯前端运行 - 无需后端,浏览器即可运行
- 🔐 安全可靠 - 基于 crypto-js 等标准库
- 📱 响应式支持 - 支持移动端和桌面端
- 🎯 易于使用 - 简洁的 API 设计
安装
# 使用 pnpm(推荐)
pnpm install
# 或使用 npm
npm install
# 或使用 yarn
yarn install编译
# 生产环境编译
pnpm run build
# 开发环境编译
pnpm run build:dev
# 监听模式
pnpm run watch使用方式
1. 浏览器引入
<!-- CDN 引入 -->
<script src="https://unpkg.com/pryprtkits/dist/pryprtkits.min.js"></script>
<script>
const { convert, encrypt, image } = PryprtKits;
// AES 加密
const encrypted = encrypt.aesEncrypt('Hello', 'mySecretKey');
// MD5 哈希
const hash = encrypt.md5('Hello World');
// 图片压缩
const compressed = await image.compressImage(file, 0.7);
</script>2. ES6 模块引入
import PryprtKits from 'pryprtkits';
const { convert, encrypt, image } = PryprtKits;
// 使用工具函数
const encrypted = encrypt.aesEncrypt('message', 'key');
const yaml = convert.jsonToYaml({ name: 'test' });3. CommonJS 引入
const PryprtKits = require('pryprtkits');
const { convert, encrypt, image } = PryprtKits;API 文档
文件转换模块 (convert)
文档转换
txtToMarkdown(text)- TXT 转 MarkdownmarkdownToTxt(markdown)- Markdown 转 TXTexcelToCsv(data)- Excel 转 CSVcsvToExcel(csv)- CSV 转 ExceljsonToCsv(jsonData)- JSON 转 CSVcsvToJson(csv)- CSV 转 JSON
数据格式转换
jsonToYaml(json, indent)- JSON 转 YAMLyamlToJson(yaml)- YAML 转 JSONjsonToXml(json, rootName)- JSON 转 XMLxmlToJson(xml)- XML 转 JSONmarkdownToHtml(markdown)- Markdown 转 HTMLhtmlToMarkdown(html)- HTML 转 Markdown
编码转换
textToBase64(text)- 文本转 Base64base64ToText(base64)- Base64 转文本fileToBase64(file)- 文件转 Base64base64ToFile(base64, filename, mimeType)- Base64 转文件urlEncode(text)- URL 编码urlDecode(text)- URL 解码htmlEscape(html)- HTML 转义htmlUnescape(text)- HTML 反转义unicodeToChinese(unicode)- Unicode 转中文chineseToUnicode(chinese)- 中文转 UnicodefullToHalf(str)- 全角转半角halfToFull(str)- 半角转全角
实用工具
timestampToDate(timestamp, format)- 时间戳转日期dateToTimestamp(date)- 日期转时间戳formatFileSize(bytes, decimals)- 文件大小格式化removeHtmlTags(html)- 移除 HTML 标签extractTextFromHtml(html)- 提取 HTML 文本objectToQueryString(obj)- 对象转查询字符串queryStringToObject(queryString)- 查询字符串转对象camelToKebab(str)- 驼峰转短横线kebabToCamel(str)- 短横线转驼峰snakeToCamel(str)- 下划线转驼峰camelToSnake(str)- 驼峰转下划线
加密解密模块 (encrypt)
对称加密
aesEncrypt(message, key)- AES 加密aesDecrypt(ciphertext, key)- AES 解密desEncrypt(message, key)- DES 加密desDecrypt(ciphertext, key)- DES 解密tripleDesEncrypt(message, key)- 3DES 加密tripleDesDecrypt(ciphertext, key)- 3DES 解密rc4Encrypt(message, key)- RC4 加密rc4Decrypt(ciphertext, key)- RC4 解密rabbitEncrypt(message, key)- Rabbit 加密rabbitDecrypt(ciphertext, key)- Rabbit 解密encryptFile(file, key)- 文件加密decryptFile(encryptedData, key, originalName)- 文件解密
哈希算法
md5(message)- MD5 哈希sha1(message)- SHA1 哈希sha256(message)- SHA256 哈希sha512(message)- SHA512 哈希sha224(message)- SHA224 哈希sha384(message)- SHA384 哈希fileMd5(file)- 文件 MD5fileSha1(file)- 文件 SHA1fileSha256(file)- 文件 SHA256fileSha512(file)- 文件 SHA512hmacSha256(message, key)- HMAC-SHA256hmacSha512(message, key)- HMAC-SHA512hmacMd5(message, key)- HMAC-MD5pbkdf2(password, salt, iterations, keySize)- PBKDF2 密钥派生
加密工具
xorEncrypt(message, key)- 异或加密xorDecrypt(encrypted, key)- 异或解密generatePassword(length, options)- 生成随机密码generateSecretKey(length)- 生成随机密钥generateSalt(length)- 生成随机盐checkPasswordStrength(password)- 密码强度检测base64UrlEncode(str)- Base64 URL 安全编码base64UrlDecode(str)- Base64 URL 安全解码encryptText(text, key, algorithm)- 文本加密decryptText(encrypted, key, algorithm)- 文本解密encryptFileChunks(file, key, chunkSize)- 文件分块加密decryptFileChunks(chunks, key)- 文件分块解密
图片处理模块 (image)
格式转换
imageToJpg(source, quality)- 转 JPGimageToPng(source)- 转 PNGimageToWebp(source, quality)- 转 WebPimageToGif(source)- 转 GIFimageToBmp(source)- 转 BMPimageToIco(source, size)- 转 ICObatchConvertImages(files, format, quality)- 批量转换convertImageFormat(source, format, quality)- 通用格式转换getImageInfo(source)- 获取图片信息
图片编辑
compressImage(source, quality, format)- 压缩图片cropImage(source, options)- 裁剪图片rotateImage(source, angle)- 旋转图片scaleImage(source, options)- 缩放图片addWatermark(source, options)- 添加水印roundCorners(source, radius)- 圆角cropToCircle(source)- 裁剪成圆形
图片美化
grayscaleImage(source)- 灰度binaryImage(source, threshold)- 二值化invertImage(source)- 反色adjustBrightness(source, brightness)- 调整亮度adjustContrast(source, contrast)- 调整对比度adjustSaturation(source, saturation)- 调整饱和度adjustHue(source, hue)- 调整色相blurImage(source, radius)- 模糊sharpenImage(source, strength)- 锐化denoiseImage(source, strength)- 降噪vintageFilter(source)- 复古滤镜coldFilter(source)- 冷色滤镜warmFilter(source)- 暖色滤镜
图片工具
getColorFromImage(source, x, y)- 取色器stitchImagesHorizontally(sources, backgroundColor)- 水平拼接stitchImagesVertically(sources, backgroundColor)- 垂直拼接stitchImagesGrid(sources, options)- 网格拼接generateQRCode(text, options)- 生成二维码generateQRCodeImage(text, options)- 生成二维码图片cropToSize(source, width, height, fit)- 裁剪到指定尺寸addBorder(source, options)- 添加边框
示例
加密解密
// AES 加密解密
const encrypted = encrypt.aesEncrypt('Hello World', 'mySecretKey123456');
const decrypted = encrypt.aesDecrypt(encrypted, 'mySecretKey123456');
// MD5 哈希
const hash = encrypt.md5('Hello World');
// SHA256 哈希
const sha256 = encrypt.sha256('Hello World');
// 生成密码
const password = encrypt.generatePassword(16, {
uppercase: true,
lowercase: true,
numbers: true,
symbols: true,
});
// 密码强度检测
const strength = encrypt.checkPasswordStrength('MyStr0ngP@ss!');文件转换
// JSON 转 YAML
const yaml = convert.jsonToYaml({ name: 'test', value: 123 });
// Markdown 转 HTML
const html = convert.markdownToHtml('# Hello\n**Bold** text');
// Base64 编码
const base64 = convert.textToBase64('Hello World');
// URL 编码
const encoded = convert.urlEncode('Hello 世界');图片处理
// 压缩图片
const compressed = await image.compressImage(file, 0.7);
// 裁剪图片
const cropped = await image.cropImage(file, {
x: 10,
y: 10,
width: 100,
height: 100,
});
// 添加水印
const watermarked = await image.addWatermark(file, {
text: '© 2024',
position: 'bottom-right',
fontSize: 24,
});
// 生成二维码
const qrCanvas = await image.generateQRCode('https://example.com');浏览器支持
- Chrome >= 60
- Firefox >= 55
- Safari >= 12
- Edge >= 79
- iOS >= 12
- Android >= 6
许可证
MIT License
贡献
欢迎提交 Issue 和 Pull Request!
