공통 stringUtil 도구

1410 단어 String
(function(){
	var ISCHINESE = /[\u4e00-\u9fa5]/;

	var getData =  function( value , maxLenth , isStrick ){
		value = value + '';
		var total = 0 , legelIndex = value.length;
		if( !isStrick ){ 
			return {
				total : legelIndex ,
				index : legelIndex
			};
		}
		for( var i = 0 , len = value.length ;i  < len ; i ++ ){
			var key = value.charAt( i );
			total += ISCHINESE.test(key) ? 2 : 1;
			maxLenth && total <= maxLenth &&( legelIndex = i+1 );
		}
		return {
			total : total ,
			index : legelIndex
		};
	}

	var stringUtil = {
		/**
		 *  , 
		 * @param  {[type]}  value     
		 * @param  {[type]}  maxLength    
		 * @param  {Boolean} isStrick  
		 * @return {[type]}            
		 */
		getLegalLengthValue : function( maxLenth , isStrick ){
              var value = this; var data = getData( value , maxLenth , isStrick ); return value.substring( 0 , data.index ); }, getLength : function( isStrick ){
             var value = this; var data = getData( value , false , isStrick ); return data.total; } }; for( var i in stringUtil ){String.prototype[i] = stringUtil[i] }
})()

좋은 웹페이지 즐겨찾기