aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'Code/Blocks/Flipboard/js/core.string.js')
-rw-r--r--Code/Blocks/Flipboard/js/core.string.js240
1 files changed, 240 insertions, 0 deletions
diff --git a/Code/Blocks/Flipboard/js/core.string.js b/Code/Blocks/Flipboard/js/core.string.js
new file mode 100644
index 0000000..312a42e
--- /dev/null
+++ b/Code/Blocks/Flipboard/js/core.string.js
@@ -0,0 +1,240 @@
1/**
2 * @depends nothing
3 * @name core.string
4 * @package jquery-sparkle {@link http://www.balupton/projects/jquery-sparkle}
5 */
6
7/**
8 * Return a new string with any spaces trimmed the left and right of the string
9 * @version 1.0.0
10 * @date June 30, 2010
11 * @package jquery-sparkle {@link http://www.balupton/projects/jquery-sparkle}
12 * @author Benjamin "balupton" Lupton {@link http://www.balupton.com}
13 * @copyright (c) 2009-2010 Benjamin Arthur Lupton {@link http://www.balupton.com}
14 * @license GNU Affero General Public License version 3 {@link http://www.gnu.org/licenses/agpl-3.0.html}
15 */
16String.prototype.trim = String.prototype.trim || function() {
17 // Trim off any whitespace from the front and back
18 return this.replace(/^\s+|\s+$/g, '');
19};
20
21/**
22 * Return a new string with the value stripped from the left and right of the string
23 * @version 1.1.1
24 * @date July 22, 2010
25 * @since 1.0.0, June 30, 2010
26 * @package jquery-sparkle {@link http://www.balupton/projects/jquery-sparkle}
27 * @author Benjamin "balupton" Lupton {@link http://www.balupton.com}
28 * @copyright (c) 2009-2010 Benjamin Arthur Lupton {@link http://www.balupton.com}
29 * @license GNU Affero General Public License version 3 {@link http://www.gnu.org/licenses/agpl-3.0.html}
30 */
31String.prototype.strip = String.prototype.strip || function(value,regex){
32 // Strip a value from left and right, with optional regex support (defaults to false)
33 value = String(value);
34 var str = this;
35 if ( value.length ) {
36 if ( !(regex||false) ) {
37 // We must escape value as we do not want regex support
38 value = value.replace(/([\[\]\(\)\^\$\.\?\|\/\\])/g, '\\$1');
39 }
40 str = str.replace(eval('/^'+value+'+|'+value+'+$/g'), '');
41 }
42 return String(str);
43}
44
45/**
46 * Return a new string with the value stripped from the left of the string
47 * @version 1.1.1
48 * @date July 22, 2010
49 * @package jquery-sparkle {@link http://www.balupton/projects/jquery-sparkle}
50 * @author Benjamin "balupton" Lupton {@link http://www.balupton.com}
51 * @copyright (c) 2009-2010 Benjamin Arthur Lupton {@link http://www.balupton.com}
52 * @license GNU Affero General Public License version 3 {@link http://www.gnu.org/licenses/agpl-3.0.html}
53 */
54String.prototype.stripLeft = String.prototype.stripLeft || function(value,regex){
55 // Strip a value from the left, with optional regex support (defaults to false)
56 value = String(value);
57 var str = this;
58 if ( value.length ) {
59 if ( !(regex||false) ) {
60 // We must escape value as we do not want regex support
61 value = value.replace(/([\[\]\(\)\^\$\.\?\|\/\\])/g, '\\$1');
62 }
63 str = str.replace(eval('/^'+value+'+/g'), '');
64 }
65 return String(str);
66}
67
68/**
69 * Return a new string with the value stripped from the right of the string
70 * @version 1.1.1
71 * @date July 22, 2010
72 * @package jquery-sparkle {@link http://www.balupton/projects/jquery-sparkle}
73 * @author Benjamin "balupton" Lupton {@link http://www.balupton.com}
74 * @copyright (c) 2009-2010 Benjamin Arthur Lupton {@link http://www.balupton.com}
75 * @license GNU Affero General Public License version 3 {@link http://www.gnu.org/licenses/agpl-3.0.html}
76 */
77String.prototype.stripRight = String.prototype.stripRight || function(value,regex){
78 // Strip a value from the right, with optional regex support (defaults to false)
79 value = String(value);
80 var str = this;
81 if ( value.length ) {
82 if ( !(regex||false) ) {
83 // We must escape value as we do not want regex support
84 value = value.replace(/([\[\]\(\)\^\$\.\?\|\/\\])/g, '\\$1');
85 }
86 str = str.replace(eval('/'+value+'+$/g'), '');
87 }
88 return String(str);
89}
90
91/**
92 * Return a int of the string
93 * @version 1.0.0
94 * @date June 30, 2010
95 * @package jquery-sparkle {@link http://www.balupton/projects/jquery-sparkle}
96 * @author Benjamin "balupton" Lupton {@link http://www.balupton.com}
97 * @copyright (c) 2009-2010 Benjamin Arthur Lupton {@link http://www.balupton.com}
98 * @license GNU Affero General Public License version 3 {@link http://www.gnu.org/licenses/agpl-3.0.html}
99 */
100String.prototype.toInt = String.prototype.toInt || function(){
101 // Convert to a Integer
102 return parseInt(this,10);
103};
104
105/**
106 * Return a new string of the old string wrapped with the start and end values
107 * @version 1.0.0
108 * @date June 30, 2010
109 * @package jquery-sparkle {@link http://www.balupton/projects/jquery-sparkle}
110 * @author Benjamin "balupton" Lupton {@link http://www.balupton.com}
111 * @copyright (c) 2009-2010 Benjamin Arthur Lupton {@link http://www.balupton.com}
112 * @license GNU Affero General Public License version 3 {@link http://www.gnu.org/licenses/agpl-3.0.html}
113 */
114String.prototype.wrap = String.prototype.wrap || function(start,end){
115 // Wrap the string
116 return start+this+end;
117};
118
119/**
120 * Return a new string of a selection of the old string wrapped with the start and end values
121 * @version 1.0.0
122 * @date June 30, 2010
123 * @package jquery-sparkle {@link http://www.balupton/projects/jquery-sparkle}
124 * @author Benjamin "balupton" Lupton {@link http://www.balupton.com}
125 * @copyright (c) 2009-2010 Benjamin Arthur Lupton {@link http://www.balupton.com}
126 * @license GNU Affero General Public License version 3 {@link http://www.gnu.org/licenses/agpl-3.0.html}
127 */
128String.prototype.wrapSelection = String.prototype.wrapSelection || function(start,end,a,z){
129 // Wrap the selection
130 if ( typeof a === 'undefined' || a === null ) a = this.length;
131 if ( typeof z === 'undefined' || z === null ) z = this.length;
132 return this.substring(0,a)+start+this.substring(a,z)+end+this.substring(z);
133};
134
135/**
136 * Return a new string of the slug of the old string
137 * @version 1.1.0
138 * @date July 16, 2010
139 * @since 1.0.0, June 30, 2010
140 * @package jquery-sparkle {@link http://www.balupton/projects/jquery-sparkle}
141 * @author Benjamin "balupton" Lupton {@link http://www.balupton.com}
142 * @copyright (c) 2009-2010 Benjamin Arthur Lupton {@link http://www.balupton.com}
143 * @license GNU Affero General Public License version 3 {@link http://www.gnu.org/licenses/agpl-3.0.html}
144 */
145String.prototype.toSlug = String.prototype.toSlug || function(){
146 // Convert a string to a slug
147 return this.toLowerCase().replace(/[\s_]/g, '-').replace(/[^-a-z0-9]/g, '').replace(/--+/g, '-').replace(/^-+|-+$/g,'');
148}
149
150/**
151 * Return a new JSON object of the old string.
152 * Turns:
153 * file.js?a=1&b.c=3.0&b.d=four&a_false_value=false&a_null_value=null
154 * Into:
155 * {"a":1,"b":{"c":3,"d":"four"},"a_false_value":false,"a_null_value":null}
156 * @version 1.1.0
157 * @date July 16, 2010
158 * @since 1.0.0, June 30, 2010
159 * @package jquery-sparkle {@link http://www.balupton/projects/jquery-sparkle}
160 * @author Benjamin "balupton" Lupton {@link http://www.balupton.com}
161 * @copyright (c) 2009-2010 Benjamin Arthur Lupton {@link http://www.balupton.com}
162 * @license GNU Affero General Public License version 3 {@link http://www.gnu.org/licenses/agpl-3.0.html}
163 */
164String.prototype.queryStringToJSON = String.prototype.queryStringToJSON || function ( )
165{ // Turns a params string or url into an array of params
166 // Prepare
167 var params = String(this);
168 // Remove url if need be
169 params = params.substring(params.indexOf('?')+1);
170 // params = params.substring(params.indexOf('#')+1);
171 // Change + to %20, the %20 is fixed up later with the decode
172 params = params.replace(/\+/g, '%20');
173 // Do we have JSON string
174 if ( params.substring(0,1) === '{' && params.substring(params.length-1) === '}' )
175 { // We have a JSON string
176 return eval(decodeURIComponent(params));
177 }
178 // We have a params string
179 params = params.split(/\&(amp\;)?/);
180 var json = {};
181 // We have params
182 for ( var i = 0, n = params.length; i < n; ++i )
183 {
184 // Adjust
185 var param = params[i] || null;
186 if ( param === null ) { continue; }
187 param = param.split('=');
188 if ( param === null ) { continue; }
189 // ^ We now have "var=blah" into ["var","blah"]
190
191 // Get
192 var key = param[0] || null;
193 if ( key === null ) { continue; }
194 if ( typeof param[1] === 'undefined' ) { continue; }
195 var value = param[1];
196 // ^ We now have the parts
197
198 // Fix
199 key = decodeURIComponent(key);
200 value = decodeURIComponent(value);
201 try {
202 // value can be converted
203 value = eval(value);
204 } catch ( e ) {
205 // value is a normal string
206 }
207
208 // Set
209 // window.console.log({'key':key,'value':value}, split);
210 var keys = key.split('.');
211 if ( keys.length === 1 )
212 { // Simple
213 json[key] = value;
214 }
215 else
216 { // Advanced (Recreating an object)
217 var path = '',
218 cmd = '';
219 // Ensure Path Exists
220 $.each(keys,function(ii,key){
221 path += '["'+key.replace(/"/g,'\\"')+'"]';
222 jsonCLOSUREGLOBAL = json; // we have made this a global as closure compiler struggles with evals
223 cmd = 'if ( typeof jsonCLOSUREGLOBAL'+path+' === "undefined" ) jsonCLOSUREGLOBAL'+path+' = {}';
224 eval(cmd);
225 json = jsonCLOSUREGLOBAL;
226 delete jsonCLOSUREGLOBAL;
227 });
228 // Apply Value
229 jsonCLOSUREGLOBAL = json; // we have made this a global as closure compiler struggles with evals
230 valueCLOSUREGLOBAL = value; // we have made this a global as closure compiler struggles with evals
231 cmd = 'jsonCLOSUREGLOBAL'+path+' = valueCLOSUREGLOBAL';
232 eval(cmd);
233 json = jsonCLOSUREGLOBAL;
234 delete jsonCLOSUREGLOBAL;
235 delete valueCLOSUREGLOBAL;
236 }
237 // ^ We now have the parts added to your JSON object
238 }
239 return json;
240}; \ No newline at end of file
Powered by cgit v1.2.3 (git 2.41.0)