This repository was archived by the owner on Jul 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStringFunctions.js
More file actions
637 lines (545 loc) · 16.9 KB
/
StringFunctions.js
File metadata and controls
637 lines (545 loc) · 16.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
var StringFunctions = {} ;
var EnglishEstesna = ['’'];
var arabicChars = 'ًٌٍَُِّْٰٜٕٖۣٞٝٓٔٗ٘ٙٚٛؕؔؓؐؑؒۖۘۗۙۚۛۜۢـ?',
arabicSames = ['ؤو','11','22','33','44','55','66','77','88','99','00',
'0٠۰','1١۱','9٩۹','8٨۸','7٧۷','6٦۶','5٥۵','4٤۴','3٣۳','2٢۲'
,'ييیىئ','اإأآ?','کك'],
arabicWord = 'والي';
/**@desc returns true if there was to many arabic signes there<br>
* if the number of arabic sign was less than 1/4 , this function detect that the str is not arabic*/
StringFunctions.isArabic = function(str="")
{
var reg = new RegExp('['+arabicChars+']','g');
var founced = 0 ;
var L = Math.min(50,str.length) ;
var searchResult = reg.exec(str);
while(searchResult!==null && reg.lastIndex<L)
{
founced++ ;
searchResult = reg.exec(str);
}
if(founced>L/4)
{
return true ;
}
else
{
return false ;
}
}
/**@desc Returns true if currenct string has at least one persian script.
* @returns {Boolean}
*/
StringFunctions.isPersian = function(str="",stringLength=NaN)
{
var max = 0;
if(isNaN(stringLength))
{
max = Math.min(str.length , 200);
}
else
{
max = Math.min(str.length , stringLength) ;
}
for(var i = 0 ; i<max ; i++)
{
if(str.charCodeAt(i)>1000 && EnglishEstesna.indexOf(str.charAt(i))===-1)
{
//console.log('This is arabic : '+str.charAt(i)+' at '+i+' the char code is : '+str.charCodeAt(i));
return true ;
}
}
return false;
}
/**@desc it will returns list of points that shows index and ofsset of each word founded > [{x:0,y:0}]*/
StringFunctions.search = function(str="",searchedWord="",fineAll = true,arabic=false, arabic2=false)
{
var founded = [{x:0,y:0}];
founded = [] ;
if(str === '' || str === ' ')
{
return founded ;
}
if(arabic)
{
//arabic search has problems
var regularEx = '' ;
var L = searchedWord.length ;
var arabChars = arabicChars ;
if(arabic2)
{
arabChars+= arabicWord ;
}
for(var i = 0 ; i<L ; i++)
{
var char = searchedWord.charAt(i);
for(var j = 0 ; j<arabicSames.length ; j++)
{
if(arabicSames[j].indexOf(char)!==-1)
{
char = '['+arabicSames[j]+']' ;
break ;
}
}
regularEx += char;
if(i<L-1)
{
regularEx+='['+arabChars+']*';
}
}
var reg = new RegExp(regularEx,'g');
var searchResult = reg.exec(str);
while(searchResult!==null)
{
founded.push({x:searchResult.index,y:reg.lastIndex});
searchResult = reg.exec(str);
if(!fineAll)
{
break ;
}
}
}
else
{
var f=-1 ;
while((f=str.indexOf(searchedWord,f+1))!==-1)
{
founded.push({x:f,y:f+searchedWord.length});
if(!fineAll)
{
break ;
}
}
}
return founded ;
}
//////////////////////////////////////////////////link generators↓
/**@desc generate link on the current string and it will returns html text*/
StringFunctions.generateLinks = function(str="",linkColors=-1)
{
var colorTagStart = '';
var colorTagEnd = '';
if(linkColors!==-1)
{
colorTagStart = '<font color="#'+linkColors.toString(16)+'">';
colorTagEnd = '</font>';
}
//debug telephone
//console.log('phone enabled');
var regNumberDetection = /([\n\r\s\t,^])([\d-+]{7,})/gi;//([\n\r\s\t,])([\d-]{8,})([\t\n\r\s,])
console.log("Find the phone : "+str);
str = str.replace(regNumberDetection,'$1'+colorTagStart+'<a href="tel:$2">$2</a>'+colorTagEnd);//
var regURLDetect = /(www|http:\/\/)[^ \n\r]*/gi ;///(www\. | http)\S*\s/gi;//TODO it has problem! if you pass an html with href= tag on it, it will break up the complete html
str = str.replace(regURLDetect,colorTagStart+'<a href="http://$&">$&</a>'+colorTagEnd);
//var regURLDetect2:RegExp = /http\S*\s/gi;
//str = str.replace(regURLDetect2,'<font color="'+linkColors+'"><a href="$&">$&</a></font>');
var regDetectEmail = /[a-z.\-1234567890_]*@[a-z.\-_]*/gi ;
str = str.replace(regDetectEmail,colorTagStart+'<a href="mailto:$&">$&</a>'+colorTagEnd);
var doubleHTTP = /http:\/\/[ ]*http:\/\//gi;
str = str.replace(doubleHTTP,'http://');
return str ;
}
/**@desc *Clear in line "s in the json values:<br>
* {"data":"my name is "ali"."}<br>
* {"data":"my name is \"ali\"."}*/
StringFunctions.clearDoubleQuartmarksOnJSON = function(str="")
{
//var str:String = '[{"IdNews":"585","DateNews":"1394\\/8\\/20 ","SubjectNews":"fdjsakl fjk\\"adsl jfkldsa ","ImageNews":"http:\\/\\/www.melkban24.ir\\/Files\\/News585.jpg"},{"IdNews":"584","DateNews":"1394\\/8\\/20 ","SubjectNews":"fsjdkl klfsad jklfsjadk ljfklds","ImageNews":"http:\\/\\/www.melkban24.ir\\/Files\\/News584.jpg"},{"IdNews":"583","DateNews":"1394\\/8\\/19 ","SubjectNews":"fdjks jkfjd skfkjdkslfj jkfd f","ImageNews":"http:\\/\\/www.melkban24.ir\\/Files\\/News583.jpg"},{"IdNews":"582","DateNews":"1394\\/8\\/19 ","SubjectNews":"fdjfk kfjd lfdk lfkdsjkfkdfkls jkf","ImageNews":"http:\\/\\/www.melkban24.ir\\/Files\\/News582.jpg"},{"IdNews":"581","DateNews":"1394\\/8\\/18 ","SubjectNews":"مjkf jkfdjsk jkfldjkfld kfdjkfdjk","ImageNews":"http:\\/\\/www.melkban24.ir\\/Files\\/News581.jpg"},{"IdNews":"580","DateNews":"1394\\/8\\/18 ","SubjectNews":"fksjdf kfjds klfjkdlfkdsj f sf sfd","ImageNews":"http:\\/\\/www.melkban24.ir\\/Files\\/News580.jpg"},{"IdNews":"579","DateNews":"1394\\/8\\/18 ","SubjectNews":"fdskl jfsdkj kfdsjk jkflfdks","ImageNews":"http:\\/\\/www.melkban24.ir\\/Files\\/News579.jpg"},{"IdNews":"578","DateNews":"1394\\/8\\/18 ","SubjectNews":"fdsa fad" sfdsa"fdfsaf","ImageNews":"http:\\/\\/www.melkban24.ir\\/Files\\/News578.jpg"},{"IdNews":"577","DateNews":"1394\\/8\\/17 ","SubjectNews":"jisfad jkfsdjakj lfasjfdjfsdj kfsdjkl jkf","ImageNews":"http:\\/\\/www.melkban24.ir\\/Files\\/News577.jpg"},{"IdNews":"576","DateNews":"1394\\/8\\/17 ","SubjectNews":"fdf afd fsadfdsaf afs df asfsda fsda ","ImageNews":"http:\\/\\/www.melkban24.ir\\/Files\\/News576.jpg"}]';
var regexp = /(":"((?!"\},\{)(?!",")(?!"\}\])(?!"\})(.))*[^\\])"((?!\},\{)(?!,")(?!\}\])(?!\}))/gi
var lastStr = "" ;
do
{
lastStr = str ;
str = str.replace(regexp,'$1\\"')
}while(str!==lastStr)
return str ;
}
/**@desc This function will remove all spaces and tabs and enters from a string*/
StringFunctions.clearSpacesAndTabs = function(str="")
{
if(str===null)
return '' ;
return str.split('\n').join('').split('\r').join('').split('\t').join('').split(' ').join('');
}
/**@desc This function will remove all spaces and tabs and enters from a string*/
StringFunctions.clearSpacesAndTabsAndArrows = function(str="")
{
if(str===null)
return '' ;
return str.split('\n').join('').split('\r').join('').split('\t').join('').split(' ').join('').split('<').join('').split('>').join('');
}
///////////////////New Funciton on String funciont
StringFunctions.utfToUnicode = function(utfString="")
{
var reg = /u[0-9a-f][0-9a-f][0-9a-f][0-9a-f]/gi;
var searchResult = reg.exec(utfString);
var correctedString = '' ;
var index = 0 ;
var lastIndex = Infinity ;
var currentIndex ;
while(searchResult!==null)
{
lastIndex = reg.lastIndex ;
currentIndex = searchResult.index;
correctedString += utfString.substring(index,currentIndex)+StringFunctions.correctUTF(utfString.substring(currentIndex,lastIndex));
index = lastIndex ;
searchResult = reg.exec(utfString);
}
correctedString+=utfString.substring(index);
return correctedString ;
}
StringFunctions.correctUTF = function(utfWord="")
{
var num = utfWord.substring(1) ;
return String.fromCharCode(parseInt(num,16)) ;
}
/////////////////////////////////////////////////Sumerize texts
/**@desc This function will shorten the senteces by the len vlaue*/
StringFunctions.short = function(str="",len=10,removeEntersWith='')
{
if(str===null)
{
return '' ;
}
if(removeEntersWith!=='')
{
str = str.split('\r').join('\n').split('\n\n').join('\n').split('\n').join(removeEntersWith);
}
var dotString = '...';
var spaceIndex = str.indexOf(' ',len-dotString.length);
if(spaceIndex === -1)
{
if(str.length>len)
{
return str.substring(0,len-dotString.length)+dotString;
}
else
{
return str ;
}
}
else
{
if(spaceIndex>=str.length)
{
//remove donts from the end
dotString='';
}
return str.substr(0,spaceIndex)+dotString;
}
}
////////////////////////////////////////////////
/**@desc This function will make inserted html understandable for UnicodeConvertor
StringFunctions.htmlCorrect = function(htm="",linkColor=-1,replacePwithEnter=false,fontSizeIs=20)
{
return Unicode.htmlCorrect(htm,linkColor,replacePwithEnter,fontSizeIs)
}*/
///////////////////////////////////////////////////////Time functions
/**@desc return the time from seconds to string 10:54*/
StringFunctions.timeInString = function(seconds=0)
{
seconds = Math.ceil(seconds);
var min = Math.floor(seconds/60);
seconds = seconds%60;
var hour = Math.floor(min/60);
min = min%60;
if(hour>0)
{
return StringFunctions.numToString(hour)+':'+StringFunctions.numToString(min)+':'+StringFunctions.numToString(seconds);
}
else
{
return StringFunctions.numToString(min)+':'+StringFunctions.numToString(seconds);
}
}
/**@desc 1 > 001*/
StringFunctions.numToString = function(num,numberLenght=2)
{
num = String(num);
while(num.length<numberLenght)
{
num = '0'+num;
}
return num;
}
/**@desc Remove all html tags from the text*/
StringFunctions.clearHTMLTags = function(ReferText="")
{
return StringFunctions.removeHTML(ReferText);
}
/**@desc Remove all html tags from the text*/
StringFunctions.removeHTML = function(ReferText="")
{
if(ReferText===null)
{
return '' ;
}
var htmlDeleter = /<\/?[^>]*>/gi;
return ReferText.replace(htmlDeleter,'');
}
/**@desc Returns -1 if string1 < str2, 1 if str1>str2*/
StringFunctions.compairFarsiString = function(str1="",str2="")
{
str1 = str1.toLowerCase();
str2 = str2.toLowerCase();
if(str1 === null)
{
str1 = '';
}
if(str2 === null)
{
str2 = '' ;
}
if(str1==='' && str2==='')
{
return 0 ;
}
if(str1==='' || str1 ===null)
{
return -1 ;
}
if(str2==='' || str2===null)
{
return 1 ;
}
var alephba = "ابپتثجچهخدذرزژسشصضطظعغفقكگلمنوهیي";
var farsiStr1 = StringFunctions.KaafYe(str1);
var farsiStr2 = StringFunctions.KaafYe(str2);
var index1 = alephba.indexOf(farsiStr1.charAt(0));
var index2 = alephba.indexOf(farsiStr2.charAt(0));
if(index1===-1 || index2 ===-1)
{
if(str1<str2)
{
return -1 ;
}
else if(str1>str2)
{
return 1 ;
}
else
{
return 0 ;
}
}
if(index1<index2)
{
return -1 ;
}
else if(index1>index2)
{
return 1 ;
}
else
{
return 0 ;
}
}
StringFunctions.htmlCharacterEncoder = function(str="")
{
var _htmlCar = [{from:"«",to:"«"},{from:"»",to:"»"},{from:" ",to:" "},{from:'<',to:"<"},{from:">",to:">"},{from:"&",to:"&"},{from:""",to:"\\\""},{from:"'",to:"'"},{from:"¢",to:"¢"},{from:"£",to:"£"},{from:"¥",to:"¥"},{from:"€",to:"€"},{from:"©",to:"©"},{from:"®",to:"®"},{from:"‌",to:" "}]
for(var i=0;i<_htmlCar.length;i++)
{
console.log('from :',_htmlCar[i].from,'to :',_htmlCar[i].to)
str = str.split(_htmlCar[i].from).join(_htmlCar[i].to)
}
return str
}
/**@desc this function will remove all persian ک and ی and will repace them with ي and ك*/
StringFunctions.KaafYe = function(str="")
{
return str.split('ی').join('ي').split('ک').join('ك').split('ى').join('ي');
}
StringFunctions.jsonCorrector = function(oldJson="")
{
return oldJson.split('\n').join(' \\n').split('\r').join(' \\r').split('"').join('"').split('\t').join('\\t')
}
/**@desc *0902-hello > hello . it will remove any number befor - sign. if there was a - sign<br>
* 0000helo > 0000helo<br>
* abs02-hello > abs02-hello*/
StringFunctions.removeNumberFromBegining = function(str="")
{
var firstDashIndex = str.indexOf('-');
if(firstDashIndex!==-1)
{
for(var i = 0 ; i<firstDashIndex ; i++)
{
if(isNaN(Number(str.charAt(i))))
{
console.log("---there is no number befor - sign in : "+str);
return str ;
}
}
return str.substr(firstDashIndex+1);
}
return str ;
}
////////////////////////////String controll
/**@desc Returns true if this was an email*/
StringFunctions.isEmail = function(email="")
{
var reg = /^[\w.-]+@\w[\w.-]+\.[\w.-]*[a-z][a-z]$/i;
return reg.test(email);
}
/**@desc Returns sized html text*/
StringFunctions.makeHTMLWithSize = function(pureHML="", defaultFontSize=0)
{
//<P ALIGN="LEFT"><FONT FACE="B Yekan Bold Bold" SIZE="38" COLOR="#000000" LETTERSPACING="0" KERNING="1">f<FONT SIZE="96">s</FONT>d</FONT></P>
return '<FONT SIZE="'+defaultFontSize+'">'+pureHML+'</FONT>';
}
////////////////////////////////////////////////////////
/**@desc Returns a domain of an url : www.google.com/translage >> google.com*/
StringFunctions.findMainDomain = function(url="",removeHTTPPart=true)
{
var founded = url.match(/^((http(s|):\/\/)|)[^/^:^\r^\n]+/);
if(founded===null || founded.length===0)
{
return '';
}
var theDomain = String(founded[0]).toLowerCase();
if(removeHTTPPart)
{
theDomain = theDomain.split('https://').join('').split('http://').join('').split('www.').join('')
}
return theDomain ;
}
/**@desc Return positive number if port founded*/
StringFunctions.findPortOfURL = function(url="")
{
var founded = url.match(/:[1234567890]+\//) ;
var portPart ;
if(founded===null || founded.length ===0 || (portPart = founded[0]).length<3)
{
return -1 ;
}
var portPartNumber = Number(portPart.substring(1,portPart.length-1));
if(isNaN(portPartNumber))
{
return -1 ;
}
else
{
return portPartNumber ;
}
}
/**@desc It will replace ي with ی and correct numbers. */
StringFunctions.correctInputString = function(str="")
{
return StringFunctions.KaafYe(StringFunctions.numCorrection(str));
}
StringFunctions.numCorrection = function(str="")
{
var I = String('۰').charCodeAt(0);
for(var i=I ; i<I+10 ; i++){
str = str.split(String.fromCharCode(i)).join(String(i-I));
}
I = String('٠').charCodeAt(0);
for(i=I ; i<I+10 ; i++){
str = str.split(String.fromCharCode(i)).join(String(i-I));
}
return str ;
}
/**@desc Returns true if entered string was URL*/
/**@desc Returns true if entered string was URL*/
StringFunctions.isURL = function(str="")
{
var reg = /^(http|ftp|https):\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?$/i;
return reg.test(str);
}
StringFunctions.isLocation=function(str="")
{
var reg = /^[\d]+\.[\d]+,[\d]+\.[\d]+$/i;
return reg.test(str);
}
///////////////////////////////////////////////////////**@desc ********/////////////////////////
/**@desc 2500>>>>>>>2,500 12.5654*/
StringFunctions.currancyPrint = function(inputcurencynumber)
{
inputcurencynumber=String(inputcurencynumber);
var relPart = '' ;
var floatPart = '' ;
var splitedNumber = String(inputcurencynumber).split('.') ;
relPart = splitedNumber[0] ;
if(splitedNumber.length>1)
{
floatPart = '.'+splitedNumber[1] ;
}
else
{
floatPart = '' ;
}
inputcurencynumber = relPart ;
var s2="";
while (inputcurencynumber.length > 3) {
s2 = ',' + inputcurencynumber.substring(inputcurencynumber.length - 3, inputcurencynumber.length) + s2;
inputcurencynumber = inputcurencynumber.substring(0, inputcurencynumber.length - 3);
}
return inputcurencynumber+s2+floatPart
}
/**@desc http://www.google.com/342 >>> 342. but it will returns 0 if no number found at the end of line*/
StringFunctions.returnLasNumberPartInInteger = function(str="")
{
var matched = str.match(/[\d]+$/) ;
if(matched === null || matched.length === 0)
{
return 0 ;
}
return Math.floor(Number(matched[0])) ;
}
//////////////////////////////////STring to color
/**@desc Creats color from a link*/
StringFunctions.stringToColor = function(str="")
{
var col = 0 ;
for(var i = 0 ; i<str.length ; i++)
{
col+=str.charCodeAt(i)*17.7589894;
}
//console.log("col : "+col);
var lastCol = col%10 ;
var maxRedColor = (lastCol<=3)?0xac0:10 ;
var maxGreenColor = (lastCol>3 && lastCol<=6)?0xc0:10 ;
var maxBlueColor = (lastCol>6)?0xc0:10 ;
//console.log("maxRedColor : "+maxRedColor)
//console.log("maxGreenColor : "+maxGreenColor)
//console.log("maxBlueColor : "+maxBlueColor)
var red = (lastCol*87789.15484848)%maxRedColor+0x40;
var gre = (lastCol*55.15641498)%maxGreenColor+0x40;
var blu = (lastCol*99.3894516)%maxBlueColor+0x40;
//console.log("red : "+red.toString(16));
//console.log("gre : "+gre.toString(16));
//console.log("blu : "+blu.toString(16));
return red*0x010000+gre*0x000100+blu ;
}
/**@desc Remove spaces from two side of the inputed string : " hello world " > "hello world" */
StringFunctions.removeSpacesFromTwoSides=function(str="")
{
str = str.replace(/[\s]+$/g,'');
str = str.replace(/^[\s]+/g,'');
return str ;
}
/**@desc Returning file size in String with lable*/
StringFunctions.fileSizeInString = function(fileSizeInByte=0)
{
if(isNaN(fileSizeInByte))
{
return '' ;
}
if(fileSizeInByte<1000)
{
return Math.round(fileSizeInByte)+' B';
}
else if(fileSizeInByte<1000*1000)
{
return Math.round(fileSizeInByte/1000)+" K";
}
else if(fileSizeInByte<1000*1000*1000)
{
return Math.round(fileSizeInByte/(1000*1000))+" M";
}
else
{
return Math.round(fileSizeInByte/(1000*1000*1000))+' G';
}
}
export default StringFunctions ;