-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
177 lines (161 loc) · 5 KB
/
server.js
File metadata and controls
177 lines (161 loc) · 5 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
var util = require('./utils')
module.exports = {
/**
* Converts string to title case.
* First letter of every word is capital rest all are lower case
* @param str Input string
* @returns String in title case
*/
titleCase: function (str) {
str = str.toLowerCase();
var string = str.split(' ');
for (var i = 0; i < string.length; i++) {
string[i] = string[i].charAt(0).toUpperCase() + string[i].slice(1);
}
return string.join(' ');
},
/**
* Converts string to camel case
* coding-is-fun to codingIsFun
* coding is fun to codingIsFun
* coding$% is%& fun to codingIsFun
* @param str Input string
* @returns String in camel case
*/
toCamelCase: function (str) {
let words = util.toWords(str);
return util.toCamelCaseUtil(words);
},
/**
* Removes special characters from string
* @param str Input string
* @param removeSpace Optional fields, true if space needs to be removed
* @returns String with no special characters
*/
removeSpecialCharacters: function (str, removeSpace = false) {
return removeSpace ? str.replace(/[^a-zA-Z]/g, "") : str.replace(/[^a-zA-Z ]/g, "");
},
/**
* Reverses the input string
* @param str Input string
* @returns Reversed string
*/
reverse: function (str) {
let newString = "";
for (let i = str.length - 1; i >= 0; i--) {
newString += str[i];
}
return newString;
},
/**
* Return true if the string is made up of whitespace or is null/undefined
* @param str Input string
* @returns Boolean true or false
*/
isEmpty: function (str) {
return str === null || str === undefined || str.match(/^ *$/) !== null;
},
/**
* Return true if the string is made up of all capital letters
* @param str Input string
* @returns Boolean true or false
*/
isUpperCase: function (str) {
return str === str.toUpperCase();
},
/**
* Return true if the string is made up of all lower letters
* @param str Input string
* @returns Boolean true or false
*/
isLowerCase: function (str) {
return str === str.toLowerCase();
},
/**
* Return true if the string is made up of all numbers
* isNaN(123) // false
* isNaN('123') // false
* isNaN('1e10000') // false (This translates to Infinity, which is a number)
* isNaN('foo') // true
* isNaN('10px') // true
* @param str Input string
* @returns Boolean true or false
*/
isNumeric: function (str) {
return !isNaN(str)
},
/**
* Removes extra spaced from string
* str.removeExtraSpaces(" this contains spaces ")) // this contains spaces
* @param str Input string
* @returns String with correct spaces
*/
removeExtraSpaces: function (str) {
return str.replace(/\s+/g, ' ').trim();
},
/**
* Check if string contains a substring
* str.contains("coding is fun", fun) // true
* @param str Input string
* @param substr Input substring
* @returns Boolean true or false
*/
contains: function (str, substr) {
return str.includes(substr);
},
/**
* Returns count of sub string in string
* str.count('abcabc', 'abc') // 2
* str.count("this is ice", "is") // 2
* @param str Input string
* @param substr Input substring
* @returns Number of occurrences of substr in str
*/
count: function (str, substr) {
count = 0,
pos = str.indexOf(substr);
while (pos > -1) {
++count;
pos = str.indexOf(substr, ++pos);
}
return count;
},
/**
* Converts string to dashed string
* str.dashCase("fooBar") // foo-bar
* str.dashCase("FooBar") // -foo-bar
* @param str Input string
* @returns dashed case string
*/
dashCase: function (str) {
return str.replace(/[A-Z]/g, m => "-" + m.toLowerCase());
},
/**
* Removed all HTML tags from a string
* str.removeHTMLTags("<html> <body> Javascript<body> is not Java") // Javascript is not Java
* @param str Input string
* @returns HTML tag free string
*/
removeHTMLTags: function (str) {
return str.replace(/(<([^>]+)>)/ig, '');
},
/**
* Convert the input string to underscore case
* BatteryAAA to battery_aaa
* coldWind to cold_wind
* @param str Input string
* @returns underscore case string
*/
underscoreCase: function (str) {
return (str.replace(/\.?([A-Z]+)/g, function (x, y) { return "_" + y.toLowerCase() }).replace(/^_/, ""));
},
/**
* Check if input string ends with given substring
* @param {*} str Input string
* @param {*} substr Input substring
* @returns boolean true or false
*/
endsWith: function (str, substr) {
return str.endsWith(substr);
}
}