工具库v1.3

直接上代码:

v1.0

;
var Tool = (function() {
    // 类型判断
    var type = {
        version: 'v1.2',
        getType: function(ele) {
            if (window == document && document != window) {
                return 'window';
            } else if (ele.nodeType === 9) {
                return 'document';
            } else if (ele.callee) {
                return 'arguments';
            } else if (isFinite(ele.length) && ele.item) {
                return 'NodeList';
            } else {
                var type = Object.prototype.toString.call(ele),
                    reg = /\[object (.*)\]/,
                    arr = reg.exec(type);
                return arr[1];
            }
        },
        isArray : function(ele){
            return (this.getType(ele) === 'Array') ? true : false;
        },
        isFunction : function(ele){
            return (this.getType(ele) === 'Function') ? true : false;
        },
        isObject : function(ele){
            return (this.getType(ele) === 'Object') ? true : false;
        },
        isString : function(ele){
            return (this.getType(ele) === 'String') ? true : false;
        },
        isNumber : function(ele){
            return (this.getType(ele) === 'Number') ? true : false;
        },
        isBoolen : function(ele){
            return (this.getType(ele) === 'Boolean') ? true : false;
        },
        isUndefined : function(ele){
            return (this.getType(ele) === 'Undefined') ? true : false;
        },
        isNull : function(ele){
            return (this.getType(ele) === 'Null') ? true : false;
        }
    }




    //工具函数
    var Tool = function() {
        if (!(this instanceof Tool)) {
            return new Tool;
        }
    };
    //字符串方法
    Tool.str = {
        //判定一个字符串是否包含另一个字符串
        contains: function(target, item) {
            return target.indexOf(item) != -1;
            //return target.indexOf(item) > -1;
        },
        //一般使用在对于类名的判断;
        containsClass: function(target, item, separator) {
            return separator ? (separator + target + separator).indexOf(separator + item + separator) > -1 : this.contains(target, item);
        },
        //参数2是参数1的开头么?
        startsWith: function(target, item, ignorecase) {
            var str = target.slice(0, item.length);
            return ignorecase ? str.toLowerCase() === item.toLowerCase() : str === item;
        },
        //参2是参1的结尾么?
        endsWith: function(target, item, ignorecase) {
            var str = target.slice(-(item.length));
            // console.log(str)
            return ignorecase ? str.toLowerCase() === item.toLowerCase() : str === item;
        },
        //重复item,times次
        repeat: function(item, times) {
            var s = item,
                target = '';
            while (times > 0) {
                if (times % 2 == 1) {
                    target += s;
                }
                if (times == 1) {
                    break;
                }
                s += s;
                times = times >> 1;// /2
            }
            return target;
            //retrun new Array(times + 1).join(item)

        },
        //获得字符串字节长度
        byteLen: function(str, charset) {
            var target = 0,
                charCode,
                i,
                len;
            charset = charset ? charset.toLowerCase() : '';
            if (charset === 'utf-16' || charset === 'utf16') {
                for (i = 0, len = str.length; i < len; i++) {
                    charCode = str.charCodeAt(i);
                    if (charCode <= 0xffff) {
                        target += 2;
                    } else {
                        target += 4;
                    }
                }
            } else {
                for (i = 0, len = str.length; i < len; i++) {
                    charCode = str.charCodeAt(i);
                    if (charCode <= 0x007f) {
                        target += 1;
                    } else if (charCode <= 0x07ff) {
                        target += 2;
                    } else if (charCode <= 0xffff) {
                        target += 3;
                    } else {
                        target += 4;
                    }
                }
            }
            return target;
        },
        //字符串截断方法
        truncate: function(target, len, truncation) {
            len = len || 30;
            truncation = truncation ? truncation : '...';
            return (target.length > len) ? target.slice(0, (len - truncation.length)) + truncation : target.toString();
        },
        //_ - 转驼峰命名
        camelize: function(target) {
            if (target.indexOf('-') < 0 && target.indexOf('_') < 0) {
                return target;
            }
            return target.replace(/[-_][^-_]/g, function(match) {
                console.log(match)
                return match.charAt(1).toUpperCase();
            })

        },
        //转成下划线方法
        underscored : function(target){
            return target.replace(/([a-z0-9])([A-Z])/g,'$1_$2').toLowerCase();
        },
        //转换成连字符模式
        dasherize : function(target){
            return this.underscored(target).replace(/_/g,'-');
        },
        //首字母大写
        capitalize : function(target){
            return target.charAt(0).toUpperCase() + target.slice(1).toLowerCase();
        },
        //去掉script中的内容和Html标签
        stripTags : function(target){
            if(type.getType(target) === 'String'){
                return target.replace(/<script[^>]*>(\S\s*?)<\/script>/img,'').replace(/<[^>]+>/g,'');
            }
        },
        //填补0
        fillZero :function(target,n){
            var z = new Array(n).join('0'),
            str = z + target,
            result = str.slice(-n);
        return result;
        }
    };
    // 数组方法
    Tool.arr = {

    };
    return Tool;

})()

v1.2

;
var Tool = (function() {
    var type = {
        version: 'v1.2',
        getType: function(ele) {
            if (window == document && document != window) {
                return 'window';
            } else if (ele.nodeType === 9) {
                return 'document';
            } else if (ele.callee) {
                return 'arguments';
            } else if (isFinite(ele.length) && ele.item) {
                return 'NodeList';
            } else {
                var type = Object.prototype.toString.call(ele),
                    reg = /\[object (.*)\]/,
                    arr = reg.exec(type);
                return arr[1];
            }
        },
        isArray : function(ele){
            return (this.getType(ele) === 'Array') ? true : false;
        },
        isFunction : function(ele){
            return (this.getType(ele) === 'Function') ? true : false;
        },
        isObject : function(ele){
            return (this.getType(ele) === 'Object') ? true : false;
        },
        isString : function(ele){
            return (this.getType(ele) === 'String') ? true : false;
        },
        isNumber : function(ele){
            return (this.getType(ele) === 'Number') ? true : false;
        },
        isBoolen : function(ele){
            return (this.getType(ele) === 'Boolean') ? true : false;
        },
        isUndefined : function(ele){
            return (this.getType(ele) === 'Undefined') ? true : false;
        },
        isNull : function(ele){
            return (this.getType(ele) === 'Null') ? true : false;
        }
    }
    Array.prototype.indexOf = function(item){
        var len = this.length;
        for(var i=0;i<len;i++){
            this[i] === item;
            return i;
        }
        return -1;
    };
    //工具函数
    var Tool = function() {
        if (!(this instanceof Tool)) {
            return new Tool;
        }
    };
    //字符串方法
    Tool.str = {
        //判定一个字符串是否包含另一个字符串
        contains: function(target, item) {
            return target.indexOf(item) != -1;
            //return target.indexOf(item) > -1;
        },
        //一般使用在对于类名的判断;
        containsClass: function(target, item, separator) {
            return separator ? (separator + target + separator).indexOf(separator + item + separator) > -1 : this.contains(target, item);
        },
        //参数2是参数1的开头么?
        startsWith: function(target, item, ignorecase) {
            var str = target.slice(0, item.length);
            return ignorecase ? str.toLowerCase() === item.toLowerCase() : str === item;
        },
        //参2是参1的结尾么?
        endsWith: function(target, item, ignorecase) {
            var str = target.slice(-(item.length));
            // console.log(str)
            return ignorecase ? str.toLowerCase() === item.toLowerCase() : str === item;
        },
        //重复item,times次
        repeat: function(item, times) {
            var s = item,
                target = '';
            while (times > 0) {
                if (times % 2 == 1) {
                    target += s;
                }
                if (times == 1) {
                    break;
                }
                s += s;
                times = times >> 1;
            }
            return target;
            //retrun new Array(times).join(item)

        },
        //获得字符串字节长度
        byteLen: function(str, charset) {
            var target = 0,
                charCode,
                i,
                len;
            charset = charset ? charset.toLowerCase() : '';
            if (charset === 'utf-16' || charset === 'utf16') {
                for (i = 0, len = str.length; i < len; i++) {
                    charCode = str.charCodeAt(i);
                    if (charCode <= 0xffff) {
                        target += 2;
                    } else {
                        target += 4;
                    }
                }
            } else {
                for (i = 0, len = str.length; i < len; i++) {
                    charCode = str.charCodeAt(i);
                    if (charCode <= 0x007f) {
                        target += 1;
                    } else if (charCode <= 0x07ff) {
                        target += 2;
                    } else if (charCode <= 0xffff) {
                        target += 3;
                    } else {
                        target += 4;
                    }
                }
            }
            return target;
        },
        //字符串截断方法
        truncate: function(target, len, truncation) {
            len = len || 30;
            truncation = truncation ? truncation : '...';
            return (target.length > len) ? target.slice(0, (len - truncation.length)) + truncation : target.toString();
        },
        //_ - 转驼峰命名
        camelize: function(target) {
            if (target.indexOf('-') < 0 && target.indexOf('_') < 0) {
                return target;
            }
            return target.replace(/[-_][^-_]/g, function(match) {
                console.log(match)
                return match.charAt(1).toUpperCase();
            })

        },
        //转成下划线方法
        underscored : function(target){
            return target.replace(/([a-z0-9])([A-Z])/g,'$1_$2').toLowerCase();
        },
        //转换成连字符模式
        dasherize : function(target){
            return this.underscored(target).replace(/_/g,'-');
        },
        //首字母大写
        capitalize : function(target){
            return target.charAt(0).toUpperCase() + target.slice(1).toLowerCase();
        },
        //去掉script中的内容和Html标签
        stripTags : function(target){
            if(type.getType(target) === 'String'){
                return target.replace(/<script[^>]*>(\S\s*?)<\/script>/img,'').replace(/<[^>]+>/g,'');
            }
        },
        //填补0
        fillZero :function(target,n){
            var z = new Array(n).join('0'),
                str = z + target,
                result = str.slice(-n);
            return result;
            //return (Math.pow(10,n) + '' + target).slice(-n);
        },
        // print
        print : function(str,object){
            var arr = [].slice.call(arguments,1),
                index;
            return str.replace(/#{([^{}]+)}/gm,function(match,name){
                index = Number(name);
                if(index >= 0){
                    return arr[index];
                }
                if(object && object[name] !== ''){
                    return object[name];
                }
                return '';
            })
        },
        //去空格
        trim : function(str){
            str = str.replace(/^\s+/,'');
            for(var i =str.length - 1;i >=0;i--){
                if(/\S/.test(str.charAt(i))){
                    str = str.slice(0,i + 1);
                    break;
                }
            }
            return str;
        }
    };
    // 数组方法
    Tool.arr = {
        //是否包含指定元素
        contains : function(target,item){
            return target.indexOf(item) > -1;
        },
        //在参数1中删除参数2指定的元素返回布尔
        removeAt : function(target,index){
            return !!target.splice(index,1).length;
        },
        //在参数1中删除参数2返回布尔
        remove : function(target,item){
            var index = target.indexOf(item);
            return index > -1 ? this.removeAt(target,index) : false;
        },
        //打乱数组返回新数组
        shuffle : function(target){
            var temp = target,
                j,
                x,
                i = target.length;
            for(;i>0;j = parseInt(Math.random()*i),x = target[--i],target[i] = target[j],target[j] = x){

            }
            return temp;
            //target.sort(function(){return 0.5 - Math.random()});
        },
        //在数组中随机取一个
        random : function(target){
            return target[Math.floor(Math.random() * target.length)];
        },
        //把多维数组变成一维数组
        flatten : function(target){//有问题
            var result = [];
            target.forEach(function(item){
                if(type.getType(item) !== 'Array'){
                    result.push(item);

                }else{
                    result = result.concat(arguments.callee(item));
                }
            });
            return result;
        },
        // 数组去重
    unique : function(target){
        var temp = [];
        _that: for(var i = 0,len = target.length;i < len;i ++){
            for(var j = i + 1;j < len;j++){
                if(target[i] === target[j]){
                    continue _that;
                }
            }
            temp.push(target[i])
        }
        return temp;
    },
    //去除数组中的undefined和Null
    compact : function(target){
        if(!type.isArray(target)){
            throw new Error('target error type');
        }
        return target.filter(function(item){
            return item != undefined;
        })
    },
    //获取数组对象中的属性值,组合成新数组
    pluck : function(target,name){
        var result = [],
            temp;
        target.forEach(function(item){
            temp = item[name];
            if(temp != null){
                result.push(temp);
            }
        });
        return result;
    },
    //2个数组的并集
    union : function(t1,t2){
        return this.unique(t1.concat(t2));
    },
    // 取2个数组的交集
    intersect : function(t1,t2){
        return t1.filter(function(item){
            return ~t2.indexOf(item);
        });
    },
    //取差集
    diff : function(t1,t2){
        var r = t1;
        for(var i=0;i<t1.length;i++){
            for(var j=0;j<t2.length;j++){
                if(r[i] === t2[j]){
                    r.splice(i,1);
                    i--;
                    break;
                }
            }
        }
        return r;
    },
    //max
    max : function(target){
        return Array.max.apply(0,target);
    },
    //min
    min : function(target){
        return Array.min.apply(0,target);
    }

    };






    return Tool;

})()

1.3 添加浏览器判断

/**
 * 201501041558_zasqw2222
 */
;
var Tool = (function() {
    //在任何原型上添加方法
    Function.prototype.method = function(name,func){
        if(!this.prototype[name]){
            this.prototype[name] = func;
        }
    };
    //数组添加indexOf方法
    Array.method('indexOf',function(item){
        var i = 0;
            len = this.length;
        for(;i < len;i++){
            if(this[i] === item){
                return i;
            }
        }
        return -1;
    });
    //函数添加bind修改上下文方法
    Function.method('bind',function(){
        var args = [].slice.call(arguments),
            _this = args[0],
            args = args.slice(1),
            that = this;
        return function(){
            return that.apply(_this,args);
        }
    });
    // 绑定
    // Function.method('bind',function(that){
    //     var _this = this,
    //         args = Array.prototype.slice.apply(arguments,[1]);
    //     return function(){
    //         return _this.apply(that,args.concat.apply(Array.prototype.slice.apply(arguments,[0])));
    //     }

    // });



    // 
    /**
     * [ConFig 直接把对象挂载到win的属性上]
     */
    function ConFig(){
        window.tstr = Tool.str;
        window.tarr = Tool.arr;
        window.type = type;
        window.tbrower = brower;
    }
    // 类型判断
    var type = {
        version: 'v1.2',
        getType: function(ele) {
            if (window == document && document != window) {
                return 'window';
            } else if (ele.nodeType === 9) {
                return 'document';
            } else if (ele.callee) {
                return 'arguments';
            } else if (isFinite(ele.length) && ele.item) {
                return 'NodeList';
            } else {
                var type = Object.prototype.toString.call(ele),
                    reg = /\[object (.*)\]/,
                    arr = reg.exec(type);
                return arr[1];
            }
        },
        isArray : function(ele){
            return (this.getType(ele) === 'Array') ? true : false;
        },
        isFunction : function(ele){
            return (this.getType(ele) === 'Function') ? true : false;
        },
        isObject : function(ele){
            return (this.getType(ele) === 'Object') ? true : false;
        },
        isString : function(ele){
            return (this.getType(ele) === 'String') ? true : false;
        },
        isNumber : function(ele){
            return (this.getType(ele) === 'Number') ? true : false;
        },
        isBoolen : function(ele){
            return (this.getType(ele) === 'Boolean') ? true : false;
        },
        isUndefined : function(ele){
            return (this.getType(ele) === 'Undefined') ? true : false;
        },
        isNull : function(ele){
            return (this.getType(ele) === 'Null') ? true : false;
        }
    };
    // 浏览器判断
    var brower = {
        version : '1.0',
        isIe : function(){
            var isVersion = eval("''+/*@cc_on" + " @_javascript_version@*/-0") * 1,
                _isIE = /*@cc_on!@*/!1;
            return {
                ie9 : isVersion === 5.9,
                ie8 : isVersion === 5.8,
                ie7 : isVersion === 5.7,
                ie6 : isVersion === 5.6,
                ie5 : isVersion === 5.5,
                ie10 : window.navigator.msPointerEnabled,
                ie11 : '-ms-scroll-limit' in document.documentElement.style || !!window.MSInputMethodContext,
                opera : !!window.opera,
                ie : _isIE
            }
        },
        isIphone : function(){
            return /iphone/i.test(navigator.userAgent);
        },
        isIpad : function(){
            return /ipad/i.test(navigator.userAgent);       
        },
        isAndroid :function(){
            return /android/i.test(navigator.userAgent);
        },
        isIos : function(){
            return this.isIpad || this.isIphone;
        }
    }
    //原型
    var Tool = function() {
        if (!(this instanceof Tool)) {
            return new Tool;
        }
    };
    //字符串方法
    Tool.str = {
        //判定一个字符串是否包含另一个字符串
        contains: function(target, item) {
            return target.indexOf(item) != -1;
            //return target.indexOf(item) > -1;
        },
        //一般使用在对于类名的判断;
        containsClass: function(target, item, separator) {
            return separator ? (separator + target + separator).indexOf(separator + item + separator) > -1 : this.contains(target, item);
        },
        //参数2是参数1的开头么?
        startsWith: function(target, item, ignorecase) {
            var str = target.slice(0, item.length);
            return ignorecase ? str.toLowerCase() === item.toLowerCase() : str === item;
        },
        //参2是参1的结尾么?
        endsWith: function(target, item, ignorecase) {
            var str = target.slice(-(item.length));
            return ignorecase ? str.toLowerCase() === item.toLowerCase() : str === item;
        },
        //重复item,times次
        repeat: function(item, times) {
            var s = item,
                target = '';
            while (times > 0) {
                if (times % 2 == 1) {
                    target += s;
                }
                if (times == 1) {
                    break;
                }
                s += s;
                times = times >> 1;
            }
            return target;
            //retrun new Array(times).join(item)
        },
        //获得字符串字节长度
        byteLen: function(str, charset) {
            var target = 0,
                charCode,
                i,
                len;
            charset = charset ? charset.toLowerCase() : '';
            if (charset === 'utf-16' || charset === 'utf16') {
                for (i = 0, len = str.length; i < len; i++) {
                    charCode = str.charCodeAt(i);
                    if (charCode <= 0xffff) {
                        target += 2;
                    } else {
                        target += 4;
                    }
                }
            } else {
                for (i = 0, len = str.length; i < len; i++) {
                    charCode = str.charCodeAt(i);
                    if (charCode <= 0x007f) {
                        target += 1;
                    } else if (charCode <= 0x07ff) {
                        target += 2;
                    } else if (charCode <= 0xffff) {
                        target += 3;
                    } else {
                        target += 4;
                    }
                }
            }
            return target;
        },
        //字符串截断方法
        truncate: function(target, len, truncation) {
            len = len || 30;
            truncation = truncation ? truncation : '...';
            return (target.length > len) ? target.slice(0, (len - truncation.length)) + truncation : target.toString();
        },
        //_ - 转驼峰命名
        camelize: function(target) {
            if (target.indexOf('-') < 0 && target.indexOf('_') < 0) {
                return target;
            }
            return target.replace(/[-_][^-_]/g, function(match) {
                console.log(match)
                return match.charAt(1).toUpperCase();
            })
        },
        //转成下划线方法
        underscored : function(target){
            return target.replace(/([a-z0-9])([A-Z])/g,'$1_$2').toLowerCase();
        },
        //转换成连字符模式
        dasherize : function(target){
            return this.underscored(target).replace(/_/g,'-');
        },
        //首字母大写
        capitalize : function(target){
            return target.charAt(0).toUpperCase() + target.slice(1).toLowerCase();
        },
        //去掉script中的内容和Html标签
        stripTags : function(target){
            if(type.getType(target) === 'String'){
                return target.replace(/<script[^>]*>(\S\s*?)<\/script>/img,'').replace(/<[^>]+>/g,'');
            }
        },
        //填补0
        fillZero :function(target,n){
            var z = new Array(n).join('0'),
                str = z + target,
                result = str.slice(-n);
            return result;
            //return (Math.pow(10,n) + '' + target).slice(-n);
        },
        // print
        print : function(str,object){
            var arr = [].slice.call(arguments,1),
                index;
            return str.replace(/#{([^{}]+)}/gm,function(match,name){
                index = Number(name);
                if(index >= 0){
                    return arr[index];
                }
                if(object && object[name] !== ''){
                    return object[name];
                }
                return '';
            })
        },
        //去空格
        trim : function(str){
            str = str.replace(/^\s+/,'');
            for(var i =str.length - 1;i >=0;i--){
                if(/\S/.test(str.charAt(i))){
                    str = str.slice(0,i + 1);
                    break;
                }
            }
            return str;
        }
    };
    // 数组方法
    Tool.arr = {
        //是否包含指定元素
        contains : function(target,item){
            return target.indexOf(item) > -1;
        },
        //在参数1中删除参数2指定的元素返回布尔
        removeAt : function(target,index){
            return !!target.splice(index,1).length;
        },
        //在参数1中删除参数2返回布尔
        remove : function(target,item){
            var index = target.indexOf(item);
            return index > -1 ? this.removeAt(target,index) : false;
        },
        //打乱数组返回新数组
        shuffle : function(target){
            var temp = target,
                j,
                x,
                i = target.length;
            for(;i>0;j = parseInt(Math.random()*i),x = target[--i],target[i] = target[j],target[j] = x){

            }
            return temp;
            //target.sort(function(){return 0.5 - Math.random()});
        },
        //在数组中随机取一个
        random : function(target){
            return target[Math.floor(Math.random() * target.length)];
        },
        //把多维数组变成一维数组

        // 数组去重
        unique : function(target){
            var temp = [];
            _that: for(var i = 0,len = target.length;i < len;i ++){
                for(var j = i + 1;j < len;j++){
                    if(target[i] === target[j]){
                        continue _that;
                    }
                }
                temp.push(target[i])
            }
            return temp;
        },
        //去除数组中的undefined和Null
        compact : function(target){
            if(!type.isArray(target)){
                throw new Error('target error type');
            }
            return target.filter(function(item){
                return item != undefined;
            })
        },
        //获取数组对象中的属性值,组合成新数组
        pluck : function(target,name){
            var result = [],
                temp;
            target.forEach(function(item){
                temp = item[name];
                if(temp != null){
                    result.push(temp);
                }
            });
            return result;
        },
        //2个数组的并集
        union : function(t1,t2){
            return this.unique(t1.concat(t2));
        },
        // 取2个数组的交集
        intersect : function(t1,t2){
            return t1.filter(function(item){
                return ~t2.indexOf(item);
            });
        },
        //取差集
        diff : function(t1,t2){
            var r = t1;
            for(var i=0;i<t1.length;i++){
                for(var j=0;j<t2.length;j++){
                    if(r[i] === t2[j]){
                        r.splice(i,1);
                        i--;
                        break;
                    }
                }
            }
            return r;
        },
        //max
        max : function(target){
            return Array.max.apply(0,target);
        },
        //min
        min : function(target){
            return Array.min.apply(0,target);
        }
    };
    //数字扩展
    Tool.num = {
        // 确保指定范围
        limit : function(target,n1,n2){
            var a = [n1,n2].sort();
            if(target < a[0]){
                target = a[0];
            }
            if(target > a[1]){
                target = a[1];
            }
            return target;
        },
        //返回在n1和n2中哪个离targer近一点
        nearer : function(target,n1,n2){
            var diff1 = Math.abs(target - n1),
                diff2 = Math.abs(target - n2);
            return diff1 < diff2 ? n1 : n2;
        }
    };   
    //类型方法对外扩展
    Tool.config = ConFig;
    return Tool;
})();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值