UNPKG

2.42 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim
4var SPACE_REGEX = '\\s\\uFEFF\\xA0';
5// https://remarkablemark.org/blog/2019/09/28/javascript-remove-punctuation/
6var DEFAULT_PUNCTUATION_REGEX = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~';
7/**
8 * split the long text to short texts
9 * Time Complexity: O(n)
10 *
11 * @param {string} text
12 * @param {object?} option
13 * @param {number?} option.maxLength default is 200
14 * @param {string?} option.splitPunct default is ''
15 * @returns {string[]} short text list
16 */
17var splitLongText = function (text, _a) {
18 var _b = _a === void 0 ? {} : _a, _c = _b.maxLength, maxLength = _c === void 0 ? 200 : _c, _d = _b.splitPunct, splitPunct = _d === void 0 ? '' : _d;
19 var isSpaceOrPunct = function (s, i) {
20 var regex = new RegExp('[' + SPACE_REGEX + DEFAULT_PUNCTUATION_REGEX + splitPunct + ']');
21 return regex.test(s.charAt(i));
22 };
23 var lastIndexOfSpaceOrPunct = function (s, left, right) {
24 for (var i = right; i >= left; i--) {
25 if (isSpaceOrPunct(s, i))
26 return i;
27 }
28 return -1; // not found
29 };
30 var result = [];
31 var addResult = function (text, start, end) {
32 result.push(text.slice(start, end + 1));
33 };
34 var start = 0;
35 for (;;) {
36 // check text's length
37 if (text.length - start <= maxLength) {
38 addResult(text, start, text.length - 1);
39 break; // end of text
40 }
41 // check whether the word is cut in the middle.
42 var end = start + maxLength - 1;
43 if (isSpaceOrPunct(text, end) || isSpaceOrPunct(text, end + 1)) {
44 addResult(text, start, end);
45 start = end + 1;
46 continue;
47 }
48 // find last index of space
49 end = lastIndexOfSpaceOrPunct(text, start, end);
50 if (end === -1) {
51 var str = text.slice(start, start + maxLength);
52 throw new Error('The word is too long to split into a short text:' +
53 ("\n" + str + " ...") +
54 '\n\nTry the option "splitPunct" to split the text by punctuation.');
55 }
56 // add result
57 addResult(text, start, end);
58 start = end + 1;
59 }
60 return result;
61};
62exports.default = splitLongText;
63//# sourceMappingURL=splitLongText.js.map
\No newline at end of file