/src/ext/sugar/String.js

https://github.com/zynga/core · JavaScript · 174 lines · 75 code · 35 blank · 64 comment · 8 complexity · 48840544a0fdbbfaa11cb36fd086cd39 MD5 · raw file

  1. /*
  2. ==================================================================================================
  3. Core - JavaScript Foundation
  4. Copyright 2010-2012 Zynga Inc.
  5. ==================================================================================================
  6. */
  7. (function()
  8. {
  9. var hexTable = "0123456789abcdef".split("");
  10. /**
  11. * Adds useful non-standard extensions to the `String.prototype` like {#hyphenate}, {#startsWith} and {#contains}.
  12. */
  13. core.Main.addMembers("String",
  14. {
  15. /**
  16. * {String} Encodes the string as Base64.
  17. *
  18. * #require(ext.Base64)
  19. */
  20. encodeBase64 : function() {
  21. return btoa(this);
  22. },
  23. /**
  24. * {String} Decodes the string from Base64.
  25. *
  26. * #require(ext.Base64)
  27. */
  28. decodeBase64 : function() {
  29. return atob(this);
  30. },
  31. /**
  32. * {String} Converts the string into a hex string
  33. */
  34. toHex : function()
  35. {
  36. var output = "";
  37. var code;
  38. for (var i = 0, l = this.length; i < l; i++)
  39. {
  40. code = this.charCodeAt(i);
  41. output += hexTable[(code >>> 4) & 0x0F] + hexTable[code & 0x0F];
  42. }
  43. return output;
  44. },
  45. /**
  46. * {String} Encodes the string as UTF-8.
  47. *
  48. * Via: http://ecmanaut.blogspot.com/2006/07/encoding-decoding-utf8-in-javascript.html
  49. */
  50. encodeUtf8 : function() {
  51. return unescape(encodeURIComponent(this));
  52. },
  53. /**
  54. * {String} Decodes the string from UTF-8.
  55. *
  56. * Via: http://ecmanaut.blogspot.com/2006/07/encoding-decoding-utf8-in-javascript.html
  57. */
  58. decodeUtf8 : function() {
  59. return decodeURIComponent(escape(this));
  60. },
  61. /**
  62. * Whether the string contains the given @substring {String}.
  63. */
  64. contains : function(substring) {
  65. return this.indexOf(substring) != -1;
  66. },
  67. /**
  68. * {Boolean} Returns true if the string has a length of 0 or contains only whitespace.
  69. */
  70. isBlank : function() {
  71. return this.trim().length == 0;
  72. },
  73. /**
  74. * {String} Reverses the string
  75. */
  76. reverse : function() {
  77. return this.split("").reverse().join("");
  78. },
  79. /**
  80. * {String} Removes double spaces and line breaks.
  81. */
  82. compact : function() {
  83. return this.replace(/[\r\n]/g, " ").trim().replace(/([\s ])+/g, '$1');
  84. },
  85. /**
  86. * {String} Returns a hyphenated copy of the original string e.g.
  87. *
  88. * - camelCase => camel-case
  89. * - HelloWorld => -hello-world
  90. */
  91. hyphenate : function()
  92. {
  93. // Via: http://es5.github.com/#x15.5.4.11
  94. return this.replace(/[A-Z]/g,'-$&').toLowerCase();
  95. },
  96. /**
  97. * {String} Camelizes this string.
  98. */
  99. camelize : function ()
  100. {
  101. return this.replace(/\-+(\S)?/g, function(match, chr) {
  102. return chr ? chr.toUpperCase() : '';
  103. });
  104. },
  105. /**
  106. * {String} Returns a new string which is a @nr {Integer} repeated copy of the original one.
  107. */
  108. repeat : function(nr)
  109. {
  110. // Optimized by: http://jsperf.com/repeat-vs-repeat/3
  111. if (nr < 1) {
  112. return '';
  113. }
  114. var pattern = this;
  115. var result = "";
  116. while (nr > 0)
  117. {
  118. if (nr & 1) {
  119. result += pattern;
  120. }
  121. nr >>= 1;
  122. pattern += pattern;
  123. }
  124. return result;
  125. },
  126. /**
  127. * {Boolean} Returns `true` if this string starts with the given substring @begin {String}
  128. */
  129. startsWith : function(begin) {
  130. return begin == this.slice(0, begin.length);
  131. },
  132. /**
  133. * {Boolean} Returns `true` if this string ends with the given substring @end {String}
  134. */
  135. endsWith : function(end) {
  136. return end == this.slice(-end.length);
  137. }
  138. });
  139. })();