PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/qx/lang/String.js

https://github.com/fjakobs/qxoo
JavaScript | 329 lines | 98 code | 41 blank | 190 comment | 5 complexity | ab9e56ee0a85d82d660b80718a969776 MD5 | raw file
  1. var window = require('qxglobals').global;
  2. require('qx/Bootstrap');
  3. require('qx/Class');
  4. /* ************************************************************************
  5. qooxdoo - the new era of web development
  6. http://qooxdoo.org
  7. Copyright:
  8. 2004-2008 1&1 Internet AG, Germany, http://www.1und1.de
  9. License:
  10. LGPL: http://www.gnu.org/licenses/lgpl.html
  11. EPL: http://www.eclipse.org/org/documents/epl-v10.php
  12. See the LICENSE file in the project's top-level directory for details.
  13. Authors:
  14. * Sebastian Werner (wpbasti)
  15. * Andreas Ecker (ecker)
  16. ======================================================================
  17. This class contains code based on the following work:
  18. * Mootools
  19. http://mootools.net/
  20. Version 1.1.1
  21. Copyright:
  22. (c) 2007 Valerio Proietti
  23. License:
  24. MIT: http://www.opensource.org/licenses/mit-license.php
  25. ************************************************************************ */
  26. /**
  27. * String helper functions
  28. *
  29. * The native JavaScript String is not modified by this class. However,
  30. * there are modifications to the native String in {@link qx.lang.Core} for
  31. * browsers that do not support certain features.
  32. *
  33. * The string/array generics introduced in JavaScript 1.6 are supported by
  34. * {@link qx.lang.Generics}.
  35. */
  36. qx.Class.define("qx.lang.String",
  37. {
  38. statics :
  39. {
  40. /**
  41. * Converts a hyphenated string (separated by '-') to camel case.
  42. *
  43. * Example:
  44. * <pre class='javascript'>qx.lang.String.camelCase("I-like-cookies"); //returns "ILikeCookies"</pre>
  45. *
  46. * @param str {String} hyphenated string
  47. * @return {String} camelcase string
  48. */
  49. camelCase : function(str)
  50. {
  51. return str.replace(/\-([a-z])/g, function(match, chr) {
  52. return chr.toUpperCase();
  53. });
  54. },
  55. /**
  56. * Converts a camelcased string to a hyphenated (separated by '-') string.
  57. *
  58. * Example:
  59. * <pre class='javascript'>qx.lang.String.camelCase("ILikeCookies"); //returns "I-like-cookies"</pre>
  60. *
  61. * @param str {String} camelcased string
  62. * @return {String} hyphenated string
  63. */
  64. hyphenate: function(str)
  65. {
  66. return str.replace(/[A-Z]/g, function(match){
  67. return ('-' + match.charAt(0).toLowerCase());
  68. });
  69. },
  70. /**
  71. * Converts a string to camel case.
  72. *
  73. * Example:
  74. * <pre class='javascript'>qx.lang.String.camelCase("i like cookies"); //returns "I Like Cookies"</pre>
  75. *
  76. * @param str {String} any string
  77. * @return {String} capitalized string
  78. */
  79. capitalize: function(str){
  80. return str.replace(/\b[a-z]/g, function(match) {
  81. return match.toUpperCase();
  82. });
  83. },
  84. /**
  85. * Removes all extraneous whitespace from a string and trims it
  86. *
  87. * Example:
  88. *
  89. * <code>
  90. * qx.lang.String.clean(" i like cookies \n\n");
  91. * </code>
  92. *
  93. * Returns "i like cookies"
  94. *
  95. * @param str {String} the string to clean up
  96. * @return {String} Cleaned up string
  97. */
  98. clean: function(str){
  99. return this.trim(str.replace(/\s+/g, ' '));
  100. },
  101. /**
  102. * removes white space from the left side of a string
  103. *
  104. * @param str {String} the string to trim
  105. * @return {String} the trimmed string
  106. */
  107. trimLeft : function(str) {
  108. return str.replace(/^\s+/, "");
  109. },
  110. /**
  111. * removes white space from the right side of a string
  112. *
  113. * @param str {String} the string to trim
  114. * @return {String} the trimmed string
  115. */
  116. trimRight : function(str) {
  117. return str.replace(/\s+$/, "");
  118. },
  119. /**
  120. * removes white space from the left and the right side of a string
  121. *
  122. * @param str {String} the string to trim
  123. * @return {String} the trimmed string
  124. */
  125. trim : function(str) {
  126. return str.replace(/^\s+|\s+$/g, "");
  127. },
  128. /**
  129. * Check whether the string starts with the given substring
  130. *
  131. * @param fullstr {String} the string to search in
  132. * @param substr {String} the substring to look for
  133. * @return {Boolean} whether the string starts with the given substring
  134. */
  135. startsWith : function(fullstr, substr) {
  136. return fullstr.indexOf(substr) === 0;
  137. },
  138. /**
  139. * Check whether the string ends with the given substring
  140. *
  141. * @param fullstr {String} the string to search in
  142. * @param substr {String} the substring to look for
  143. * @return {Boolean} whether the string ends with the given substring
  144. */
  145. endsWith : function(fullstr, substr) {
  146. return fullstr.substring(fullstr.length - substr.length, fullstr.length) === substr;
  147. },
  148. /**
  149. * Returns a string, which repeats a string 'length' times
  150. *
  151. * @param str {String} string used to repeat
  152. * @param times {Integer} the number of repetitions
  153. * @return {String} repeated string
  154. */
  155. repeat : function(str, times) {
  156. return str.length >= 0 ? new Array(times + 1).join(str) : "";
  157. },
  158. /**
  159. * Pad a string up to a given length. Padding characters are added to the left of the string.
  160. *
  161. * @param str {String} the string to pad
  162. * @param length {Integer} the final length of the string
  163. * @param ch {String} character used to fill up the string
  164. * @return {String} padded string
  165. */
  166. pad : function(str, length, ch)
  167. {
  168. var padLength = length - str.length;
  169. if (padLength > 0)
  170. {
  171. if (typeof ch === "undefined") {
  172. ch = "0";
  173. }
  174. return this.repeat(ch, padLength) + str;
  175. }
  176. else
  177. {
  178. return str;
  179. }
  180. },
  181. /**
  182. * Convert the first character of the string to upper case.
  183. *
  184. * @signature function(str)
  185. * @param str {String} the string
  186. * @return {String} the string with a upper case first character
  187. */
  188. firstUp : qx.Bootstrap.firstUp,
  189. /**
  190. * Convert the first character of the string to lower case.
  191. *
  192. * @signature function(str)
  193. * @param str {String} the string
  194. * @return {String} the string with a lower case first character
  195. */
  196. firstLow : qx.Bootstrap.firstLow,
  197. /**
  198. * Check whether the string contains a given substring
  199. *
  200. * @param str {String} the string
  201. * @param substring {String} substring to search for
  202. * @return {Boolean} whether the string contains the substring
  203. */
  204. contains : function(str, substring) {
  205. return str.indexOf(substring) != -1;
  206. },
  207. /**
  208. * Print a list of arguments using a format string
  209. * In the format string occurrences of %n are replaced by the n'th element of the args list.
  210. * Example:
  211. * <pre class='javascript'>qx.lang.String.format("Hello %1, my name is %2", ["Egon", "Franz"]) == "Hello Egon, my name is Franz"</pre>
  212. *
  213. * @param pattern {String} format string
  214. * @param args {Array} array of arguments to insert into the format string
  215. * @return {String} the formatted string
  216. */
  217. format : function(pattern, args)
  218. {
  219. var str = pattern;
  220. for (var i=0; i<args.length; i++) {
  221. str = str.replace(new RegExp("%" + (i + 1), "g"), args[i]);
  222. }
  223. return str;
  224. },
  225. /**
  226. * Escapes all chars that have a special meaning in regular expressions
  227. *
  228. * @param str {String} the string where to escape the chars.
  229. * @return {String} the string with the escaped chars.
  230. */
  231. escapeRegexpChars : function(str) {
  232. return str.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1');
  233. },
  234. /**
  235. * Converts a string to an array of characters.
  236. * <pre>"hello" => [ "h", "e", "l", "l", "o" ];</pre>
  237. *
  238. * @param str {String} the string which should be split
  239. * @return {Array} the result array of characters
  240. */
  241. toArray : function(str) {
  242. return str.split(/\B|\b/g);
  243. },
  244. /**
  245. * Remove HTML/XML tags from a string
  246. * Example:
  247. * <pre class='javascript'>qx.lang.String.stripTags("&lt;h1>Hello&lt;/h1>") == "Hello"</pre>
  248. *
  249. * @param str {String} string containing tags
  250. * @return {String} the string with stripped tags
  251. */
  252. stripTags : function(str) {
  253. return str.replace(/<\/?[^>]+>/gi, "");
  254. },
  255. /**
  256. * Strips <script> tags including its content from the given string.
  257. *
  258. * @param str {String} string containing tags
  259. * @param exec {Boolean?false} Whether the filtered code should be executed
  260. * @return {String} The filtered string
  261. */
  262. stripScripts: function(str, exec)
  263. {
  264. var scripts = "";
  265. var text = str.replace(/<script[^>]*>([\s\S]*?)<\/script>/gi, function()
  266. {
  267. scripts += arguments[1] + '\n';
  268. return "";
  269. });
  270. if (exec === true) {
  271. qx.lang.Function.globalEval(scripts);
  272. }
  273. return text;
  274. }
  275. }
  276. });
  277. exports.String = qx.lang.String;