/
string.js
382 lines (341 loc) · 16.4 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
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
// ==========================================================================
// Project: SproutCore - JavaScript Application Framework
// Copyright: ©2006-2011 Strobe Inc. and contributors.
// Portions ©2008-2011 Apple Inc. All rights reserved.
// License: Licensed under MIT license (see license.js)
// ==========================================================================
// These are basic enhancements to the string class used throughout
// SproutCore.
/** @private */
SC.STRING_TITLEIZE_REGEXP = (/([\s|\-|\_|\n])([^\s|\-|\_|\n]?)/g);
SC.STRING_HUMANIZE_REGEXP = (/[\-_]/g);
SC.STRING_REGEXP_ESCAPED_REGEXP = (/([\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:])/g);
/** @private
Since there are many strings that are commonly dasherized(), we'll maintain
// a cache. Moreover, we'll pre-add some common ones.
*/
SC.STRING_DASHERIZE_CACHE = {
top: 'top',
left: 'left',
right: 'right',
bottom: 'bottom',
width: 'width',
height: 'height',
minWidth: 'min-width',
maxWidth: 'max-width'
};
/** @private
Active Support style inflection constants
*/
SC.INFLECTION_CONSTANTS = {
/** @private */
PLURAL: [
[/(quiz)$/i, "$1zes" ],
[/^(ox)$/i, "$1en" ],
[/([m|l])ouse$/i, "$1ice" ],
[/(matr|vert|ind)ix|ex$/i, "$1ices" ],
[/(x|ch|ss|sh)$/i, "$1es" ],
[/([^aeiouy]|qu)y$/i, "$1ies" ],
[/(hive)$/i, "$1s" ],
[/(?:([^f])fe|([lr])f)$/i, "$1$2ves"],
[/sis$/i, "ses" ],
[/([ti])um$/i, "$1a" ],
[/(buffal|tomat)o$/i, "$1oes" ],
[/(bu)s$/i, "$1ses" ],
[/(alias|status)$/i, "$1es" ],
[/(octop|vir)us$/i, "$1i" ],
[/(ax|test)is$/i, "$1es" ],
[/s$/i, "s" ],
[/$/, "s" ]
],
/** @private */
SINGULAR: [
[/(quiz)zes$/i, "$1" ],
[/(matr)ices$/i, "$1ix" ],
[/(vert|ind)ices$/i, "$1ex" ],
[/^(ox)en/i, "$1" ],
[/(alias|status)es$/i, "$1" ],
[/(octop|vir)i$/i, "$1us" ],
[/(cris|ax|test)es$/i, "$1is" ],
[/(shoe)s$/i, "$1" ],
[/(o)es$/i, "$1" ],
[/(bus)es$/i, "$1" ],
[/([m|l])ice$/i, "$1ouse" ],
[/(x|ch|ss|sh)es$/i, "$1" ],
[/(m)ovies$/i, "$1ovie" ],
[/(s)eries$/i, "$1eries"],
[/([^aeiouy]|qu)ies$/i, "$1y" ],
[/([lr])ves$/i, "$1f" ],
[/(tive)s$/i, "$1" ],
[/(hive)s$/i, "$1" ],
[/([^f])ves$/i, "$1fe" ],
[/(^analy)ses$/i, "$1sis" ],
[/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i, "$1$2sis"],
[/([ti])a$/i, "$1um" ],
[/(n)ews$/i, "$1ews" ],
[/s$/i, "" ]
],
/** @private */
IRREGULAR: [
['move', 'moves' ],
['sex', 'sexes' ],
['child', 'children'],
['man', 'men' ],
['person', 'people' ]
],
/** @private */
UNCOUNTABLE: [
"sheep",
"fish",
"series",
"species",
"money",
"rice",
"information",
"info",
"equipment"
]
};
/**
@namespace
@lends SC.String
*/
SC.StringInflections = {
/**
Capitalizes every word in a string. Unlike titleize, spaces or dashes
will remain in-tact.
## Examples
- **Input String** -> **Output String**
- my favorite items -> My Favorite Items
- css-class-name -> Css-Class-Name
- action_name -> Action_Name
- innerHTML -> InnerHTML
@returns {String} capitalized string
*/
capitalizeEach: function() {
return this.replace(SC.STRING_TITLEIZE_REGEXP,
function(str,sep,character) {
return (character) ? (sep + character.toUpperCase()) : sep;
}).capitalize() ;
},
/**
Converts a string to a title. This will decamelize the string, convert
separators to spaces and capitalize every word.
## Examples
- **Input String** -> **Output String**
- my favorite items -> My Favorite Items
- css-class-name -> Css Class Name
- action_name -> Action Name
- innerHTML -> Inner HTML
@return {String} titleized string.
*/
titleize: function() {
var ret = this.replace(SC.STRING_DECAMELIZE_REGEXP,'$1_$2'); // decamelize
return ret.replace(SC.STRING_TITLEIZE_REGEXP,
function(str,separater,character) {
return (character) ? (' ' + character.toUpperCase()) : ' ';
}).capitalize() ;
},
/**
Converts the string into a class name. This method will camelize your
string and then capitalize the first letter.
## Examples
- **Input String** -> **Output String**
- my favorite items -> MyFavoriteItems
- css-class-name -> CssClassName
- action_name -> ActionName
- innerHTML -> InnerHtml
@returns {String}
*/
classify: function() {
var ret = this.replace(SC.STRING_TITLEIZE_REGEXP,
function(str,separater,character) {
return (character) ? character.toUpperCase() : '' ;
}) ;
var first = ret.charAt(0), upper = first.toUpperCase() ;
return (first !== upper) ? (upper + ret.slice(1)) : ret ;
},
/**
Converts a camelized string or a string with dashes or underscores into
a string with components separated by spaces.
## Examples
- **Input String** -> **Output String**
- my favorite items -> my favorite items
- css-class-name -> css class name
- action_name -> action name
- innerHTML -> inner html
@returns {String} the humanized string.
*/
humanize: function() {
return this.decamelize().replace(SC.STRING_HUMANIZE_REGEXP,' ') ;
},
/**
Will escape a string so it can be securely used in a regular expression.
Useful when you need to use user input in a regular expression without
having to worry about it breaking code if any reserved regular expression
characters are used.
@returns {String} the string properly escaped for use in a regexp.
*/
escapeForRegExp: function() {
return this.replace(SC.STRING_REGEXP_ESCAPED_REGEXP, "\\$1");
},
/**
Removes any standard diacritic characters from the string. So, for
example, all instances of 'Á' will become 'A'.
@returns {String} the modified string
*/
removeDiacritics: function() {
// Lazily create the SC.diacriticMappingTable object.
var diacriticMappingTable = SC.diacriticMappingTable;
if (!diacriticMappingTable) {
SC.diacriticMappingTable = {
'À':'A', 'Á':'A', 'Â':'A', 'Ã':'A', 'Ä':'A', 'Å':'A', 'Ā':'A', 'Ă':'A',
'Ą':'A', 'Ǎ':'A', 'Ǟ':'A', 'Ǡ':'A', 'Ǻ':'A', 'Ȁ':'A', 'Ȃ':'A', 'Ȧ':'A',
'Ḁ':'A', 'Ạ':'A', 'Ả':'A', 'Ấ':'A', 'Ầ':'A', 'Ẩ':'A', 'Ẫ':'A', 'Ậ':'A',
'Ắ':'A', 'Ằ':'A', 'Ẳ':'A', 'Ẵ':'A', 'Ặ':'A', 'Å':'A', 'Ḃ':'B', 'Ḅ':'B',
'Ḇ':'B', 'Ç':'C', 'Ć':'C', 'Ĉ':'C', 'Ċ':'C', 'Č':'C', 'Ḉ':'C', 'Ď':'D',
'Ḋ':'D', 'Ḍ':'D', 'Ḏ':'D', 'Ḑ':'D', 'Ḓ':'D', 'È':'E', 'É':'E', 'Ê':'E',
'Ë':'E', 'Ē':'E', 'Ĕ':'E', 'Ė':'E', 'Ę':'E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':'a',
'ą':'a', 'ǎ':'a', 'ǟ':'a', 'ǡ':'a', 'ǻ':'a', 'ȁ':'a', 'ȃ':'a', 'ȧ':'a',
'ḁ':'a', 'ạ':'a', 'ả':'a', 'ấ':'a', 'ầ':'a', 'ẩ':'a', 'ẫ':'a', 'ậ':'a',
'ắ':'a', 'ằ':'a', 'ẳ':'a', 'ẵ':'a', 'ặ':'a', 'ḃ':'b', 'ḅ':'b', 'ḇ':'b',
'ç':'c', 'ć':'c', 'ĉ':'c', 'ċ':'c', 'č':'c', 'ḉ':'c', 'ď':'d', 'ḋ':'d',
'ḍ':'d', 'ḏ':'d', 'ḑ':'d', 'ḓ':'d', 'è':'e', 'é':'e', 'ê':'e', 'ë':'e',
'ē':'e', 'ĕ':'e', 'ė':'e', 'ę':'e', 'ě':'e', 'ȅ':'e', 'ȇ':'e', 'ȩ':'e',
'ḕ':'e', 'ḗ':'e', 'ḙ':'e', 'ḛ':'e', 'ḝ':'e', 'ẹ':'e', 'ẻ':'e', 'ẽ':'e',
'ế':'e', 'ề':'e', 'ể':'e', 'ễ':'e', 'ệ':'e', 'ḟ':'f', 'ĝ':'g', 'ğ':'g',
'ġ':'g', 'ģ':'g', 'ǧ':'g', 'ǵ':'g', 'ḡ':'g', 'ĥ':'h', 'ȟ':'h', 'ḣ':'h',
'ḥ':'h', 'ḧ':'h', 'ḩ':'h', 'ḫ':'h', 'ẖ':'h', 'ì':'i', 'í':'i', 'î':'i',
'ï':'i', 'ĩ':'i', 'ī':'i', 'ĭ':'i', 'į':'i', 'ǐ':'i', 'ȉ':'i', 'ȋ':'i',
'ḭ':'i', 'ḯ':'i', 'ỉ':'i', 'ị':'i', 'ĵ':'j', 'ǰ':'j', 'ķ':'k', 'ǩ':'k',
'ḱ':'k', 'ḳ':'k', 'ḵ':'k', 'ĺ':'l', 'ļ':'l', 'ľ':'l', 'ḷ':'l', 'ḹ':'l',
'ḻ':'l', 'ḽ':'l', 'ḿ':'m', 'ṁ':'m', 'ṃ':'m', 'ñ':'n', 'ń':'n', 'ņ':'n',
'ň':'n', 'ǹ':'n', 'ṅ':'n', 'ṇ':'n', 'ṉ':'n', 'ṋ':'n', 'ò':'o', 'ó':'o',
'ô':'o', 'õ':'o', 'ö':'o', 'ō':'o', 'ŏ':'o', 'ő':'o', 'ơ':'o', 'ǒ':'o',
'ǫ':'o', 'ǭ':'o', 'ȍ':'o', 'ȏ':'o', 'ȫ':'o', 'ȭ':'o', 'ȯ':'o', 'ȱ':'o',
'ṍ':'o', 'ṏ':'o', 'ṑ':'o', 'ṓ':'o', 'ọ':'o', 'ỏ':'o', 'ố':'o', 'ồ':'o',
'ổ':'o', 'ỗ':'o', 'ộ':'o', 'ớ':'o', 'ờ':'o', 'ở':'o', 'ỡ':'o', 'ợ':'o',
'ṕ':'p', 'ṗ':'p', 'ŕ':'r', 'ŗ':'r', 'ř':'r', 'ȑ':'r', 'ȓ':'r', 'ṙ':'r',
'ṛ':'r', 'ṝ':'r', 'ṟ':'r', 'ś':'s', 'ŝ':'s', 'ş':'s', 'š':'s', 'ș':'s',
'ṡ':'s', 'ṣ':'s', 'ṥ':'s', 'ṧ':'s', 'ṩ':'s', 'ţ':'t', 'ť':'t', 'ț':'t',
'ṫ':'t', 'ṭ':'t', 'ṯ':'t', 'ṱ':'t', 'ẗ':'t', 'ù':'u', 'ú':'u', 'û':'u',
'ü':'u', 'ũ':'u', 'ū':'u', 'ŭ':'u', 'ů':'u', 'ű':'u', 'ų':'u', 'ư':'u',
'ǔ':'u', 'ǖ':'u', 'ǘ':'u', 'ǚ':'u', 'ǜ':'u', 'ȕ':'u', 'ȗ':'u', 'ṳ':'u',
'ṵ':'u', 'ṷ':'u', 'ṹ':'u', 'ṻ':'u', 'ụ':'u', 'ủ':'u', 'ứ':'u', 'ừ':'u',
'ử':'u', 'ữ':'u', 'ự':'u', 'ṽ':'v', 'ṿ':'v', 'ŵ':'w', 'ẁ':'w', 'ẃ':'w',
'ẅ':'w', 'ẇ':'w', 'ẉ':'w', 'ẘ':'w', 'ẋ':'x', 'ẍ':'x', 'ý':'y', 'ÿ':'y',
'ŷ':'y', 'ȳ':'y', 'ẏ':'y', 'ẙ':'y', 'ỳ':'y', 'ỵ':'y', 'ỷ':'y', 'ỹ':'y',
'ź':'z', 'ż':'z', 'ž':'z', 'ẑ':'z', 'ẓ':'z', 'ẕ':'z'
};
diacriticMappingTable = SC.diacriticMappingTable;
}
var original, replacement, ret = "",
length = this.length;
for (var i = 0; i <= length; ++i) {
original = this.charAt(i);
replacement = diacriticMappingTable[original];
if (replacement) {
ret += replacement;
}
else {
ret += original;
}
}
return ret;
},
/**
Converts a word into its plural form.
@returns {String} the plural form of the string
*/
pluralize: function() {
var idx, len,
compare = this.split(/\s/).pop(), //check only the last word of a string
restOfString = this.replace(compare,''),
isCapitalized = compare.charAt(0).match(/[A-Z]/) ? true : false;
compare = compare.toLowerCase();
for (idx=0, len=SC.INFLECTION_CONSTANTS.UNCOUNTABLE.length; idx < len; idx++) {
var uncountable = SC.INFLECTION_CONSTANTS.UNCOUNTABLE[idx];
if (compare == uncountable) {
return this.toString();
}
}
for (idx=0, len=SC.INFLECTION_CONSTANTS.IRREGULAR.length; idx < len; idx++) {
var singular = SC.INFLECTION_CONSTANTS.IRREGULAR[idx][0],
plural = SC.INFLECTION_CONSTANTS.IRREGULAR[idx][1];
if ((compare == singular) || (compare == plural)) {
if(isCapitalized) plural = plural.capitalize();
return restOfString + plural;
}
}
for (idx=0, len=SC.INFLECTION_CONSTANTS.PLURAL.length; idx < len; idx++) {
var regex = SC.INFLECTION_CONSTANTS.PLURAL[idx][0],
replace_string = SC.INFLECTION_CONSTANTS.PLURAL[idx][1];
if (regex.test(compare)) {
return this.replace(regex, replace_string);
}
}
},
/**
Converts a word into its singular form.
@returns {String} the singular form of the string
*/
singularize: function() {
var idx, len,
compare = this.split(/\s/).pop(), //check only the last word of a string
restOfString = this.replace(compare,''),
isCapitalized = compare.charAt(0).match(/[A-Z]/) ? true : false;
compare = compare.toLowerCase();
for (idx=0, len=SC.INFLECTION_CONSTANTS.UNCOUNTABLE.length; idx < len; idx++) {
var uncountable = SC.INFLECTION_CONSTANTS.UNCOUNTABLE[idx];
if (compare == uncountable) {
return this.toString();
}
}
for (idx=0, len=SC.INFLECTION_CONSTANTS.IRREGULAR.length; idx < len; idx++) {
var singular = SC.INFLECTION_CONSTANTS.IRREGULAR[idx][0],
plural = SC.INFLECTION_CONSTANTS.IRREGULAR[idx][1];
if ((compare == singular) || (compare == plural)) {
if(isCapitalized) singular = singular.capitalize();
return restOfString + singular;
}
}
for (idx=0, len=SC.INFLECTION_CONSTANTS.SINGULAR.length; idx < len; idx++) {
var regex = SC.INFLECTION_CONSTANTS.SINGULAR[idx][0],
replace_string = SC.INFLECTION_CONSTANTS.SINGULAR[idx][1];
if (regex.test(compare)) {
return this.replace(regex, replace_string);
}
}
}
};
/** @private */
SC.String.strip = SC.String.trim; // convenience alias.
SC.supplement(SC.String, SC.StringInflections);
// Apply SC.String mixin to built-in String object
SC.supplement(String.prototype, SC.StringInflections) ;