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