1 /** 2 * string utilities Not instanciable. 3 * @constructor 4 */ 5 var YStr = { 6 /** return true if 'source' begin with 'fetch'.*/ 7 beginwith : function(fetch,source){return source.indexOf(fetch)==0;}, 8 /** return true if 'source' ends with 'fetch'.*/ 9 endwith : function(fetch,source){return source.lastIndexOf(fetch)==source.length-fetch.length;}, 10 /** return source repeated iter times.*/ 11 repeat : function(source,iter){ 12 var tmpv = iter+0,sample=source+"",rst=""; 13 while(tmpv>0){ 14 if(tmpv%2)rst+=sample; 15 sample+=sample; 16 tmpv=Math.floor(tmpv/2); 17 } 18 return rst; 19 }, 20 /** return source splitted with every samples values.*/ 21 split : function(samples,source){ 22 var result=[source]; 23 for(var i in samples){ 24 var tmparr=[]; 25 for(var j in result){ 26 var tmp = result[j].split(samples[i]); 27 for(var k in tmp)tmparr.push(tmp[k]); 28 } 29 result=tmparr; 30 } 31 return result; 32 }, 33 /** return source having fetch values replaced by repla values.*/ 34 replace : function(fetch,repla,source){ 35 var f = fetch&&fetch.join ? fetch : [fetch]; 36 var r = repla&&repla.join ? repla : [repla]; 37 var rst = source+""; 38 for(var i=0;i<f.length;i++){ 39 var tabl = rst.split(f[i]+""); 40 rst = tabl.join(r[Math.min(i,r.length-1)]); 41 }return rst; 42 }, 43 /** convert object datas to it's js equivalent 44 * @param {object} object data to convert 45 * @param {int} [tabs] root tabulations(default=0). 46 * @param {int} [limit] obj parsing limit.set to more than 0 to limit and avoid stack overflow.(default=0). 47 * return {string} the result. 48 */ 49 toStr:function(object,tabs,limit,isJson){ 50 var tab=isJson?"":" "; 51 var spa=isJson?"":" "; 52 var ret=isJson?"":"\n"; 53 var ots = function(obj,level,lim){ 54 if(level==lim)return ""; 55 var rst = "";var rar = []; 56 if( typeof(obj)=="undefined"){ 57 return "null"+tab+"/*undefined*/"; 58 }else 59 if( obj+""=="null" || obj+""=="true" || obj+""=="false" || parseFloat(obj+"")+""==obj+""){ 60 return obj+""; 61 }else if(obj && obj.join && !array_is_indexed(obj)){ 62 for(var i in obj){rar.push(str_repeat(tab,level+1)+""+ots(obj[i],level+1,lim) );} 63 return "["+ret+rar.join(","+ret)+str_repeat(tab,level)+"]"; 64 }else if( obj && (typeof(obj)=="object" || obj.join) && !obj.tagName){ 65 for(var i in obj){rar.push(str_repeat(tab,level+1)+"\""+YStr.replace(["\\","\r","\n","\""],["\\\\","\\r","\\n","\\\""],i)+"\""+spa+":"+spa+""+ots(obj[i],level+1,lim) );} 66 return "{"+ret+rar.join(","+ret)+str_repeat(tab,level)+"}"; 67 }else if(typeof(obj)=="string"){ 68 return "\""+YStr.replace(["\\","\r","\n","\""],["\\\\","\\r","\\n","\\\""],obj)+"\""; 69 }else if(!obj || obj.tagName){ 70 return "'"+obj+"'"; 71 }else{ 72 return obj+""; 73 } 74 }; 75 return ots(object, typeof(tabs)=="number"?tabs:0, typeof(limit)=="number"?limit:-1); 76 }, 77 toCleanFileName : function(filename){ 78 var result = ""; 79 var enabled = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._"; 80 var replp = { 81 "e":"éè", 82 "a":"à" 83 }; 84 for(var i=0;i<filename.length;i++){ 85 var chr = filename.charAt(i); 86 if(enabled.indexOf(chr)==-1){ 87 var replaced = false; 88 for(var cr in replp){ 89 if(replp[cr].indexOf(chr)>-1){ 90 chr = cr; 91 replaced = true; 92 break; 93 } 94 } 95 if(!replaced)chr = "_"; 96 } 97 result+=chr; 98 } 99 return result; 100 }, 101 isCleanFileName : function(filename){ 102 var cfn = YStr.toCleanFileName(filename); 103 return cfn==filename; 104 } 105 }; 106 var str_beginwith = YStr.beginwith; 107 var str_endwith = YStr.endwith; 108 var str_repeat = YStr.repeat; 109 var str_repeat2 = str_repeat 110 var str_replace = YStr.replace; 111 var objToStr = YStr.toStr; 112 var objToJson = function(object,tabs,limit){return YStr.toStr(object,tabs,limit,true);}; 113