Javascript开发app常用的一些函数

原创
2017/01/07 08:42
阅读数 125

调用sqlite时需要处理的特殊字符

/****/
function htmlspecialchars (s){
		
		if(!s || typeof(s)!='string')
		{
			return s;
		}
		s = s + '#';
		s = s.replace(/['"\r\n\\]?/mg,function(tokens,index,currentStr){
	    if(tokens.charCodeAt(0)==34)
	    {
	        return '\\'+tokens; //双引号
	    }else if(tokens.charCodeAt(0)==39){
	    	return '\\'+tokens+'\\'+tokens; //单引号
	    }else if(tokens.charCodeAt(0)==13){
	       return '\\r';
	    }else if(tokens.charCodeAt(0)==10){
	       return '\\n';
	    }else if(tokens.charCodeAt(0)==0){
      		return '\\0';
    	}else if(tokens.charCodeAt(0)==92){
      		return '\\\\';
    	}
   		return tokens;
		});
		s = s.substring(0,s.length-1);
		return s;
	};

字符串处理

/**
* 删除左右两端的空格
*/
String.prototype.trim=function(){
    return this.replace(/(^\s*)|(\s*$)/g, "");
}
/**
* 删除左边的空格
*/
String.prototype.ltrim=function(){
    return this.replace(/(^\s*)/g, "");
}
/**
* 删除右边的空格
*/
String.prototype.rtrim=function(){
    return this.replace(/(\s*$)/g, "");
}

数组数据处理

Array.prototype.remove = function(i){
    if(isNaN(i) || i < 0 || i >= this.length){
	    return this;
	}
	this.splice(i,1);
	return this;
}
Array.prototype.remove2 = function(i){
    if(isNaN(i))
	    return this;
	if(i < 0 || i >= this.length)
	    return this;
	else
	    return this.slice(0,i).concat(this.slice(i+1,this.length));
}
Array.prototype.remove3 = function(dx){
    if(isNaN(dx) || dx > this.length){
		return false;
	}
	for(var i=0,n=0;i<this.length;i++){
		if(this[i]!=this[dx]){
			this[n++]=this[i];
		}
	}
	this.length-=1;
}
Array.prototype.insert = function (i, item){
  return this.splice(i, 0, item);
}

日期格式化处理

Date.prototype.format = function(format){
	// (new Date()).format("yyyy-MM-dd hh:mm:ss")
    var o = {
        "M+" : this.getMonth()+1, //month 
        "d+" : this.getDate(), //day 
        "h+" : this.getHours(), //hour 
        "m+" : this.getMinutes(), //minute 
        "s+" : this.getSeconds(), //second 
        "q+" : Math.floor((this.getMonth()+3)/3), //quarter 
        "S" : this.getMilliseconds() //millisecond 
    }

    if(/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
    }

    for(var k in o) {
        if(new RegExp("("+ k +")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
        }
    }
    return format;
} 

Map数据结构

Map = function() {
    var struct = function(key, value) {
        this.key = key;
        this.value = value;
    }
    
    var put = function(key, value){
        for (var i = 0; i < this.arr.length; i++) {
            if ( this.arr[i].key === key ) {
                this.arr[i].value = value;
                return;
            }
        }
        this.arr[this.arr.length] = new struct(key, value);
    }
    
    var get = function(key) {
        for (var i = 0; i < this.arr.length; i++) {
            if ( this.arr[i].key === key ) {
                return this.arr[i].value;
            }
        }
        return null;
    }
    
    var remove = function(key) {
        var v;
        for (var i = 0; i < this.arr.length; i++) {
            v = this.arr.pop();
            if ( v.key === key ) {
                continue;
            }
            this.arr.unshift(v);
        }
    }
    
    var size = function() {
        return this.arr.length;
    }
    
    var isEmpty = function() {
        return this.arr.length <= 0;
    }
    this.arr = new Array();
    this.get = get;
    this.put = put;
    this.remove = remove;
    this.size = size;
    this.isEmpty = isEmpty;
}

JSON处理

JSON.isJSON = JSON.isJSON || function(obj){
		if(Object.prototype.toString.call(obj) === '[object Object]'){
			try{
				var str1 = JSON.stringify(obj);
				var str2 = JSON.stringify(JSON.parse(JSON.stringify(obj)));
				return str1==str2;
			}catch(e){
				return false;
			}
		}
	}
	JSON.tryParse = JSON.tryParse || function(str){
		try{
			var json = JSON.parse(str);
			return true;
		}catch(e){
			return false;
		}
	}
	JSON.tryParseJSON = JSON.tryParseJSON || function(str){
		try{
			var obj = JSON.parse(str);
			if(Object.prototype.toString.call(obj) === '[object Object]'){
				return true;
			}
			return false;
		}catch(e){
			return false;
		}
	}

uuid生成

function uuid(len, radix) {
    var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
    var uuid = [], i;
    radix = radix || chars.length;
 
    if (len) {
      // Compact form
      for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random()*radix];
    } else {
      // rfc4122, version 4 form
      var r;
 
      // rfc4122 requires these characters
      uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
      uuid[14] = '4';
 
      // Fill in random data.  At i==19 set the high bits of clock sequence as
      // per rfc4122, sec. 4.1.5
      for (i = 0; i < 36; i++) {
        if (!uuid[i]) {
          r = 0 | Math.random()*16;
          uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
        }
      }
    }
 
    return uuid.join('');
}

 

 

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部