SpecialString.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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: "_getLength",
  84. value: function _getLength() {
  85. var sum;
  86. sum = 0;
  87. self._countChars(this._str, function (char, charLength) {
  88. sum += charLength;
  89. });
  90. return sum;
  91. }
  92. }, {
  93. key: "cut",
  94. value: function cut(from, to) {
  95. var _this = this;
  96. var trimLeft = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  97. var after, before, cur, cut;
  98. if (to == null) {
  99. to = this.length;
  100. }
  101. from = parseInt(from);
  102. if (from >= to) {
  103. throw Error("`from` shouldn't be larger than `to`");
  104. }
  105. before = '';
  106. after = '';
  107. cut = '';
  108. cur = 0;
  109. self._countChars(this._str, function (char, charLength) {
  110. if (_this.str === 'ab<tag>') {
  111. console.log(charLength, char);
  112. }
  113. if (cur === from && char.match(/^\s+$/) && trimLeft) {
  114. return;
  115. }
  116. if (cur < from) {
  117. before += char; // let's be greedy
  118. } else if (cur < to || cur + charLength <= to) {
  119. cut += char;
  120. } else {
  121. after += char;
  122. }
  123. cur += charLength;
  124. });
  125. this._str = before + after;
  126. this._reset();
  127. return new SpecialString(cut);
  128. }
  129. }], [{
  130. key: "_countChars",
  131. value: function _countChars(text, cb) {
  132. var char, charLength, m;
  133. while (text.length !== 0) {
  134. if (m = text.match(self._tagRx)) {
  135. char = m[0];
  136. charLength = 0;
  137. text = text.substr(char.length, text.length);
  138. } else if (m = text.match(self._quotedHtmlRx)) {
  139. char = m[0];
  140. charLength = 1;
  141. text = text.substr(char.length, text.length);
  142. } else if (text.match(self._tabRx)) {
  143. char = "\t";
  144. charLength = 8;
  145. text = text.substr(1, text.length);
  146. } else {
  147. char = text[0];
  148. charLength = 1;
  149. text = text.substr(1, text.length);
  150. }
  151. cb.call(null, char, charLength);
  152. }
  153. }
  154. }]);
  155. return SpecialString;
  156. }();
  157. ;
  158. self = SpecialString;
  159. SpecialString._tabRx = /^\t/;
  160. SpecialString._tagRx = /^<[^>]+>/;
  161. SpecialString._quotedHtmlRx = /^&(gt|lt|quot|amp|apos|sp);/;
  162. return SpecialString;
  163. }.call(void 0);
  164. ref = ['str', 'length'];
  165. for (i = 0, len = ref.length; i < len; i++) {
  166. prop = ref[i];
  167. (function () {
  168. var methodName;
  169. methodName = '_get' + prop[0].toUpperCase() + prop.substr(1, prop.length);
  170. return SpecialString.prototype.__defineGetter__(prop, function () {
  171. return this[methodName]();
  172. });
  173. })();
  174. }