Javascript代码段记录: utils

浏览器检查

/**
         * @property browser check browser agent
         * @property {Boolean} browser.webkit  Webkit 浏览器检查
         * @property {Boolean} browser.mozilla  Mozilla 浏览器检查
         * @property {Boolean} browser.msie  IE 浏览器检查 */
        browser: {
            webkit: ('WebkitAppearance' in document.documentElement.style) ? true : false,
            mozilla: (typeof window.mozInnerScreenX != "undefined") ? true : false,
            msie: (window.navigator.userAgent.indexOf("Trident") != -1) ? true : false
        },

源码可以看这里jui-chart core.js

检测触摸设备


        /**
         * @property {Boolean} isTouch
         * check touch device
         */
        isTouch: /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(window.navigator.userAgent)

数据类型

/**
         * @method typeCheck
         * check data  type
         * @param {String} t  type string
         * @param {Object} v value object
         * @return {Boolean}
         */
        typeCheck: function (t, v) {
            function check(type, value) {
                if (typeof(type) != "string") return false;

                if (type == "string") {
                    return (typeof(value) == "string");
                }
                else if (type == "integer") {
                    return (typeof(value) == "number" && value % 1 == 0);
                }
                else if (type == "float") {
                    return (typeof(value) == "number" && value % 1 != 0);
                }
                else if (type == "number") {
                    return (typeof(value) == "number");
                }
                else if (type == "boolean") {
                    return (typeof(value) == "boolean");
                }
                else if (type == "undefined") {
                    return (typeof(value) == "undefined");
                }
                else if (type == "null") {
                    return (value === null);
                }
                else if (type == "array") {
                    return (value instanceof Array);
                }
                else if (type == "date") {
                    return (value instanceof Date);
                }
                else if (type == "function") {
                    return (typeof(value) == "function");
                }
                else if (type == "object") {
                    // typeCheck如果类型在对象中定义,那么当对象被选中时,它将返回false。 (date, array, null)
                    return (
                    typeof(value) == "object" &&
                    value !== null && !(value instanceof Array) && !(value instanceof Date) && !(value instanceof RegExp)
                    );
                }

                return false;
            }

            if (typeof(t) == "object" && t.length) {
                var typeList = t;

                for (var i = 0; i < typeList.length; i++) {
                    if (check(typeList[i], v)) return true;
                }

                return false;
            } else {
                return check(t, v);
            }
        }

jui

内存转换

图片描述

export function bytesToSize(value) {
  if (+value === 0) return '0 B'
  let k = 1024,
    sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
    i = Math.floor(Math.log(value)/Math.log(k))
  return (value / Math.pow(k, i)).toPrecision(3) + ' '+ sizes[i]  
}
阅读 622

推荐阅读
镜心的小树屋
用户专栏

方寸湛然GitHub组织地址:[链接]

47 人关注
123 篇文章
专栏主页