-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathstring.js
153 lines (143 loc) · 3.71 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
/*
* String prototype extensions. Doesn't depend on any
* other code. Doens't overwrite existing methods.
*
* Adds trim, camelize, startsWith, endsWith, truncate and stripTags.
*
* Copyright (c) 2006 Jörn Zaefferer
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/
(function() {
/**
* Adds a given method under the given name
* to the String prototype if it doesn't
* currently exist.
*
* @private
*/
function add(name, method) {
if( !String.prototype[name] ) {
String.prototype[name] = method;
}
}
/**
* Returns a string with with leading and trailing whitespace removed.
*
* @example " Hello Boys and Girls! ".trim()
* @result "Hello Boys and Girls!"
*
* @name trim
* @type String
* @cat Plugins/Methods/String
*/
add("trim", function(){
return this.replace(/(^\s+|\s+$)/g, "");
});
/**
* Return a camelized String, removing all underscores and dashes
* and replacing the next character with it's uppercase representation.
*
* @example "font-weight".camelize()
* @result "fontWeight"
*
* @example "border_width_bottom".camelize()
* @result "borderWidthBottom"
*
* @example "border_width-bottom".camelize()
* @result "borderWidthBottom"
*
* @name camelize
* @type String
* @cat Plugins/Methods/String
*/
add("camelize", function() {
return this.replace( /[-_]([a-z])/ig, function(z,b){ return b.toUpperCase();} );
});
/**
* Tests if this string starts with a prefix.
*
* An optional offset specifies where to start searching,
* default is 0 (start of the string).
*
* Returns false if the offset is negative or greater than the length
* of this string.
*
* @example "goldvein".startsWith("go")
* @result true
*
* @example "goldvein".startsWith("god")
* @result false
*
* @example "goldvein".startsWith("ld", 2)
* @result true
*
* @example "goldvein".startsWith("old", 2)
* @result false
*
* @name startsWith
* @type Boolean
* @param prefix The prefix to test
* @param offset (optional) From where to start testing
* @cat Plugins/Methods/String
*/
add("startsWith", function(prefix, offset) {
var offset = offset || 0;
if(offset < 0 || offset > this.length) return false;
return this.substring(offset, offset + prefix.length) == prefix;
});
/**
* Tests if this string ends with the specified suffix.
*
* @example "goldvein".endsWith("ein")
* @result true
*
* @example "goldvein".endsWith("vei")
* @result false
*
* @name endsWith
* @type Boolean
* @param suffix The suffix to test
* @cat Plugins/Methods/String
*/
add("endsWith", function(suffix) {
return this.substring(this.length - suffix.length) == suffix;
});
/**
* Returns a new String that is no longer than a certain length.
*
* @example "thisistenc ".truncate(5);
* @result "th..."
*
* @example "thisistenc ".truncate(5, "x")
* @result "thisx"
*
* @name truncate
* @type String
* @param Number length (optional) The maximum length of the returned string, default is 30
* @param String suffix (optional) The suffix to append to the truncated string, default is "..."
* @cat Plugins/Methods/String
*/
add("truncate", function(length, suffix) {
length = length || 30;
suffix = suffix === undefined ? "..." : suffix;
return this.length > length ?
this.slice(0, length - suffix.length) + suffix : this;
});
/**
* Returns a new String with all tags stripped.
*
* @example "<div id='hi'>Bla</div>".stripTags()
* @result "Bla"
*
* @name stripTags
* @type String
* @cat Plugins/Methods/String
*/
add("stripTags", function() {
return this.replace(/<\/?[^>]+>/gi, '');
});
})();