Skip to content

useful Javascript Library when i on work (工作中常用的js库,以及相关函数)

License

Notifications You must be signed in to change notification settings

webszy/useful_js_library

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 

Repository files navigation

useful_js_library

useful Javascript Library when i on work (工作中常用的js库,以及相关函数)

函数类

  • 处理数字为逗号分隔

    function parseNum(num) {
      // 数字每三位加逗号
      return (num || 0).toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')
    }
  • 获取get时的parameter

    function getURLParams(str){
      let q={}
      str.replace(/([^?&=]+)=([^&]+)/g,(_,k,v)=>q[k]=v)
      return q
    }
  • 捕获await的错误

      function to(promise) {
        return promise
            .then(data => {
                return [null, data];
            })
            .catch(err => [err]);
    }
  • 判断游览器信息(来自网络)

      function BrowserType() {
        const ua = navigator.userAgent.toLowerCase();
        const testUa = regexp => regexp.test(ua);
        const testVs = regexp => (ua.match(regexp) + "").replace(/[^0-9|_.]/ig, "").replace(/_/ig, ".");
        // 接上以上if...else条件判断
        // ......
        // 获取到system、systemVs、platform、engine、engineVs、supporter、supporterVs、shell、shellVs
        return Object.assign({
            engine, // webkit gecko presto trident
            engineVs,
            platform, // desktop mobile
            supporter, // chrome safari firefox opera iexplore edge
            supporterVs,
            system, // windows macos linux android ios
            systemVs
        }, shell === "none" ? {} : {
            shell, // wechat qq uc 2345 sougou liebao maxthon baidu
            shellVs
        });
    }
    • 获取本月天数
    function GetDaysOfMonth(Year,Month){
      var date = new Date(Year, Month, 0);
       return(parseInt(date.getDate()));
    }
    • 防止精度丢失
    function FixedANumber(num,n) {
     return +(Math.round((num + 0.000001) * 100) / 100).toFixed(n)
    }
    • 判断变量类型
      var checkType = Function.prototype.call.bind(Object.prototype.toString);
      console.log(toStr1({}));      // "[object Object]"
      console.log(toStr1([]));      // "[object Array]"
      console.log(toStr1(123));     // "[object Number]"
      console.log(toStr1("abc"));   // "[object String]"
      console.log(toStr1("abc"));   // "[object String]"
      console.log(toStr1(new Date));// "[object Date]"

工具库

UI库

公众号.jpg

About

useful Javascript Library when i on work (工作中常用的js库,以及相关函数)

Resources

License

Stars

Watchers

Forks

Packages

No packages published