js_ryl3
v1.0.0
Published
A collection of utility functions for JavaScript development
Readme
js_ryl3
JavaScript 工具函数库,提供常用的工具函数,支持按需导入。
安装
npm install js_ryl3按需导入
// 导入单个模块
import { cookie } from 'js_ryl3/cookie'
import { deepCopy } from 'js_ryl3/deepCopy'
import { num } from 'js_ryl3/number'
import { reg } from 'js_ryl3/reg'
import { time } from 'js_ryl3/time'
import { emoji } from 'js_ryl3/emoji'
import { emojiToUnicode } from 'js_ryl3/emojiToUnicode'
import { highlight } from 'js_ryl3/highLight'
import { getMonthDays } from 'js_ryl3/getMonthDays'
import { getParamsUtil } from 'js_ryl3/getParamsUtil'
import { filterTable } from 'js_ryl3/filterTable'
import { copy } from 'js_ryl3/copy'
import { flyToCart } from 'js_ryl3/flyToCart'
import { fun } from 'js_ryl3/function'
import { verCode } from 'js_ryl3/verCode'
import { downExcel } from 'js_ryl3/downExcel'
import { base64Img } from 'js_ryl3/base64Img'全量导入
import {
cookie,
deepCopy,
num,
reg,
time,
emoji,
emojiToUnicode,
highlight,
getMonthDays,
getParamsUtil,
filterTable,
copy,
flyToCart,
fun,
verCode,
downExcel,
base64Img
} from 'js_ryl3'API 文档
1. cookie - Cookie 操作
// 设置 Cookie
cookie.set('key', 'value', {
expires: new Date(), // 过期时间
path: '/', // 路径
domain: 'domain' // 域名,传入 'domain' 自动获取二级域名
})
// 获取 Cookie
cookie.get('key')
// 删除 Cookie
cookie.remove('key', { path: '/' })2. deepCopy - 深拷贝
const obj = { name: 'test', arr: [1, 2, 3], nested: { a: 1 } }
const copy = deepCopy({}, obj)3. num - 数字运算
解决 JavaScript 浮点数精度问题。
num.add(0.1, 0.2) // 0.3
num.sub(0.3, 0.1) // 0.2
num.mul(0.1, 0.2) // 0.02
num.div(0.2, 0.1) // 24. reg - 正则验证
reg.Phone('13812345678') // 验证手机号
reg.Email('[email protected]') // 验证邮箱
reg.IdCard('110101199001011234') // 验证身份证
reg.Common('abc123') // 验证字母数字下划线
reg.QQ('123456') // 验证 QQ 号
reg.Money('123.45') // 验证金额
reg.imgUrl('data:image/png;base64,...') // 验证图片 URL
reg.verChinese('中文') // 验证中文5. time - 时间处理
// 时间戳转日期
time.toDate(1719820800, 'yyyy-MM-dd') // '2024-07-01'
time.toDate(1719820800, 'yyyy-MM-dd HH:mm') // '2024-07-01 00:00'
time.toDate(1719820800) // '2024-07-01 00:00:00'
// 日期转时间戳
time.toStamp('2024-07-01') // 1719820800 (10位)
time.toStamp('2024-07-01', 13) // 1719820800000 (13位)
// 获取当前时间
time.getNow() // '2024-07-01 12:00:00'
// 相对时间格式化
time.timestampFormat(1719820800) // '昨天 12:00' 或 '07/01 12:00'6. emoji - Unicode 转 Emoji
emoji('U+1F600 U+1F44D') // '😊👍'7. emojiToUnicode - Emoji 转 Unicode
emojiToUnicode('😊👍') // '[u+1f600][u+1f44d]'8. highlight - 文本高亮
highlight('搜索词', '这是搜索词文本', 'highlight-class')
// '<span class="highlight-class">搜索词</span>文本'9. getMonthDays - 获取月份天数
getMonthDays(2024, 2) // 29 (2024年2月)
getMonthDays(2023, 2) // 28 (2023年2月)10. getParamsUtil - URL 参数处理
// 解析 URL 参数
getParamsUtil.getParams('http://example.com?name=test&age=20')
// { name: 'test', age: '20' }
// 获取单个参数
getParamsUtil.getParam('name', url, 'default')
getParamsUtil.getParam('age', url, 0, 'number')
// 构建查询字符串
getParamsUtil.buildQueryString({ name: 'test', age: 20 })
// 'name=test&age=20'
// 拼接参数到 URL
getParamsUtil.joinParams('http://example.com', { name: 'test' })
// 'http://example.com?name=test'
// 移除参数
getParamsUtil.removeParams('http://example.com?a=1&b=2', 'a')
// 'http://example.com?b=2'11. filterTable - 表格数据过滤
将 null、undefined、空字符串替换为指定占位符。
const data = [
{ id: 1, name: '张三', address: null },
{ id: 2, name: '李四', age: undefined }
]
filterTable(data, '--')
// [{ id: 1, name: '张三', address: '--' }, { id: 2, name: '李四', age: '--' }]12. copy - 复制到剪贴板
copy('复制内容', (res) => {
console.log(res.code, res.msg) // 200, '复制成功'
})13. flyToCart - 飞入购物车动画
flyToCart(event, imageUrl, targetElement, () => {
console.log('动画完成')
})14. verCode - 验证码倒计时
// 开始倒计时
verCode.PhoneCode(event, 60, '后重新获取')
// 清除倒计时
verCode.PhoneCodeClear()
// 获取状态
verCode.phoneCodeStatus()15. fun - 节流与防抖
// 节流
const throttled = fun.throole(fn, 500)
// 防抖
const debounced = fun.debounce(fn, 500)16. downExcel - 导出 Excel
注意:使用此功能需先安装 xlsx 依赖
npm install xlsxconst data = [
{ name: '张三', age: 25, city: '北京' },
{ name: '李四', age: 30, city: '上海' }
]
downExcel(data, '文件名', 'Sheet1')17. base64Img - 图片上传
base64Img(content, apiUrl, token, data, (original, error) => {
console.log('上传失败:', error)
}).then(result => {
console.log('替换后的内容:', result)
})注意事项
- 浏览器环境:
cookie、copy、flyToCart、verCode、base64Img依赖浏览器 API,无法在 Node.js 环境使用 - Excel 导出:
downExcel需要安装xlsx依赖 - ESM 格式:本库仅支持 ES Module 格式,请确保项目使用 ESM
