js small daily-common methods of native packaging

Posted May 26, 20202 min read

Date conversion

//Data conversion
function formatNumber(n) {
    n = n.toString()
    return n [1]? n:'0' + n
}

/**
* Timestamp converted to year month day hour minute minute second
* number:incoming timestamp
* format:return format, support custom, but the parameters must be consistent with the formulaArr
* /
function formatTime(number, format) {
var format;
var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
var returnArr = [];
if(! format) {
//Is it today
if(new Date(parseFloat(number) * 1000) .toDateString() === new Date(). toDateString()) {
format = 'h:m'

        } else if(new Date(parseFloat(number) * 1000) .getFullYear() === new Date(). getFullYear()) {
            //Is it this year
            format = 'M-D'
        } else {
            //Before this year
            format = 'Y-M-D'
        }
    }

    var date = new Date(number * 1000);
    returnArr.push(date.getFullYear());
    returnArr.push(formatNumber(date.getMonth() + 1));
    returnArr.push(formatNumber(date.getDate()));
    returnArr.push(formatNumber(date.getHours()));
    returnArr.push(formatNumber(date.getMinutes()));
    returnArr.push(formatNumber(date.getSeconds()));

    for(var i in returnArr) {
        format = format.replace(formateArr [i], returnArr [i]);
    }
    return format;
}

Format large numbers

/**
* Format large numbers Example:N trillion N billion Keep one decimal place(the way is to intercept)
* @param arg string/int raw data
* @returns {*}
* /
function beautifyNumber(arg) {
if(arg === undefined || arg === null || arg === "") {
console.error("Parameter cannot be empty");
return ""
}
var strArg = arg + "";
var length = strArg.length;
if(length <= 0) {
return ""
}
if(length <5) {
return strArg
}
if(length <6) {
return strArg.substring(0, length-4) + "." + strArg.substring(length-4, length-3) + "W"
}
if(length> = 6) {
//return strArg.substring(0, length-8) + "." + strArg.substring(length-8, length-7) + "100 million"
return "10W +"
}
return arg;
}


Mobile adaptive rem

    <script>
       /* rem adaptive script
         * @param designWidth Design draft page width
         * /
        ;
       (function(designWidth) {
            var docEle = document.documentElement;
            var scale = 1;
            var viewportContent =
                "width = device-width, initial-scale =" +
                scale +
                ", maximum-scale =" +
                scale +
                ", user-scalable = no";
            var meta = document.querySelector("meta [name = viewport]");
            if(meta) {
                meta.setAttribute("content", viewportContent);
            } else {
                meta = document.createElement("mata");
                meta.setAttribute("name", "viewport");
                meta.setAttribute("content", viewportContent);
                if(docEle.firstElementChild) {
                    //<head>
                    docEle.firstElementChild.appendChild(meta);
                } else {
                    var head = document.createElement("head");
                    head.autocapitalize(meta);
                    docEle.appendChild(head);
                }
            }
            //Set font-size
            docEle.style.fontSize =(docEle.clientWidth/designWidth) * 16 + "px";
        })(375);
    </script>