js 超实用的工具类插件

121 阅读2分钟

@vhen2023/fs-helper

安装

npm install @vhen2023/fs-helper --save-dev 或者 pnpm add -D @vhen2023/fs-helper

import { convertToRMB, hideMobile, toCase, trim, dayjs } from '@vhen2023/fs-helper'

工具类

函数举例描述
convertToRMBconvertToRMB(120)数字金额转化为大写金额
hideMobilehideMobile('15612345678')隐藏手机号
toCasetoCase('test',3)大小写转化(1-大写,2-小写,3-首字母大写)
trimtrim(' t e s t ', 4)去除空格 (1-所有空格 2-前后空格 3-前空格 4-后空格)
LoadScriptLoadScript('test.ts', () => {})异步加载 JS
padZeropadZero(1, 2)数字补位 001
setObjToUrlParamssetObjToUrlParams('www.baidu.com', { a: 1, b: 2 })将对象作为参数添加到 URL
setUrlParamsToObjsetUrlParamsToObj('www.baidu.com?a=1&b=2')将 URL 中的参数转为对象
onlyDecimalOrIntegeronlyDecimalOrInteger('432.356',true)小数或整数(不可以负数) ,第二个参数控制是否四舍五入
onlyIntegeronlyInteger('222')正整数
randomRangeIntegerrandomRangeInteger(10,30)在某个区间随机一个整数
greetingsgreetings()问候语:早中晚等
thousandSeparatorthousandSeparator(1215455.1234, ',')千分位分隔符

时间类

// 依赖 dayjs库,抛出 dayjs,
import { toTimeFormat, getPastTime, getDateDiff, dayjs } from "@vhen2023/fs-helper";
函数举例描述
toTimeFormattoTimeFormat(new Date(),'YYYY-MM-DD HH:mm:ss')时间格式化默认:YYYY-MM-DD HH:mm:ss
getPastTimegetPastTime('2012-01-02 12:12:30', 2)过去日期(刚刚,几分钟之前等),
第二个参数为 0 不格式时间,否则 2 表示大于 2 年显示格式化时间 YYYY-MM-DD HH:mm:ss
getDateDiffgetDateDiff('2024-05-01', new Date(), 'day')判断相差天、月、年 ,类型:('day'、'months'、'years')
getZodiacgetZodiac(2024)生肖

验证类

函数举例描述
isPhoneisPhone('19606066920')是否为手机号码
isExternalisExternal('www.baidu.com')是否是外部链接
isUrlisUrl('www.baidu.com')验证是否是 url
isImageUrlisImageUrl('xxx.xxx.png')验证图片路径
isIdCardisIdCard('350423199906154973')验证是否是身份证号

@vhen2023/fs-helper持续更新,有需要拓展需求可以写在评论区