SpecialString.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. "use strict";
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  4. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  5. // Generated by CoffeeScript 2.5.1
  6. var SpecialString, i, len, prop, ref;
  7. module.exports = SpecialString = function () {
  8. var self;
  9. var SpecialString = /*#__PURE__*/function () {
  10. function SpecialString(str) {
  11. _classCallCheck(this, SpecialString);
  12. if (!(this instanceof self)) {
  13. return new self(str);
  14. }
  15. this._str = String(str);
  16. this._len = 0;
  17. }
  18. _createClass(SpecialString, [{
  19. key: "_getStr",
  20. value: function _getStr() {
  21. return this._str;
  22. }
  23. }, {
  24. key: "set",
  25. value: function set(str) {
  26. this._str = String(str);
  27. return this;
  28. }
  29. }, {
  30. key: "clone",
  31. value: function clone() {
  32. return new SpecialString(this._str);
  33. }
  34. }, {
  35. key: "isEmpty",
  36. value: function isEmpty() {
  37. return this._str === '';
  38. }
  39. }, {
  40. key: "isOnlySpecialChars",
  41. value: function isOnlySpecialChars() {
  42. return !this.isEmpty() && this.length === 0;
  43. }
  44. }, {
  45. key: "_reset",
  46. value: function _reset() {
  47. return this._len = 0;
  48. }
  49. }, {
  50. key: "splitIn",
  51. value: function splitIn(limit) {
  52. var trimLeftEachLine = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  53. var buffer, bufferLength, justSkippedSkipChar, lines;
  54. buffer = '';
  55. bufferLength = 0;
  56. lines = [];
  57. justSkippedSkipChar = false;
  58. self._countChars(this._str, function (char, charLength) {
  59. if (bufferLength > limit || bufferLength + charLength > limit) {
  60. lines.push(buffer);
  61. buffer = '';
  62. bufferLength = 0;
  63. }
  64. if (bufferLength === 0 && char === ' ' && !justSkippedSkipChar && trimLeftEachLine) {
  65. return justSkippedSkipChar = true;
  66. } else {
  67. buffer += char;
  68. bufferLength += charLength;
  69. return justSkippedSkipChar = false;
  70. }
  71. });
  72. if (buffer.length > 0) {
  73. lines.push(buffer);
  74. }
  75. return lines;
  76. }
  77. }, {
  78. key: "trim",
  79. value: function trim() {
  80. return new SpecialString(this.str.trim());
  81. }
  82. }, {
  83. key: "trimLeft",
  84. value: function trimLeft() {
  85. return new SpecialString(this.str.replace(/^\s+/, ''));
  86. }
  87. }, {
  88. key: "trimRight",
  89. value: function trimRight() {
  90. return new SpecialString(this.str.replace(/\s+$/, ''));
  91. }
  92. }, {
  93. key: "_getLength",
  94. value: function _getLength() {
  95. var sum;
  96. sum = 0;
  97. self._countChars(this._str, function (char, charLength) {
  98. sum += charLength;
  99. });
  100. return sum;
  101. }
  102. }, {
  103. key: "cut",
  104. value: function cut(from, to) {
  105. var _this = this;
  106. var trimLeft = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  107. var after, before, cur, cut;
  108. if (to == null) {
  109. to = this.length;
  110. }
  111. from = parseInt(from);
  112. if (from >= to) {
  113. throw Error("`from` shouldn't be larger than `to`");
  114. }
  115. before = '';
  116. after = '';
  117. cut = '';
  118. cur = 0;
  119. self._countChars(this._str, function (char, charLength) {
  120. if (_this.str === 'ab<tag>') {
  121. console.log(charLength, char);
  122. }
  123. if (cur === from && char.match(/^\s+$/) && trimLeft) {
  124. return;
  125. }
  126. if (cur < from) {
  127. before += char; // let's be greedy
  128. } else if (cur < to || cur + charLength <= to) {
  129. cut += char;
  130. } else {
  131. after += char;
  132. }
  133. cur += charLength;
  134. });
  135. this._str = before + after;
  136. this._reset();
  137. return new SpecialString(cut);
  138. }
  139. }], [{
  140. key: "_countChars",
  141. value: function _countChars(text, cb) {
  142. var char, charLength, m;
  143. while (text.length !== 0) {
  144. if (m = text.match(self._tagRx)) {
  145. char = m[0];
  146. charLength = 0;
  147. text = text.substr(char.length, text.length);
  148. } else if (m = text.match(self._quotedHtmlRx)) {
  149. char = m[0];
  150. charLength = 1;
  151. text = text.substr(char.length, text.length);
  152. } else if (text.match(self._tabRx)) {
  153. char = "\t";
  154. charLength = 8;
  155. text = text.substr(1, text.length);
  156. } else {
  157. char = text[0];
  158. charLength = 1;
  159. text = text.substr(1, text.length);
  160. }
  161. cb.call(null, char, charLength);
  162. }
  163. }
  164. }]);
  165. return SpecialString;
  166. }();
  167. ;
  168. self = SpecialString;
  169. SpecialString._tabRx = /^\t/;
  170. SpecialString._tagRx = /^<[^>]+>/;
  171. SpecialString._quotedHtmlRx = /^&(gt|lt|quot|amp|apos|sp);/;
  172. return SpecialString;
  173. }.call(void 0);
  174. ref = ['str', 'length'];
  175. for (i = 0, len = ref.length; i < len; i++) {
  176. prop = ref[i];
  177. (function () {
  178. var methodName;
  179. methodName = '_get' + prop[0].toUpperCase() + prop.substr(1, prop.length);
  180. return SpecialString.prototype.__defineGetter__(prop, function () {
  181. return this[methodName]();
  182. });
  183. })();
  184. }