sparkle-blink
v1.0.3
Published
常用js函数库
Downloads
10
Readme
blink.js
一个简单的js常用函数库 created by 2019-08-03
函数名|作用| -|-| trim|去除空格 turnCase|大小写转换-全大写,全小写,首字母大写 parseCase|大小写相互转换 checkPwdStrength|验证密码强度 hexColor|随机16进制颜色 randomCode|指定位数随机码 keywordsCount|关键词统计 caseFormat|短横,下划线与小驼峰的相互转化 dateFormat|时间格式化 reverse|逆序输出 onlyArray|数组去重 flat|数组扁平化 sort|数组排序 bubble_sort|冒泡排序 resort|数组错乱排序 totalArr|数组求和 mergeArr|数组合并 lastItem|获取数组最后一项 preventShake|防抖 throttle|节流 deepCopy|深拷贝 clearWebSite|让网页变得干净--调试时使用 delInvalidprops|去除对象或数组的无效属性 downloadByUrl|根据指定url下载文件 enterEvent|回车事件监听 pastTime|已过时间 isLegalEmail|邮箱校验 isLegalName|中文名校验--(2-6)位 isLegalIdCard|身份证校验 isLegalPhone|手机号码校验 jsonFormat|自定义缩进的JSON格式化
安装:npm i sparkle-blink
API
字符串常用函数
- param1 string str 待处理字符串
- param2 number type 去除空格类型 1-所有空格 2-前后空格 3-前空格 4-后空格 默认为1
- return string str 处理后的字符串
trim(' hello world ',1)//helloworld
- param1 string str 待转换的字符串
- param2 number type 1-全大写 2-全小写 3-首字母大写
- return string str 处理后的字符串
turnCase('hello',1);//HELLO
turnCase('hello',2);//hello
turnCase('hello',3);//Hello
- param string str 待转换的字符串
- return string result 处理后的字符串
parseCase('hello');//HELLO
parseCase('HELLo');//hellO
- param string str 待转换的字符串
- 低于6为强度为0,字母+1,数字+1,下划线+1,特殊字符!@#$%+1,最高为4
- return number lv 密码强度等级
checkPwdStrength('12345');//0
checkPwdStrength('123456');//1
checkPwdStrength('123456a');//2
checkPwdStrength('123456a_');//3
checkPwdStrength('123456a_#');//4
checkPwdStrength('123456a_#$');//4
- return string str 带#号的随机16进制颜色
hexColor();//#2Ecc71
- param number count 随机码位数
- return string str 指定位数随机码
randomCode(6);//anfmk1
- param1 string text 进行统计的文本
- param2 string keywords 进行统计的关键词
- return number count 关键词出现次数
- tip:param1 document.body.innerText--全文统计
keywordsCount('富强民主文明和谐富强民主文明和谐','和谐');//2
- param1 string str 待转换字符串
- param2 string type '-'or '_',默认为下划线
- return string str 转换后字符串
caseFormat('stuName','-');//stu-name
caseFormat('stuName','_');//stu_name
- 时间格式化 dateFormat 形如--"20190803 11:01:07"
- param1 Date date 待格式化的时间
- param2 number type 格式化范围 1-精确到月 2-精确到日 3-精确到分钟 4-精确到秒
- return string time 格式化后的时间
dateFormat(new Date(),1)//201908
dateFormat(new Date(),2)//20190806
dateFormat(new Date(),3)//20190806 09:22
dateFormat(new Date(),4)//20190806 09:23:03
- param string str 源字符串
- return string result 逆序输出的字符串
reverse('hello');//olleh
数组常用函数
- param Array arr 待去重的数组
- return Array arr 去重后的数组
onlyArray([1,2,1,1,2,3,3]);//[1,2,3]
- param Array arr 待扁平化的数组
- return Array arr 扁平化后的数组
flat([1,[2,[3,4]]]);//[1,2,3,4]
- param1 Array arr 待排序的数组
- param2 number rule 排序规则 1-升序 0-降序,默认升序
- return Array arr 排序后的数组
sort([1,5,3],1);//[1,3,5]
sort([1,5,3],0);//[5,3,1]
- param1 Array arr 要进行排序的数组
- param2 number rule 排序规则 1-升序 0-降序,默认升序
- return Array arr 排序后的数组
bubble_sort([1,5,3],1);//[1,3,5]
bubble_sort([1,5,3],0);//[5,3,1]
- param Array arr 待排序的数组
- return Array arr 排序后的数组
resort([1,2,3,4,5,6]);//[2, 1, 5, 3, 4, 6]
- param Array arr 待求和的数组
- return Number num 求和结果
totalArr([1,2,3]);//6
- param1 Array arr 待合并数组
- ...params Array arr 合并数组,可放置多个
- return Array arr 合并后的数组
mergeArr([],[1,2],[3]);//[1,2,3]
- param Array arr 源数组
- return <> item 数组最后一项
lastItem([1,2,3]);//3
其他常用函数
- param function fn 要进行防抖的函数
function fn(){};
preventShake(fn);
- param function fn 要进行节流的函数
function fn(){};
throttle(fn);
- param Object obj 要进行拷贝的对象
- return Object obj 拷贝后的对象
deepCopy({name:'tom',age:18});//{name:'tom',age:18}
- tip:本质是将html-body内容隐藏并清除控制台
clearWebSite();
- param Object obj 待处理对象或数组
- return <> obj 处理后的对象或数组
delInvalidprops([undefined,null,'',1]);//[1]
delInvalidprops({name:undefined,age:null,hobby:'',sex:'boy');//{sex: "boy"}
- param1 string url 待下载文件url
- param2 string name 指定下载文件的名称 --默认为'下载'
- tip:html5新特性 --a 标签 download属性 部分浏览器不支持
downloadByUrl('url','desc');
- param function fn 业务函数
function fn(){console.log('hello')};
enterEvent(fn);
- param Date startTime 起始时间
- return string 时间节点描述
pastTime(new Date());//刚刚
- param1 Object/Array obj 待格式化的对象或数组
- param2 number space 缩进数
- return string jsonStr 格式化后的json字符串
jsonFormat([{name:'tom',age:18},{name:'jack',age:20},{name:'merry',age:19}],4);
/*"[
{
"name": "tom",
"age": 18
},
{
"name": "jack",
"age": 20
},
{
"name": "merry",
"age": 19
}
]"
*/
正则表达式
param string email 待校验邮箱
return boolean 校验结果
isLegalEmail(yourEmail);//true or false
- param string name 待校验中文名
- return boolean 校验结果
isLegalName(yourName);//true or false
- param string idCard 待校验身份证
- return boolean 校验结果
isLegalIdCard(yourIdCard);//true or false
- param string phone 待校验手机号码
- return boolean 校验结果
isLegalPhone(yourPhone);//true or false
使用
import {Blink} from 'sparkle-blink'
const {trim}=Blink;
console.log(trim(' hello world ',1))