PageRenderTime 23ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/includes/js/mootools-core/Source/Types/String.js

https://github.com/KenBoyer/CompactCMS
JavaScript | 82 lines | 52 code | 15 blank | 15 comment | 6 complexity | 241e1bc97829066f3a7bdd1a5c201227 MD5 | raw file
  1. /*
  2. ---
  3. name: String
  4. description: Contains String Prototypes like camelCase, capitalize, test, and toInt.
  5. license: MIT-style license.
  6. requires: Type
  7. provides: String
  8. ...
  9. */
  10. String.implement({
  11. test: function(regex, params){
  12. return ((typeOf(regex) == 'regexp') ? regex : new RegExp('' + regex, params)).test(this);
  13. },
  14. contains: function(string, separator){
  15. return (separator) ? (separator + this + separator).indexOf(separator + string + separator) > -1 : this.indexOf(string) > -1;
  16. },
  17. trim: function(){
  18. return this.replace(/^\s+|\s+$/g, '');
  19. },
  20. clean: function(){
  21. return this.replace(/\s+/g, ' ').trim();
  22. },
  23. camelCase: function(){
  24. return this.replace(/-\D/g, function(match){
  25. return match.charAt(1).toUpperCase();
  26. });
  27. },
  28. hyphenate: function(){
  29. return this.replace(/[A-Z]/g, function(match){
  30. return ('-' + match.charAt(0).toLowerCase());
  31. });
  32. },
  33. capitalize: function(){
  34. return this.replace(/\b[a-z]/g, function(match){
  35. return match.toUpperCase();
  36. });
  37. },
  38. escapeRegExp: function(){
  39. return this.replace(/([-.*+?^${}()|[\]\/\\])/g, '\\$1');
  40. },
  41. toInt: function(base){
  42. return parseInt(this, base || 10);
  43. },
  44. toFloat: function(){
  45. return parseFloat(this);
  46. },
  47. hexToRgb: function(array){
  48. var hex = this.match(/^#?(\w{1,2})(\w{1,2})(\w{1,2})$/);
  49. return (hex) ? hex.slice(1).hexToRgb(array) : null;
  50. },
  51. rgbToHex: function(array){
  52. var rgb = this.match(/\d{1,3}/g);
  53. return (rgb) ? rgb.rgbToHex(array) : null;
  54. },
  55. substitute: function(object, regexp){
  56. return this.replace(regexp || (/\\?\{([^{}]+)\}/g), function(match, name){
  57. if (match.charAt(0) == '\\') return match.slice(1);
  58. return (object[name] != null) ? object[name] : '';
  59. });
  60. }
  61. });