Requires

Provides

String.Extras.js

Extends the String native object to include methods useful in managing various kinds of strings (query strings, urls, html, etc).

License:
MIT-style license
Authors:
Aaron Newton, Guillermo Rauch, Christopher Pitt
  1. 27
  2. 28
  3. 29
  4. 30
  5. 31
  6. 32
  7. 33
  8. 34
  9. 35
  10. 36
  11. 37
  12. 38
  13. 39
  14. 40
  15. 41
  16. 42
  17. 43
  18. 44
  19. 45
  20. 46
  21. 47
  22. 48
  23. 49
  24. 50
  25. 51
  26. 52
  27. 53
  28. 54
  29. 55
  30. 56
  31. 57
  32. 58
  33. 59
  34. 60
  35. 61
  36. 62
  37. 63
  38. 64
  39. 65
  40. 66
  41. 67
  42. 68
  43. 69
  44. 70
  45. 71
  46. 72
  47. 73
  48. 74
  49. 75
  50. 76
  51. 77
  52. 78
  53. 79
  54. 80
  55. 81
  56. 82
  57. 83
  58. 84
  59. 85
  60. 86
  61. 87
  62. 88
  63. 89
  64. 90
  65. 91
  66. 92
  67. 93
  68. 94
  69. 95
  70. 96
  71. 97
  72. 98
  73. 99
  74. 100
  75. 101
  76. 102
  77. 103
  78. 104
  79. 105
  80. 106
  81. 107
  82. 108
  83. 109
  84. 110
  85. 111
  86. 112
  87. 113
  88. 114
  89. 115
  90. 116
  91. 117
  92. 118
  93. 119
  94. 120
  95. 121
  96. 122
  97. 123
  98. 124
  99. 125
  100. 126
  101. 127
  102. 128
  103. 129
  104. 130
  105. 131
  106. 132
  107. 133
  108. 134
  109. 135
  110. 136
  111. 137
  112. 138
  113. 139
  114. 140
  115. 141
  116. 142
  117. 143
  118. 144
  119. 145
(function(){ var special = { 'a': '[àáâãäåăą]', 'A': '[ÀÁÂÃÄÅĂĄ]', 'c': '[ćčç]', 'C': '[ĆČÇ]', 'd': '[ďđ]', 'D': '[ĎÐ]', 'e': '[èéêëěę]', 'E': '[ÈÉÊËĚĘ]', 'g': '[ğ]', 'G': '[Ğ]', 'i': '[ìíîï]', 'I': '[ÌÍÎÏ]', 'l': '[ĺľł]', 'L': '[ĹĽŁ]', 'n': '[ñňń]', 'N': '[ÑŇŃ]', 'o': '[òóôõöøő]', 'O': '[ÒÓÔÕÖØ]', 'r': '[řŕ]', 'R': '[ŘŔ]', 's': '[ššş]', 'S': '[ŠŞŚ]', 't': '[ťţ]', 'T': '[ŤŢ]', 'ue': '[ü]', 'UE': '[Ü]', 'u': '[ùúûůµ]', 'U': '[ÙÚÛŮ]', 'y': '[ÿý]', 'Y': '[ŸÝ]', 'z': '[žźż]', 'Z': '[ŽŹŻ]', 'th': '[þ]', 'TH': '[Þ]', 'dh': '[ð]', 'DH': '[Ð]', 'ss': '[ß]', 'oe': '[œ]', 'OE': '[Œ]', 'ae': '[æ]', 'AE': '[Æ]' }, tidy = { ' ': '[\xa0\u2002\u2003\u2009]', '*': '[\xb7]', '\'': '[\u2018\u2019]', '"': '[\u201c\u201d]', '...': '[\u2026]', '-': '[\u2013]', '--': '[\u2014]', '&raquo;': '[\uFFFD]' }; function walk(string, replacements){ var result = string; for (key in replacements){ result = result.replace(new RegExp(replacements[key], 'g'), key); } return result; } function getRegexForTag(tag, contents){ tag = tag || ''; var regstr = contents ? "<" + tag + "[^>]*>([\\s\\S]*?)<\/" + tag + ">" : "<\/?" + tag + "([^>]+)?>"; reg = new RegExp(regstr, "gi"); return reg; }; String.implement({ standardize: function(){ return walk(this, special); }, repeat: function(times){ return new Array(times + 1).join(this); }, pad: function(length, str, direction){ if (this.length >= length) return this; var pad = (str == null ? ' ' : '' + str) .repeat(length - this.length) .substr(0, length - this.length); if (!direction || direction == 'right') return this + pad; if (direction == 'left') return pad + this; return pad.substr(0, (pad.length / 2).floor()) + this + pad.substr(0, (pad.length / 2).ceil()); }, getTags: function(tag, contents){ return this.match(getRegexForTag(tag, contents)) || []; }, stripTags: function(tag, contents){ return this.replace(getRegexForTag(tag, contents), ''); }, tidy: function(){ return walk(this, tidy); } }); var UID = Math.floor(Math.random() * 10e12); String.uniqueID = function(){ return (UID++).toString(36); }; })();