-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringMethodExtend.js
More file actions
123 lines (101 loc) · 2.94 KB
/
stringMethodExtend.js
File metadata and controls
123 lines (101 loc) · 2.94 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
/**
* Created by thangnv on 5/20/15.
*/
module.exports = String ;
String.prototype.revert_String = function(){
var temp="";
for( var i = this.length-1 ; i >=0 ; i--){
//console.log(a[i]);
temp+=this.charAt(i);
}
return temp;
};
//khai bao de co the export
String.prototype.isEmail= function(){
var pattern = /^[a-z0-9_-]{3,}@[a-z0-9]{2,}[.][a-z0-9-_]{3,10}$/i;
if (this.match(pattern)){
return true;
}else {
return false;
}
};
//
String.prototype.isPassword = function (){
var pattern = /^[a-z0-9@]{6,16}$/i;
if ( this.match(pattern)){
return true;
}else{
return false;
}
};
String.prototype.camelUpperCase = function () {
"use strict";
var result = "";
var preChar = "";
//console.log("camel function");
for( var i = 0 ; i < this.length ; i++){
if(i == 0 || preChar == " "){
result += this.charAt(i).toUpperCase();
//console.log(this.charAt(i));
}else {
result += this.charAt(i);
}
preChar = this.charAt(i);
//console.log(preChar);
}
return result;
};
//
String.prototype.wordCount = function () {
return this.split(" ").length;
}
module.exports.statisticWord = statisticWord ;
function statisticWord(str) {
var statisticResult = new Array();
var result = str.split(" ");
var listTemp ="";
//filter element in array
for( var i =0 ; i < result.length ; i++ ){
var temp = "";
for( var j =0 ; j < result[i].length ; j++){
if(!result[i].charAt(j).match(/[,.]/)){
temp+=result[i].charAt(j);
}
}
result[i] = temp;
temp = "";
//console.log(temp[i]);
}
//count word appear in string
//console.log(result);
listTemp = result ;
for( var i =0 ; i < result.length ; i++ ){
var count =1 ;
for( var j = 1+i ; j < listTemp.length ; j++){
if( result[i] == listTemp [j] && result[i] != "" ){
listTemp[j] = "" ;
count++;
}
}
//console.log("***** "+result[i]);
if( result[i] != "" && result[i] != null ) {
var wordTemp = {word: result[i], time: count};
statisticResult.push(wordTemp);
}
}
// order array higher ==> Lower
// console.log(statisticResult);
var tempOrder = new Object() ;
for ( var i = 0 ; i < statisticResult.length - 1 ;i++) {
for(var j = i + 1 ; j < statisticResult.length ; j++){
//console.log(statisticResult[i],statisticResult[j]);
if( statisticResult[i].time < statisticResult[j].time){
tempOrder = statisticResult[i] ;
statisticResult[i] = statisticResult[j];
statisticResult[j] = tempOrder ;
//console.log(statisticResult[i],statisticResult[j]);
}
}
}
return statisticResult;
}