tool-hai v1.1.0-1
🦄tool-hai
工作中总是要写重复的一些工具方法,浪费时间又没有意义。即想将常用方法函数封装成多个模块,由此
tool-hai
出现了。
🧙安装
npm i tool-hai
💪使用
nodeJs可以直接引用,浏览器环境借助
webpack
ES6module使用
🏹日历模块
getStringCalender(dateTime,format)
可传入两个参数
dateTime
:时间
format
:时间转化成字符串日历需要的格式,默认'YY-MM-DD hh:mm:ss'
const {Calendar} = require('tool-hai');
Calendar.getStringCalender(Date.now());//2021.07.24 20:19:14
Calendar.getStringCalender(Date.now(), 'YY年MM月DD日');//2021年07月24日
Calendar.getStringCalender(Date.now(), 'YY年MM月DD日 hh:mm');//2021年07月24日 20:28
Calendar.getStringCalender(Date.now(), 'YY年MM月DD日 hh:mm:ss');//2021年07月24日 20:29:41
Calendar.getStringCalender(Date.now(), 'YY-MM-DD hh:mm:ss');//2021-07-24 20:30:40
Calendar.getStringCalender(Date.now(), 'hh:mm:ss');//20:43:57
🏹数字模块
getRangeNumber(min,max)
随机获取一个min
与max
之间的一个整数
min
:最小值
max
:最大值
getUseTwoNumberToString(num)
:num
小10
,返回一个字符串
getThousandsChar(num,char)
:获取num
的千位符
num
:数字
char
:千位符拼接的符号,默认,
const {MathTool} = require('tool-hai');
MathTool.getRangeNumber(1,7);//3
MathTool.getUseTwoNumberToString(-2);//'02'
MathTool.getUseTwoNumberToString(2);//'2'
MathTool.getThousandsChar(1000.04);//1,000.04
MathTool.getThousandsChar(1000.04,"-");//1-000.04
🏹SessionStorage,LocalStorage
模块
针对浏览器中
sessionStorage
,localStorage
进行封装,所以适用于浏览器环境。nodeJs
环境调用不会报错仅会输出警告方案。
这里用node环境演示一下方法
方法与原生sessionStorage
,localStorage
一致,用法也一致。但对传入的key
,value
进行校验,value会被JSON.stringify
转成JSON
格式
const {MyStorage } = require('tool-hai'),
{ LocalStorage, SessionStorage } = MyStorage;
LocalStorage.setItem('11',{name:"LocalStorage",value:23});
LocalStorage.getItem('11');
LocalStorage.removeItem('11');
LocalStorage.clear();
🏹表单校验模块
addCacheTest(dataSource,config)
将表单校验方法添加至缓存区
dataSource
:表单值
config
:校验表单值配置,类型为Array(数组值为对象类型)
或Object
对象中必须含有description
和errorFn
字段
description
:调用哪个方法来校验表单值
description
字段可选值有:
isValueNoEmpty
:值不为空
islengthNoZero
:数组长度为零
isValueNoUndefined
:值不为undefined
isQualifiedTel
:值符合电话格式 /^1[3568][\d]{9}$/
isEqualsValue
:值等于某个指定的值
errorFn
:表单校验不正确执行的函数
start()
按顺序执行添加至缓存区的表单校验方法,并且清空缓存区的数据。返回一个Boolean
值
const { StrategyFrom } = require('tool-hai');
let dataSource = '';
//校验表单值不为空
StrategyFrom.addCacheTest(dataSource, {description: 'isValueNoEmpty', errorFn: () => {console.log('值不能为空');}});
console.log(StrategyFrom.start());//'值不能为空' false
//校验表单值为undefined并且不能为空
StrategyFrom.addCacheTest(undefined, [
{description:'isValueNoUndefined',errorFn()=>console.ware('值不能为undefined')}
{description: 'isValueNoEmpty', errorFn: () => {console.log('值不能为空');}},
]);
console.log(StrategyFrom.start());//值不能为undefined true
StrategyFrom.addCacheTest('13453232335', { description: 'isQualifiedTel', errorFn: () => { console.log('请确定电话号码是否正确'); } });
console.log(StrategyFrom.start());//true
//校验表单值是否等于某个指定值,指定值在description字段值后面用`:`拼接
StrategyFrom.addCacheTest('13453232335', { description: 'isEqualsValue:23', errorFn: () => { console.log('值不相等'); } });
console.log(StrategyFrom.start());//值不相等 false
StrategyFrom.addCacheTest('23', { description: 'isEqualsValue:23', errorFn: () => { console.log('值不相等'); } });
console.log(StrategyFrom.start());//true
//校验数组是否含有值
StrategyFrom.addCacheTest([], { description: 'islengthNoZero', errorFn: () => { console.log('数组为空'); } });
console.log(StrategyFrom.start());//数组为空 false
StrategyFrom.addCacheTest(['我有值'], { description: 'islengthNoZero', errorFn: () => { console.log('数组为空'); } });
console.log(StrategyFrom.start());//false
🏹节流,防抖模块
const {DebounceAndThrottle} = require('tool-hai');
防抖
debounce(fn,wait,immediate)
:防抖函数,返回一个函数。n秒只执行一次,n秒内多次触发重新定时
fn
:回调函数
wait
:等待时间, 默认1000
immediate
:是否立即执行,默认为false
cancelDebounce()
:取消防抖即将执行的函数
节流
throttle(fn,wait)
:节流函数,返回一个函数 。n秒内只执行一次,n秒内多次触发无效,保证n秒只执行一次
参数与debounce
函数一致,但没有immediate
cancelThrottle()
:取消节流即将执行的函数
let debounce = DebounceAndThrottle.debounce(()=>{
console.log(1);
})
debounce()//1000ms后 输出1
let debounce1 = DebounceAndThrottle.debounce(()=>{
console.log(1);
},2000,true);//立即执行 输出1
debounce1();//2000ms后 输出1
let throttle = DebounceAndThrottle.throttle(()=>{
console.log(1);
})
throttle()//1000ms后 输出1
let throttle1 = DebounceAndThrottle.throttle(()=>{
console.log(1);
})
throttle1();
throttle1();//throttle1两次调用 只会执行回调函数一次 输出 1
👋最后
这就是tool-hai
目前封装的方法,比较少。后期我会一点一点的完善新增更多常用的方法🙈,欢迎提issues,当然也欢迎大家一起完善增强tool-hai
🥺
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago