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.ts
More file actions
799 lines (697 loc) · 21.3 KB
/
StringFunctions.ts
File metadata and controls
799 lines (697 loc) · 21.3 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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
interface StringFunctionsModel {
isNullOrEmpty:typeof isNullOrEmpty,
isArabic:typeof isArabic,
isPersian:typeof isPersian,
search:typeof search,
generateLinks:typeof generateLinks,
clearDoubleQuartmarksOnJSON:typeof clearDoubleQuartmarksOnJSON,
clearSpacesAndTabs:typeof clearSpacesAndTabs,
clearSpacesAndTabsAndArrows:typeof clearSpacesAndTabsAndArrows,
utfToUnicode:typeof utfToUnicode,
correctUTF:typeof correctUTF,
short:typeof short,
timeInString:typeof timeInString,
numToString:typeof numToString,
clearHTMLTags:typeof clearHTMLTags,
removeHTML:typeof removeHTML,
compairFarsiString:typeof compairFarsiString,
htmlCharacterEncoder:typeof htmlCharacterEncoder,
KaafYe:typeof KaafYe,
jsonCorrector:typeof jsonCorrector,
removeNumberFromBegining:typeof removeNumberFromBegining,
isEmail:typeof isEmail,
makeHTMLWithSize:typeof makeHTMLWithSize,
findMainDomain:typeof findMainDomain,
findPortOfURL:typeof findPortOfURL,
correctInputString:typeof correctInputString,
numCorrection:typeof numCorrection,
isURL:typeof isURL,
isLocation:typeof isLocation,
currancyPrint:typeof currancyPrint,
returnLasNumberPartInInteger:typeof returnLasNumberPartInInteger,
stringToColor:typeof stringToColor,
removeSpacesFromTwoSides:typeof removeSpacesFromTwoSides,
fileSizeInString:typeof fileSizeInString,
}
var StringFunctions:StringFunctionsModel = {
isNullOrEmpty:isNullOrEmpty,
isArabic:isArabic,
isPersian:isPersian,
search:search,
generateLinks:generateLinks,
clearDoubleQuartmarksOnJSON:clearDoubleQuartmarksOnJSON,
clearSpacesAndTabs:clearSpacesAndTabs,
clearSpacesAndTabsAndArrows:clearSpacesAndTabsAndArrows,
utfToUnicode:utfToUnicode,
correctUTF:correctUTF,
short:short,
timeInString:timeInString,
numToString:numToString,
clearHTMLTags:clearHTMLTags,
removeHTML:removeHTML,
compairFarsiString:compairFarsiString,
htmlCharacterEncoder:htmlCharacterEncoder,
KaafYe:KaafYe,
jsonCorrector:jsonCorrector,
removeNumberFromBegining:removeNumberFromBegining,
isEmail:isEmail,
makeHTMLWithSize:makeHTMLWithSize,
findMainDomain:findMainDomain,
findPortOfURL:findPortOfURL,
correctInputString:correctInputString,
numCorrection:numCorrection,
isURL:isURL,
isLocation:isLocation,
currancyPrint:currancyPrint,
returnLasNumberPartInInteger:returnLasNumberPartInInteger,
stringToColor:stringToColor,
removeSpacesFromTwoSides:removeSpacesFromTwoSides,
fileSizeInString:fileSizeInString,
} ;
var EnglishEstesna:string[] = ['’'];
var arabicChars:string = 'ًٌٍَُِّْٰٜٕٖۣٞٝٓٔٗ٘ٙٚٛؕؔؓؐؑؒۖۘۗۙۚۛۜۢـ?',
arabicSames:string[] = ['ؤو','11','22','33','44','55','66','77','88','99','00',
'0٠۰','1١۱','9٩۹','8٨۸','7٧۷','6٦۶','5٥۵','4٤۴','3٣۳','2٢۲'
,'ييیىئ','اإأآ?','کك'],
arabicWord = 'والي';
function isNullOrEmpty(value:string, ignoreSpaces:boolean=false):boolean
{
if(ignoreSpaces) {
value.split(' ').join('');
}
return value === null || value === '';
}
/**@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*/
function isArabic(str:string=""):boolean
{
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}
*/
function isPersian(str:string="",stringLength:number=NaN):boolean
{
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}]*/
function search(str:string="",searchedWord:string="",fineAll:boolean = true,arabic:boolean=false, arabic2:boolean=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*/
function generateLinks(str:string, linkColors:number=-1):string
{
var colorTagStart:string = '';
var colorTagEnd:string = '';
if(linkColors!=-1)
{
colorTagStart = '<font color="#'+linkColors.toString(16)+'">';
colorTagEnd = '</font>';
}
var str:string = str;
//debug telephone
var regNumberDetection:RegExp = /([\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:RegExp = /([^"]|^)(www|http[s]?:\/\/)([^\s^\n^\r^<^\]^\[^>^"^']*)/gi ;
str = str.replace(/(http[s]?:\/\/)www\./gi,'$1');//Remove extra www
str = str.replace(regURLDetect,'<a href="$2$3">$2$3</a>');
//var regURLDetect2:RegExp = /http\S*\s/gi;
//str = str.replace(regURLDetect2,'<font color="'+linkColors+'"><a href="$&">$&</a></font>');
var regDetectEmail:RegExp = /[a-z\.\-1234567890_]*\@[a-z\.\-_]*/gi ;
str = str.replace(regDetectEmail,colorTagStart+'<a href="mailto:$&">$&</a>'+colorTagEnd);
var doubleHTTP:RegExp = /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\"."}*/
function clearDoubleQuartmarksOnJSON(str:string=""):string
{
//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*/
function clearSpacesAndTabs(str:string=""):string
{
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*/
function clearSpacesAndTabsAndArrows(str:string="")
{
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
function utfToUnicode(utfString:string=""):string
{
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 ;
}
function correctUTF(utfWord:string=""):string
{
var num = utfWord.substring(1) ;
return String.fromCharCode(parseInt(num,16)) ;
}
/////////////////////////////////////////////////Sumerize texts
/**@desc This function will shorten the senteces by the len vlaue*/
function short(str:string="",len:number=10,removeEntersWith:string='')
{
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*/
function timeInString(seconds:number=0):string
{
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 numToString(hour)+':'+numToString(min)+':'+numToString(seconds);
}
else
{
return numToString(min)+':'+numToString(seconds);
}
}
/**@desc 1 > 001*/
function numToString(num:number,numberLenght:number=2):string
{
var num2:string = String(num);
while(num2.length<numberLenght)
{
num2 = '0'+num2;
}
return num2;
}
/**@desc Remove all html tags from the text*/
function clearHTMLTags(ReferText:string=""):string
{
return removeHTML(ReferText);
}
/**@desc Remove all html tags from the text*/
function removeHTML(ReferText:string=""):string
{
if(ReferText===null)
{
return '' ;
}
var htmlDeleter = /<\/?[^>]*>/gi;
return ReferText.replace(htmlDeleter,'');
}
/**@desc Returns -1 if string1 < str2, 1 if str1>str2*/
function compairFarsiString(str1:string="",str2:string=""):number
{
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 = KaafYe(str1);
var farsiStr2 = 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 ;
}
}
function htmlCharacterEncoder(str:string=""):string
{
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 ك*/
function KaafYe(str:string=""):string
{
return str.split('ی').join('ي').split('ک').join('ك').split('ى').join('ي');
}
function jsonCorrector(oldJson:string=""):string
{
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*/
function removeNumberFromBegining(str:string=""):string
{
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*/
function isEmail(email:string="", canBeEmpty_p:boolean=false):boolean
{
if(canBeEmpty_p)
{
if(email === ''|| email === null) return true
}
if( isPersian(email) )
{
return false ;
}
var reg:RegExp = /^[\w.-]+@\w[\w.-]+\.[\w.-]*[a-z][a-z]$/i;
return reg.test(email);
}
/**@desc Returns sized html text*/
function makeHTMLWithSize(pureHML:string="", defaultFontSize:number=0):string
{
//<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*/
function findMainDomain(url:string="",removeHTTPPart:boolean=true):string
{
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*/
function findPortOfURL(url:string=""):number
{
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. */
function correctInputString(str:string=""):string
{
return StringFunctions.KaafYe(StringFunctions.numCorrection(str));
}
function numCorrection(str:string=""):string
{
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*/
function isURL(str:string=""):boolean
{
var reg = /^(http|ftp|https):\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?$/i;
return reg.test(str);
}
function isLocation(str:string=""):boolean
{
var reg = /^[\d]+\.[\d]+,[\d]+\.[\d]+$/i;
return reg.test(str);
}
///////////////////////////////////////////////////////**@desc ********/////////////////////////
/**@desc 2500>>>>>>>2,500 12.5654*/
function currancyPrint(inputcurencynumber:string):string
{
if(isNullOrEmpty(inputcurencynumber))
{
return '' ;
}
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*/
function returnLasNumberPartInInteger(str:string=""):number
{
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*/
function stringToColor(str:string=""):number
{
var col:number = 0 ;
for(var i:number = 0 ; i<str.length ; i++)
{
col+=str.charCodeAt(i)*17.7589894;
}
//console.log("col : "+col);
var lastCol:number = col%10 ;
var maxRedColor:number = (lastCol<=3)?0xac0:10 ;
var maxGreenColor:number = (lastCol>3 && lastCol<=6)?0xc0:10 ;
var maxBlueColor:number = (lastCol>6)?0xc0:10 ;
//console.log("maxRedColor : "+maxRedColor)
//console.log("maxGreenColor : "+maxGreenColor)
//console.log("maxBlueColor : "+maxBlueColor)
var red:number = (lastCol*87789.15484848)%maxRedColor+0x40;
var gre:number = (lastCol*55.15641498)%maxGreenColor+0x40;
var blu:number = (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" */
function removeSpacesFromTwoSides(str:string=""):string | null
{
if(str === null) {
return null;
}
str = str.replace(/[\s\t\n\r]+$/g,'');
str = str.replace(/^[\s\t\n\r]+/g,'');
return str ;
}
/**@desc Returning file size in String with lable*/
function fileSizeInString(fileSizeInByte:number=0):string
{
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';
}
}
function removeCurrancyPrint(inputcurencynumber:any):string
{
var lengthCharchters:number = String(inputcurencynumber).length;
inputcurencynumber = String(inputcurencynumber);
for(var i:number = 0;i<lengthCharchters;i++)
{
if(String(inputcurencynumber).charAt(i) === ',')
{
inputcurencynumber = String(inputcurencynumber).replace(",","");
}
}
return inputcurencynumber;
}
function makeRandomNum(str:string, seed:number):number {
var randomSeed:number = 0 ;
for(var i:number=0; i<str.length ; i++)
{
randomSeed += str.charCodeAt(i) ;
}
var max:number;
switch(seed)
{
case 1:
max = 135;
break;
case 2:
max = 456;
break;
case 3:
max = 984;
break;
default:
max = 844
break;
}
randomSeed = (randomSeed%max)/max;
return randomSeed;
}
function isPhoneNumber(str:string):boolean
{
return str.match(/((\+98|[0]{1,2}98)|0)[\d]{8,12}/) !== null;
}
function removeReservedChar(str:string, joinChar:string='_'):string
{
return str.split('?').join(joinChar).split('"').join(joinChar).split('|').join(joinChar).split(':').join(joinChar).split('*').join(joinChar).split('<').join(joinChar).split('>').join(joinChar).split('/').join(joinChar).split('\/').join(joinChar);
}
function randomString(len:number):string
{
var key:string = '' ;
var keyvalues:string = '';
keyvalues += "abcdefghijklmnopqrstuvwxyz";
keyvalues += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
keyvalues += "1234567890";
var keyvaluesLenght:number = keyvalues.length ;
for(var i:number = 0 ; i<len ; i++)
{
key += keyvalues.charAt(Math.floor(Math.random()*keyvaluesLenght));
}
return key;
}
export default StringFunctions ;