npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

light-func

v1.1.0

Published

一个轻量的js常用函数库

Downloads

11

Readme

使用

  import {trim} from 'light-func'
  console.log(trim('  hello  world  ',1))//helloworld

API

| 函数名 | 作用 | | ---------------- | ------------------------------------- | | trim | 去除空格 | | turnCase | 大小写转换-全大写,全小写,首字母大写 | | parseCase | 大小写相互转换 | | checkPwdStrength | 验证密码强度 | | hexColor | 随机16进制颜色 | | randomCode | 指定位数随机码 | | keywordsCount | 关键词统计 | | caseFormat | 短横,下划线与小驼峰的相互转化 | | dateFormat | 时间格式化 | | reverse | 逆序输出 | | onlyArray | 数组去重 | | sort | 数组排序 | | resort | 数组错乱排序 | | totalArr | 数组求和 | | mergeArr | 数组合并 | | lastItem | 获取数组最后一项 | | debounce | 防抖 | | throttle | 节流 | | deepClone | 深拷贝 | | clearWebSite | 让网页变得干净--调试时使用 | | delInvalidprops | 去除对象或数组的无效属性 | | downloadByUrl | 根据指定url下载文件 | | enterEvent | 回车事件监听 | | pastTime | 已过时间 | | isLegalEmail | 邮箱校验 | | isLegalName | 中文名校验--(2-6)位 | | isLegalIdCard | 身份证校验 | | isLegalPhone | 手机号码校验 | | jsonFormat | 自定义缩进的JSON格式化 | | setCookie | 设置cookie | | getCookie | 获取cookie | | clearCookie | 清除cookie | | info | 美化打印--信息 | | warn | 美化打印--警告 | | error | 美化打印--错误 |

安装:npm i light-func

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]
  • 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]
  • 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

其他常用函数

  • param1 function fn 要进行防抖的函数
  • param2 delay 延迟时间
    function fn(){};
    var debounceFn=debounce(fn,1000);//返回一个新的防抖函数
    debounceFn(参数列表)
  • param1 function fn 要进行节流的函数
  • param2 delay 延迟时间
    function fn(){};
    var throttleFn=throttle(fn,1000);//返回一个新的节流函数
    throttleFn(参数列表)
  • param Object obj 要进行拷贝的对象
  • return Object obj 拷贝后的对象
   deepClone({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
    }
]"
*/
  • param1 String name 要设置的cookie名称
  • param2 * value 要设置的cookie值

    setCookie("name","gaoqian");
  • param String name 要获取的cookie名
  • return String value 要获取的cookie值

    getCookie("name");//"gaoqian"
  • param String name 要清除的cookie名

    clearCookie("name");
  • ...param String msg 要输出的信息
  • return String 美化后的信息

    info("hello");//hello 背景色#2ecc71
    warn("hello");//hello 背景色orange
    error("hello");//hello 背景色#FF0000

正则表达式

  • 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