This repository has been archived by the owner on Jan 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
string.js
executable file
·281 lines (250 loc) · 6.8 KB
/
string.js
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
/**
* String prototype extensions.
*
* @version 0.9
* @copyright Copyright (c) 2007-2012 Harald Hanek
* @license MIT (http://harrydeluxe.mit-license.org)
*/
(function(){
/**
* Extend the string prototype with the method under the given name if it doesn't currently exist.
*
* @private
*/
function append(name, method, force)
{
if(!String.prototype[name] || force)
String.prototype[name] = method;
}
/**
* Returns a String with with leading and trailing whitespace removed.
* ATTENTION: This method overwrite the existing method 'trim'
*
* @example " My name is Harry! ".trim();
* @result "My name is Harry!"
*
* @name trim
* @param String ch Optionally, the stripped characters can also be specified using the ch parameter.
* @return String
*/
append("trim", function(ch){
ch = !ch ? ' \\s\u00A0' : (ch + '').replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '$1');
return this.replace(new RegExp('^[' + ch + ']+|[' + ch + ']+$', 'g'), '');
}, true);
/**
* Removes leading whitespace.
*
* @example " My name is Harry!".ltrim();
* @result "My name is Harry!"
*
* @name ltrim
* @param String ch Optionally, the stripped characters can also be specified using the ch parameter.
* @return String
*/
append("ltrim", function(ch){
ch = !ch ? ' \\s\u00A0' : (ch + '').replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '$1');
return this.replace(new RegExp('^[' + ch + ']+', 'g'), '');
});
/**
* Removes trailing whitespace.
*
* @example "My name is Harry! ".rtrim();
* @result "My name is Harry!"
*
* @name rtrim
* @param String ch Optionally, the stripped characters can also be specified using the ch parameter.
* @return String
*/
append("rtrim", function(ch){
ch = !ch ? ' \\s\u00A0' : (ch + '').replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\\$1');
return this.replace(new RegExp('[' + ch + ']+$', 'g'), '');
});
/**
* Entfernt Whitespace am Anfang und Ende des Strings und reduziert Whitespace innerhalb des Strings auf ein Zeichen.
*
* @example " My name is Harry! ".collapseSpaces();
* @result "My name is Harry!"
*
* @name collapseSpaces
* @return String
*/
append("collapseSpaces", function(){
return this.trim().replace(/\s{2,}/g, " ").replace(/{(Keyword):\s*(.*?)\s*}/gi, "{$1:$2}");
});
/**
* Substitute substrings from an array into a format String that includes '%1'-type specifiers
*
* @example "My %0 is %1".format(['name','Harry']);
* @result "My name is Harry"
*
* @name format
* @param Array<String> substrings
* @return String
*/
append("format", function(substrings){
var subRegExp = /(?:%(\d+))/mg, currPos = 0, r = [];
do
{
var match = subRegExp.exec(this);
if(match && match[1])
{
if(match.index > currPos)
r.push(this.substring(currPos,match.index));
r.push(substrings[parseInt(match[1])]);
currPos = subRegExp.lastIndex;
}
}
while(match);
if(currPos < this.length)
r.push(this.substring(currPos,this.length));
return r.join("");
});
/**
* Removes a specified length of characters from a given postion.
*
* @example "JavaScript".remove(4, 6);
* @result "Java"
*
* @name remove
* @param Int start start postion
* @param Int length length
* @return String
*/
append("remove", function(start, length){
var s = '' ;
if(start > 0)
s = this.substring(0, start);
if(start + length < this.length)
s += this.substring(start + length , this.length);
return s ;
});
/**
* Reverses the characters in the specified string.
*
* @example "Hello".reverse();
* @result "olleH"
*
* @name reverse
* @return String
*/
append("reverse", function(){
if(!this)
return '';
var a = (this+'').split('');
a.reverse();
return a.join('');
});
/**
* Returns 'str' repeated 'count' times, optionally placing 'separator' between each repetition
*
* @example "Hello".repeat(5);
* @result "HelloHelloHelloHelloHello"
*
*
* @example "Hello".repeat(5,'#');
* @result "Hello#Hello#Hello#Hello#Hello"
*
* @name repeat
* @param Int count Number of repetitions
* @param String separator Separator
* @return String
*/
append("repeat", function(count, separator){
var t = this, s = "";
while(--count+1 > 0)
s += (separator && count != 0) ? t+separator : t;
return s;
});
/**
* Pad a string up to a given length. Padding characters are added to the left of the string.
*
*
* @example "22".pad(4, '#');
* @result "##22"
*
*
* @example "22".pad(4, '#', 1);
* @result "22##"
*
* @name pad
* @param Int length The final length of the string. (optional) default is (30)
* @param String ch Character used to fill up the string. (optional) default is (' ')
* @param Int direction Direction to fill (start, end) 0|1 . (optional) default is start (0)
* @return String
*/
append("pad", function(length, ch, direction){
length = length || 30;
direction = direction || 0;
ch = ch || ' ';
var t = this;
while(t.length < length)
t = (direction == 1) ? t+=ch : ch+t;
return t;
});
/**
* Capitalizes the first letter of a string and downcases all the others.
*
* @example "my name is harry".capitalize();
* @result "My Name Is Harry"
*
* @name capitalize
* @return String
*/
append("capitalize", function(){
var w = this.split(' ');
for(var i = 0; i < w.length; i++)
//w[i] = w[i].charAt(0).toUpperCase() + w[i].substring(1);
w[i] = w[i].charAt(0).toUpperCase() + w[i].substring(1).toLowerCase();
return w.join(" ");
});
/**
* Converts a string separated by dashes and/or underscores into a camelCase equivalent.
* For instance, 'java-script' would be converted to 'javaScript'.
*
* @example "java-script".camelize();
* @result "javaScript"
*
* @example "java_script_methods".camelize();
* @result "javaScriptMethods"
*
* @example "java_script-methods".camelize();
* @result "javaScriptMethods"
*
* @name camelize
* @return String
*/
append("camelize", function(){
return this.replace( /[-_]([a-z])/ig, function(z,b){ return b.toUpperCase()} );
});
/**
* Returns a string that is no longer than a certain length.
*
* @example "JavaScript ".truncate(5);
* @result "Ja..."
*
* @example "JavaScript ".truncate(5, "#");
* @result "Java#"
*
* @name truncate
* @param Number length (optional) The maximum length of the returned String, default is 50
* @param String suffix (optional) The suffix to append to the truncated String, default is "..."
* @return String
*/
append("truncate", function(length, suffix){
length = length || 50;
suffix = suffix === undefined ? "..." : suffix;
return this.length > length ? this.slice(0, length - suffix.length) + suffix : this;
});
/**
* Returns a string with all HTML tags stripped.
*
* @example "<div id='type'>JavaScript</div>".stripTags();
* @result "JavaScript"
*
* @name stripTags
* @return String
*/
append("stripTags", function(){
return this.replace(/<\/?[^>]+>/gi, '');
});
})();