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
|
/**
* @depends nothing
* @name core.string
* @package jquery-sparkle {@link http://www.balupton/projects/jquery-sparkle}
*/
/**
* Return a new string with any spaces trimmed the left and right of the string
* @version 1.0.0
* @date June 30, 2010
* @package jquery-sparkle {@link http://www.balupton/projects/jquery-sparkle}
* @author Benjamin "balupton" Lupton {@link http://www.balupton.com}
* @copyright (c) 2009-2010 Benjamin Arthur Lupton {@link http://www.balupton.com}
* @license GNU Affero General Public License version 3 {@link http://www.gnu.org/licenses/agpl-3.0.html}
*/
String.prototype.trim = String.prototype.trim || function() {
// Trim off any whitespace from the front and back
return this.replace(/^\s+|\s+$/g, '');
};
/**
* Return a new string with the value stripped from the left and right of the string
* @version 1.1.1
* @date July 22, 2010
* @since 1.0.0, June 30, 2010
* @package jquery-sparkle {@link http://www.balupton/projects/jquery-sparkle}
* @author Benjamin "balupton" Lupton {@link http://www.balupton.com}
* @copyright (c) 2009-2010 Benjamin Arthur Lupton {@link http://www.balupton.com}
* @license GNU Affero General Public License version 3 {@link http://www.gnu.org/licenses/agpl-3.0.html}
*/
String.prototype.strip = String.prototype.strip || function(value,regex){
// Strip a value from left and right, with optional regex support (defaults to false)
value = String(value);
var str = this;
if ( value.length ) {
if ( !(regex||false) ) {
// We must escape value as we do not want regex support
value = value.replace(/([\[\]\(\)\^\$\.\?\|\/\\])/g, '\\$1');
}
str = str.replace(eval('/^'+value+'+|'+value+'+$/g'), '');
}
return String(str);
}
/**
* Return a new string with the value stripped from the left of the string
* @version 1.1.1
* @date July 22, 2010
* @package jquery-sparkle {@link http://www.balupton/projects/jquery-sparkle}
* @author Benjamin "balupton" Lupton {@link http://www.balupton.com}
* @copyright (c) 2009-2010 Benjamin Arthur Lupton {@link http://www.balupton.com}
* @license GNU Affero General Public License version 3 {@link http://www.gnu.org/licenses/agpl-3.0.html}
*/
String.prototype.stripLeft = String.prototype.stripLeft || function(value,regex){
// Strip a value from the left, with optional regex support (defaults to false)
value = String(value);
var str = this;
if ( value.length ) {
if ( !(regex||false) ) {
// We must escape value as we do not want regex support
value = value.replace(/([\[\]\(\)\^\$\.\?\|\/\\])/g, '\\$1');
}
str = str.replace(eval('/^'+value+'+/g'), '');
}
return String(str);
}
/**
* Return a new string with the value stripped from the right of the string
* @version 1.1.1
* @date July 22, 2010
* @package jquery-sparkle {@link http://www.balupton/projects/jquery-sparkle}
* @author Benjamin "balupton" Lupton {@link http://www.balupton.com}
* @copyright (c) 2009-2010 Benjamin Arthur Lupton {@link http://www.balupton.com}
* @license GNU Affero General Public License version 3 {@link http://www.gnu.org/licenses/agpl-3.0.html}
*/
String.prototype.stripRight = String.prototype.stripRight || function(value,regex){
// Strip a value from the right, with optional regex support (defaults to false)
value = String(value);
var str = this;
if ( value.length ) {
if ( !(regex||false) ) {
// We must escape value as we do not want regex support
value = value.replace(/([\[\]\(\)\^\$\.\?\|\/\\])/g, '\\$1');
}
str = str.replace(eval('/'+value+'+$/g'), '');
}
return String(str);
}
/**
* Return a int of the string
* @version 1.0.0
* @date June 30, 2010
* @package jquery-sparkle {@link http://www.balupton/projects/jquery-sparkle}
* @author Benjamin "balupton" Lupton {@link http://www.balupton.com}
* @copyright (c) 2009-2010 Benjamin Arthur Lupton {@link http://www.balupton.com}
* @license GNU Affero General Public License version 3 {@link http://www.gnu.org/licenses/agpl-3.0.html}
*/
String.prototype.toInt = String.prototype.toInt || function(){
// Convert to a Integer
return parseInt(this,10);
};
/**
* Return a new string of the old string wrapped with the start and end values
* @version 1.0.0
* @date June 30, 2010
* @package jquery-sparkle {@link http://www.balupton/projects/jquery-sparkle}
* @author Benjamin "balupton" Lupton {@link http://www.balupton.com}
* @copyright (c) 2009-2010 Benjamin Arthur Lupton {@link http://www.balupton.com}
* @license GNU Affero General Public License version 3 {@link http://www.gnu.org/licenses/agpl-3.0.html}
*/
String.prototype.wrap = String.prototype.wrap || function(start,end){
// Wrap the string
return start+this+end;
};
/**
* Return a new string of a selection of the old string wrapped with the start and end values
* @version 1.0.0
* @date June 30, 2010
* @package jquery-sparkle {@link http://www.balupton/projects/jquery-sparkle}
* @author Benjamin "balupton" Lupton {@link http://www.balupton.com}
* @copyright (c) 2009-2010 Benjamin Arthur Lupton {@link http://www.balupton.com}
* @license GNU Affero General Public License version 3 {@link http://www.gnu.org/licenses/agpl-3.0.html}
*/
String.prototype.wrapSelection = String.prototype.wrapSelection || function(start,end,a,z){
// Wrap the selection
if ( typeof a === 'undefined' || a === null ) a = this.length;
if ( typeof z === 'undefined' || z === null ) z = this.length;
return this.substring(0,a)+start+this.substring(a,z)+end+this.substring(z);
};
/**
* Return a new string of the slug of the old string
* @version 1.1.0
* @date July 16, 2010
* @since 1.0.0, June 30, 2010
* @package jquery-sparkle {@link http://www.balupton/projects/jquery-sparkle}
* @author Benjamin "balupton" Lupton {@link http://www.balupton.com}
* @copyright (c) 2009-2010 Benjamin Arthur Lupton {@link http://www.balupton.com}
* @license GNU Affero General Public License version 3 {@link http://www.gnu.org/licenses/agpl-3.0.html}
*/
String.prototype.toSlug = String.prototype.toSlug || function(){
// Convert a string to a slug
return this.toLowerCase().replace(/[\s_]/g, '-').replace(/[^-a-z0-9]/g, '').replace(/--+/g, '-').replace(/^-+|-+$/g,'');
}
/**
* Return a new JSON object of the old string.
* Turns:
* file.js?a=1&b.c=3.0&b.d=four&a_false_value=false&a_null_value=null
* Into:
* {"a":1,"b":{"c":3,"d":"four"},"a_false_value":false,"a_null_value":null}
* @version 1.1.0
* @date July 16, 2010
* @since 1.0.0, June 30, 2010
* @package jquery-sparkle {@link http://www.balupton/projects/jquery-sparkle}
* @author Benjamin "balupton" Lupton {@link http://www.balupton.com}
* @copyright (c) 2009-2010 Benjamin Arthur Lupton {@link http://www.balupton.com}
* @license GNU Affero General Public License version 3 {@link http://www.gnu.org/licenses/agpl-3.0.html}
*/
String.prototype.queryStringToJSON = String.prototype.queryStringToJSON || function ( )
{ // Turns a params string or url into an array of params
// Prepare
var params = String(this);
// Remove url if need be
params = params.substring(params.indexOf('?')+1);
// params = params.substring(params.indexOf('#')+1);
// Change + to %20, the %20 is fixed up later with the decode
params = params.replace(/\+/g, '%20');
// Do we have JSON string
if ( params.substring(0,1) === '{' && params.substring(params.length-1) === '}' )
{ // We have a JSON string
return eval(decodeURIComponent(params));
}
// We have a params string
params = params.split(/\&(amp\;)?/);
var json = {};
// We have params
for ( var i = 0, n = params.length; i < n; ++i )
{
// Adjust
var param = params[i] || null;
if ( param === null ) { continue; }
param = param.split('=');
if ( param === null ) { continue; }
// ^ We now have "var=blah" into ["var","blah"]
// Get
var key = param[0] || null;
if ( key === null ) { continue; }
if ( typeof param[1] === 'undefined' ) { continue; }
var value = param[1];
// ^ We now have the parts
// Fix
key = decodeURIComponent(key);
value = decodeURIComponent(value);
try {
// value can be converted
value = eval(value);
} catch ( e ) {
// value is a normal string
}
// Set
// window.console.log({'key':key,'value':value}, split);
var keys = key.split('.');
if ( keys.length === 1 )
{ // Simple
json[key] = value;
}
else
{ // Advanced (Recreating an object)
var path = '',
cmd = '';
// Ensure Path Exists
$.each(keys,function(ii,key){
path += '["'+key.replace(/"/g,'\\"')+'"]';
jsonCLOSUREGLOBAL = json; // we have made this a global as closure compiler struggles with evals
cmd = 'if ( typeof jsonCLOSUREGLOBAL'+path+' === "undefined" ) jsonCLOSUREGLOBAL'+path+' = {}';
eval(cmd);
json = jsonCLOSUREGLOBAL;
delete jsonCLOSUREGLOBAL;
});
// Apply Value
jsonCLOSUREGLOBAL = json; // we have made this a global as closure compiler struggles with evals
valueCLOSUREGLOBAL = value; // we have made this a global as closure compiler struggles with evals
cmd = 'jsonCLOSUREGLOBAL'+path+' = valueCLOSUREGLOBAL';
eval(cmd);
json = jsonCLOSUREGLOBAL;
delete jsonCLOSUREGLOBAL;
delete valueCLOSUREGLOBAL;
}
// ^ We now have the parts added to your JSON object
}
return json;
};
|