0.5.0 • Published 2 years ago
@hyjs/utils v0.5.0
utils
一些常用的工具类
使用
npm i @hyjs/utils -S
#or yarn
yarn add @hyjs/utils -S
#or pnpm
pnpm i @hyjs/utils -SgetDevice 获取当前设备
getDevice();
// "iOS" | "Android" | "Web"ieIE 是否IE
isIE();
// booleanisMobile 是否移动端
isMobile();
// booleancompressImage 压缩图片
await compressImage(file);
// Promise<Blob>downloadFile 下载流文件
注意:在请求时需要设置
headers头responseType: blobdownloadFile(data, 'image/jpeg', 'filename'); // filename.jpegconvertBase64ToFile Base64转File或Blob
await convertBase64ToFile(base64, 'file', 'filename'); // Promise<File>
await convertBase64ToFile(base64, 'blob', 'filename'); // Promise
## fileToBase64 File转Base64
``` ts
await fileToBase64(file);
// Promise<string>getAudioDuration 获取视频/音频时长
await getAudioDuration(file);
// 12sgetAccessType 获取类型函数
const accessType = getAccessType({});
// Object
const accessType = getAccessType(new RegExp());
// RegExp
const accessType = getAccessType(Symbol());
// Symbol
...sleep 休眠函数
async function() {
await sleep(3000);
// 3s ----
console.log('log');
}debounce 防抖函数
// debounce(() => {}, 毫秒);
debounce(() => {}, 2000);throttle 节流函数
// throttle(() => {}, 毫秒);
throttle(() => {}, 2000);convertCurrency 数字转大写金额
convertCurrency(987654321);
// 玖亿捌仟柒佰陆拾伍万肆仟叁佰贰拾壹元整numberToChinese 数字转大写数字
numberToChinese(987654321);
// 九億八仟七百六十五萬四仟三百二十一filterObjectEmpty 过滤对象指定内容
filterObjectEmpty({
a: undefined,
b: null,
c: '',
d: 0
});
// { d: 0 }
filterObjectEmpty({
a: undefined,
b: 111,
c: 222
}, [111, 222]);
// { a: undefined }random4Code 生成4位code
random4Code();
// dsj1randomChar 生成指定长度的字符,可选择number(数字), lowercase(小写字母), capital(大写字母)
randomChar();
// zZqt
randomChar(32);
// w2rAOdMRqhlhNEYzVUv2zw0Zp616rNFp
randomChar(32, ['number']);
// 05099593713036830668381743720300randomNumber 生成数字
randomNumber(100);
// 32
randomNumber(1, 3);
// 2currency 千分位分隔
currency(987654321);
// '987,654,321.00'
currency(987654321, 1);
// '987,654,321.0'
currency(987654321, 0);
// '987,654,321'toHump 下划线转驼峰
toHump('a_bc_d_e');
// aBcDEtoLine 驼峰转下划线
toLine('aBcDE');
// a_bc_d_euuid 生成uuid
uuid();
// 15afbbae-a98b-b07c-df94-e2f916ac1cd1dateFormatter 时间格式化
dateFormatter('YYYY-MM-DD hh:mm:ss');
// 2022-01-13 12:00:00
dateFormatter('YYYY-MM-DD hh:mm:ss', 'Thu Jan 13 2022 12:00:00 GMT+0800 (中国标准时间)')
// 2022-01-13 12:00:00RegExp+Name 正则校验
Strict严谨的Loose宽松的
// RegExpIMEI 手机机身码(IMEI)
RegExpIMEI.test('123456789012345');
// RegExpURL 网址(URL)
RegExpURL.test('www.npmjs.com');
RegExpURL.test('https://www.npmjs.com');
...getWxEnv 获取当前微信环境
⚠️微信内判断是否在小程序需要引入wx的sdk
const env = await getWxEnv();
switch(env) {
case 'wx':
// 微信内
break;
case 'mini-wx':
// 小程序内
break;
case 'no-wx':
// 非微信
break;
default:
//未知
}