forked from documentcloud/underscore-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunderscore.util.strings.js
49 lines (38 loc) · 1.37 KB
/
underscore.util.strings.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
// Underscore-contrib (underscore.util.strings.js 0.0.1)
// (c) 2013 Michael Fogus, DocumentCloud and Investigative Reporters & Editors
// Underscore-contrib may be freely distributed under the MIT license.
(function(root) {
// Baseline setup
// --------------
// Establish the root object, `window` in the browser, or `global` on the server.
var _ = root._ || require('underscore');
// Helpers
// -------
// Mixing in the string utils
// ----------------------------
_.mixin({
// Explodes a string into an array of chars
explode: function(s) {
return s.split('');
},
// Implodes and array of chars into a string
implode: function(a) {
return a.join('');
},
// Converts a string to camel case
camelCase : function( string ){
return string.replace(/-([a-z])/g, function (g) { return g[1].toUpperCase(); });
},
// Converts camel case to dashed (opposite of _.camelCase)
toDash : function( string ){
string = string.replace(/([A-Z])/g, function($1){return "-"+$1.toLowerCase();});
// remove first dash
return ( string.charAt( 0 ) == '-' ) ? string.substr(1) : string;
},
// Reports whether a string contains a search string.
strContains: function(str, search) {
if (typeof str != 'string') throw new TypeError;
return (str.indexOf(search) != -1);
}
});
})(this);