string-remove-widows 3.0.5 → 3.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/string-remove-widows.esm.js +534 -305
- package/dist/string-remove-widows.umd.js +10 -10
- package/package.json +23 -27
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @name string-remove-widows
|
|
3
3
|
* @fileoverview Helps to prevent widow words in a text
|
|
4
|
-
* @version 3.0.
|
|
4
|
+
* @version 3.0.6
|
|
5
5
|
* @author Roy Revelt, Codsen Ltd
|
|
6
6
|
* @license MIT
|
|
7
7
|
* {@link https://codsen.com/os/string-remove-widows/}
|
|
@@ -12,7 +12,7 @@ import { left, right } from 'string-left-right';
|
|
|
12
12
|
import { Ranges } from 'ranges-push';
|
|
13
13
|
import { rApply } from 'ranges-apply';
|
|
14
14
|
|
|
15
|
-
var version$1 = "3.0.
|
|
15
|
+
var version$1 = "3.0.6";
|
|
16
16
|
|
|
17
17
|
const rawnbsp = "\u00A0";
|
|
18
18
|
const encodedNbspHtml = " ";
|
|
@@ -26,334 +26,563 @@ const rawMdash = "\u2014";
|
|
|
26
26
|
const encodedMdashHtml = "—";
|
|
27
27
|
const encodedMdashCss = "\\2014";
|
|
28
28
|
const encodedMdashJs = "\\u2014";
|
|
29
|
-
const headsAndTailsJinja = [
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
},
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
},
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}]
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
29
|
+
const headsAndTailsJinja = [
|
|
30
|
+
{
|
|
31
|
+
heads: "{{",
|
|
32
|
+
tails: "}}",
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
heads: ["{% if", "{%- if"],
|
|
36
|
+
tails: ["{% endif", "{%- endif"],
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
heads: ["{% for", "{%- for"],
|
|
40
|
+
tails: ["{% endfor", "{%- endfor"],
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
heads: ["{%", "{%-"],
|
|
44
|
+
tails: ["%}", "-%}"],
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
heads: "{#",
|
|
48
|
+
tails: "#}",
|
|
49
|
+
},
|
|
50
|
+
];
|
|
51
|
+
const headsAndTailsHugo = [
|
|
52
|
+
{
|
|
53
|
+
heads: "{{",
|
|
54
|
+
tails: "}}",
|
|
55
|
+
},
|
|
56
|
+
];
|
|
57
|
+
const headsAndTailsHexo = [
|
|
58
|
+
{
|
|
59
|
+
heads: ["<%", "<%=", "<%-"],
|
|
60
|
+
tails: ["%>", "=%>", "-%>"],
|
|
61
|
+
},
|
|
62
|
+
];
|
|
63
|
+
const knownHTMLTags = [
|
|
64
|
+
"abbr",
|
|
65
|
+
"address",
|
|
66
|
+
"area",
|
|
67
|
+
"article",
|
|
68
|
+
"aside",
|
|
69
|
+
"audio",
|
|
70
|
+
"base",
|
|
71
|
+
"bdi",
|
|
72
|
+
"bdo",
|
|
73
|
+
"blockquote",
|
|
74
|
+
"body",
|
|
75
|
+
"br",
|
|
76
|
+
"button",
|
|
77
|
+
"canvas",
|
|
78
|
+
"caption",
|
|
79
|
+
"center",
|
|
80
|
+
"cite",
|
|
81
|
+
"code",
|
|
82
|
+
"col",
|
|
83
|
+
"colgroup",
|
|
84
|
+
"data",
|
|
85
|
+
"datalist",
|
|
86
|
+
"dd",
|
|
87
|
+
"del",
|
|
88
|
+
"details",
|
|
89
|
+
"dfn",
|
|
90
|
+
"dialog",
|
|
91
|
+
"div",
|
|
92
|
+
"dl",
|
|
93
|
+
"doctype",
|
|
94
|
+
"dt",
|
|
95
|
+
"em",
|
|
96
|
+
"embed",
|
|
97
|
+
"fieldset",
|
|
98
|
+
"figcaption",
|
|
99
|
+
"figure",
|
|
100
|
+
"footer",
|
|
101
|
+
"form",
|
|
102
|
+
"h1",
|
|
103
|
+
"h2",
|
|
104
|
+
"h3",
|
|
105
|
+
"h4",
|
|
106
|
+
"h5",
|
|
107
|
+
"h6",
|
|
108
|
+
"head",
|
|
109
|
+
"header",
|
|
110
|
+
"hgroup",
|
|
111
|
+
"hr",
|
|
112
|
+
"html",
|
|
113
|
+
"iframe",
|
|
114
|
+
"img",
|
|
115
|
+
"input",
|
|
116
|
+
"ins",
|
|
117
|
+
"kbd",
|
|
118
|
+
"keygen",
|
|
119
|
+
"label",
|
|
120
|
+
"legend",
|
|
121
|
+
"li",
|
|
122
|
+
"link",
|
|
123
|
+
"main",
|
|
124
|
+
"map",
|
|
125
|
+
"mark",
|
|
126
|
+
"math",
|
|
127
|
+
"menu",
|
|
128
|
+
"menuitem",
|
|
129
|
+
"meta",
|
|
130
|
+
"meter",
|
|
131
|
+
"nav",
|
|
132
|
+
"noscript",
|
|
133
|
+
"object",
|
|
134
|
+
"ol",
|
|
135
|
+
"optgroup",
|
|
136
|
+
"option",
|
|
137
|
+
"output",
|
|
138
|
+
"param",
|
|
139
|
+
"picture",
|
|
140
|
+
"pre",
|
|
141
|
+
"progress",
|
|
142
|
+
"rb",
|
|
143
|
+
"rp",
|
|
144
|
+
"rt",
|
|
145
|
+
"rtc",
|
|
146
|
+
"ruby",
|
|
147
|
+
"samp",
|
|
148
|
+
"script",
|
|
149
|
+
"section",
|
|
150
|
+
"select",
|
|
151
|
+
"slot",
|
|
152
|
+
"small",
|
|
153
|
+
"source",
|
|
154
|
+
"span",
|
|
155
|
+
"strong",
|
|
156
|
+
"style",
|
|
157
|
+
"sub",
|
|
158
|
+
"summary",
|
|
159
|
+
"sup",
|
|
160
|
+
"svg",
|
|
161
|
+
"table",
|
|
162
|
+
"tbody",
|
|
163
|
+
"td",
|
|
164
|
+
"template",
|
|
165
|
+
"textarea",
|
|
166
|
+
"tfoot",
|
|
167
|
+
"th",
|
|
168
|
+
"thead",
|
|
169
|
+
"time",
|
|
170
|
+
"title",
|
|
171
|
+
"tr",
|
|
172
|
+
"track",
|
|
173
|
+
"ul",
|
|
174
|
+
"var",
|
|
175
|
+
"video",
|
|
176
|
+
"wbr",
|
|
177
|
+
"xml",
|
|
178
|
+
];
|
|
54
179
|
|
|
55
180
|
const version = version$1;
|
|
56
181
|
const defaults = {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
182
|
+
removeWidowPreventionMeasures: false,
|
|
183
|
+
convertEntities: true,
|
|
184
|
+
targetLanguage: "html",
|
|
185
|
+
UKPostcodes: false,
|
|
186
|
+
hyphens: true,
|
|
187
|
+
minWordCount: 4,
|
|
188
|
+
minCharCount: 5,
|
|
189
|
+
ignore: [],
|
|
190
|
+
reportProgressFunc: null,
|
|
191
|
+
reportProgressFuncFrom: 0,
|
|
192
|
+
reportProgressFuncTo: 100,
|
|
193
|
+
tagRanges: [],
|
|
69
194
|
};
|
|
70
195
|
function removeWidows(str, originalOpts) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
} else {
|
|
76
|
-
throw new Error(`string-remove-widows: [THROW_ID_02] the first input argument must be string! It was given as "${typeof str}", equal to:\n${JSON.stringify(str, null, 4)}`);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
if (originalOpts && typeof originalOpts !== "object") {
|
|
80
|
-
throw new Error(`string-remove-widows: [THROW_ID_03] the second input argument, options object, should be a plain object but it was given as type ${typeof originalOpts}, equal to ${JSON.stringify(originalOpts, null, 4)}`);
|
|
81
|
-
}
|
|
82
|
-
const isArr = Array.isArray;
|
|
83
|
-
const len = str.length;
|
|
84
|
-
const rangesArr = new Ranges({
|
|
85
|
-
mergeType: 2
|
|
86
|
-
});
|
|
87
|
-
const punctuationCharsToConsiderWidowIssue = ["."];
|
|
88
|
-
const postcodeRegexFront = /[A-Z]{1,2}[0-9][0-9A-Z]?$/;
|
|
89
|
-
const postcodeRegexEnd = /^[0-9][A-Z]{2}/;
|
|
90
|
-
const leavePercForLastStage = 0.06;
|
|
91
|
-
let currentPercentageDone;
|
|
92
|
-
let lastPercentage = 0;
|
|
93
|
-
let wordCount = 0;
|
|
94
|
-
let charCount = 0;
|
|
95
|
-
let secondToLastWhitespaceStartedAt;
|
|
96
|
-
let secondToLastWhitespaceEndedAt;
|
|
97
|
-
let lastWhitespaceStartedAt;
|
|
98
|
-
let lastWhitespaceEndedAt;
|
|
99
|
-
let lastEncodedNbspStartedAt;
|
|
100
|
-
let lastEncodedNbspEndedAt;
|
|
101
|
-
let doNothingUntil;
|
|
102
|
-
let bumpWordCountAt;
|
|
103
|
-
const opts = { ...defaults,
|
|
104
|
-
...originalOpts
|
|
105
|
-
};
|
|
106
|
-
const whatWasDone = {
|
|
107
|
-
removeWidows: false,
|
|
108
|
-
convertEntities: false
|
|
109
|
-
};
|
|
110
|
-
if (!opts.ignore || !isArr(opts.ignore) && typeof opts.ignore !== "string") {
|
|
111
|
-
opts.ignore = [];
|
|
112
|
-
} else {
|
|
113
|
-
if (typeof opts.ignore === "string") {
|
|
114
|
-
opts.ignore = [opts.ignore];
|
|
115
|
-
}
|
|
116
|
-
if (opts.ignore.includes("all")) {
|
|
117
|
-
opts.ignore = opts.ignore.concat(headsAndTailsJinja.concat(headsAndTailsHexo));
|
|
118
|
-
} else if (opts.ignore.some(val => typeof val === "string")) {
|
|
119
|
-
let temp = [];
|
|
120
|
-
opts.ignore = opts.ignore.filter(val => {
|
|
121
|
-
if (typeof val === "string" && val.length) {
|
|
122
|
-
if (["nunjucks", "jinja", "liquid"].includes(val.trim().toLowerCase())) {
|
|
123
|
-
temp = temp.concat(headsAndTailsJinja);
|
|
124
|
-
} else if (["hugo"].includes(val.trim().toLowerCase())) {
|
|
125
|
-
temp = temp.concat(headsAndTailsHugo);
|
|
126
|
-
} else if (["hexo"].includes(val.trim().toLowerCase())) {
|
|
127
|
-
temp = temp.concat(headsAndTailsHexo);
|
|
128
|
-
}
|
|
129
|
-
return false;
|
|
196
|
+
const start = Date.now();
|
|
197
|
+
if (typeof str !== "string") {
|
|
198
|
+
if (str === undefined) {
|
|
199
|
+
throw new Error("string-remove-widows: [THROW_ID_01] the first input argument is completely missing! It should be given as string.");
|
|
130
200
|
}
|
|
131
|
-
|
|
132
|
-
|
|
201
|
+
else {
|
|
202
|
+
throw new Error(`string-remove-widows: [THROW_ID_02] the first input argument must be string! It was given as "${typeof str}", equal to:\n${JSON.stringify(str, null, 4)}`);
|
|
133
203
|
}
|
|
134
|
-
});
|
|
135
|
-
if (temp.length) {
|
|
136
|
-
opts.ignore = opts.ignore.concat(temp);
|
|
137
|
-
}
|
|
138
204
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
if (opts.reportProgressFunc) {
|
|
142
|
-
ceil = Math.floor(opts.reportProgressFuncTo - (opts.reportProgressFuncTo - opts.reportProgressFuncFrom) * leavePercForLastStage - opts.reportProgressFuncFrom);
|
|
143
|
-
}
|
|
144
|
-
function push(finalStart, finalEnd) {
|
|
145
|
-
let finalWhatToInsert = rawnbsp;
|
|
146
|
-
if (opts.removeWidowPreventionMeasures) {
|
|
147
|
-
finalWhatToInsert = " ";
|
|
148
|
-
} else if (opts.convertEntities) {
|
|
149
|
-
finalWhatToInsert = encodedNbspHtml;
|
|
150
|
-
if (typeof opts.targetLanguage === "string") {
|
|
151
|
-
if (opts.targetLanguage.trim().toLowerCase() === "css") {
|
|
152
|
-
finalWhatToInsert = encodedNbspCss;
|
|
153
|
-
} else if (opts.targetLanguage.trim().toLowerCase() === "js") {
|
|
154
|
-
finalWhatToInsert = encodedNbspJs;
|
|
155
|
-
}
|
|
156
|
-
}
|
|
205
|
+
if (originalOpts && typeof originalOpts !== "object") {
|
|
206
|
+
throw new Error(`string-remove-widows: [THROW_ID_03] the second input argument, options object, should be a plain object but it was given as type ${typeof originalOpts}, equal to ${JSON.stringify(originalOpts, null, 4)}`);
|
|
157
207
|
}
|
|
158
|
-
|
|
159
|
-
|
|
208
|
+
const isArr = Array.isArray;
|
|
209
|
+
const len = str.length;
|
|
210
|
+
const rangesArr = new Ranges({ mergeType: 2 });
|
|
211
|
+
const punctuationCharsToConsiderWidowIssue = ["."];
|
|
212
|
+
const postcodeRegexFront = /[A-Z]{1,2}[0-9][0-9A-Z]?$/;
|
|
213
|
+
const postcodeRegexEnd = /^[0-9][A-Z]{2}/;
|
|
214
|
+
const leavePercForLastStage = 0.06;
|
|
215
|
+
let currentPercentageDone;
|
|
216
|
+
let lastPercentage = 0;
|
|
217
|
+
let wordCount = 0;
|
|
218
|
+
let charCount = 0;
|
|
219
|
+
let secondToLastWhitespaceStartedAt;
|
|
220
|
+
let secondToLastWhitespaceEndedAt;
|
|
221
|
+
let lastWhitespaceStartedAt;
|
|
222
|
+
let lastWhitespaceEndedAt;
|
|
223
|
+
let lastEncodedNbspStartedAt;
|
|
224
|
+
let lastEncodedNbspEndedAt;
|
|
225
|
+
let doNothingUntil;
|
|
226
|
+
let bumpWordCountAt;
|
|
227
|
+
const opts = { ...defaults, ...originalOpts };
|
|
228
|
+
const whatWasDone = {
|
|
229
|
+
removeWidows: false,
|
|
230
|
+
convertEntities: false,
|
|
231
|
+
};
|
|
232
|
+
if (!opts.ignore ||
|
|
233
|
+
(!isArr(opts.ignore) && typeof opts.ignore !== "string")) {
|
|
234
|
+
opts.ignore = [];
|
|
160
235
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
236
|
+
else {
|
|
237
|
+
if (typeof opts.ignore === "string") {
|
|
238
|
+
opts.ignore = [opts.ignore];
|
|
239
|
+
}
|
|
240
|
+
if (opts.ignore.includes("all")) {
|
|
241
|
+
opts.ignore = opts.ignore.concat(headsAndTailsJinja.concat(headsAndTailsHexo));
|
|
242
|
+
}
|
|
243
|
+
else if (opts.ignore.some((val) => typeof val === "string")) {
|
|
244
|
+
let temp = [];
|
|
245
|
+
opts.ignore = opts.ignore.filter((val) => {
|
|
246
|
+
if (typeof val === "string" && val.length) {
|
|
247
|
+
if (["nunjucks", "jinja", "liquid"].includes(val.trim().toLowerCase())) {
|
|
248
|
+
temp = temp.concat(headsAndTailsJinja);
|
|
249
|
+
}
|
|
250
|
+
else if (["hugo"].includes(val.trim().toLowerCase())) {
|
|
251
|
+
temp = temp.concat(headsAndTailsHugo);
|
|
252
|
+
}
|
|
253
|
+
else if (["hexo"].includes(val.trim().toLowerCase())) {
|
|
254
|
+
temp = temp.concat(headsAndTailsHexo);
|
|
255
|
+
}
|
|
256
|
+
return false;
|
|
257
|
+
}
|
|
258
|
+
if (typeof val === "object") {
|
|
259
|
+
return true;
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
if (temp.length) {
|
|
263
|
+
opts.ignore = opts.ignore.concat(temp);
|
|
264
|
+
}
|
|
180
265
|
}
|
|
181
|
-
});
|
|
182
|
-
}
|
|
183
|
-
if (!doNothingUntil && bumpWordCountAt && bumpWordCountAt === i) {
|
|
184
|
-
wordCount += 1;
|
|
185
|
-
bumpWordCountAt = undefined;
|
|
186
|
-
}
|
|
187
|
-
if (typeof opts.reportProgressFunc === "function") {
|
|
188
|
-
currentPercentageDone = opts.reportProgressFuncFrom + Math.floor(i / len * (ceil || 1));
|
|
189
|
-
if (currentPercentageDone !== lastPercentage) {
|
|
190
|
-
lastPercentage = currentPercentageDone;
|
|
191
|
-
opts.reportProgressFunc(currentPercentageDone);
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
if (!doNothingUntil && i && str[i] && str[i].trim() && (!str[i - 1] || str[i - 1] && !str[i - 1].trim())) {
|
|
195
|
-
lastWhitespaceEndedAt = i;
|
|
196
|
-
}
|
|
197
|
-
if (!doNothingUntil && str[i] && str[i].trim()) {
|
|
198
|
-
charCount += 1;
|
|
199
|
-
}
|
|
200
|
-
if (!doNothingUntil && opts.hyphens && (`-${rawMdash}${rawNdash}`.includes(str[i]) || str.startsWith(encodedNdashHtml, i) || str.startsWith(encodedNdashCss, i) || str.startsWith(encodedNdashJs, i) || str.startsWith(encodedMdashHtml, i) || str.startsWith(encodedMdashCss, i) || str.startsWith(encodedMdashJs, i)) && str[i + 1] && (!str[i + 1].trim() || str[i] === "&")) {
|
|
201
|
-
if (str[i - 1] && !str[i - 1].trim() && str[left(str, i)]) {
|
|
202
|
-
push(left(str, i) + 1, i);
|
|
203
|
-
whatWasDone.removeWidows = true;
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
if (!doNothingUntil && (str.startsWith(" ", i) || str.startsWith(" ", i))) {
|
|
207
|
-
lastEncodedNbspStartedAt = i;
|
|
208
|
-
lastEncodedNbspEndedAt = i + 6;
|
|
209
|
-
if (str[i + 6] && str[i + 6].trim()) {
|
|
210
|
-
bumpWordCountAt = i + 6;
|
|
211
|
-
}
|
|
212
|
-
if (!opts.convertEntities) {
|
|
213
|
-
rangesArr.push(i, i + 6, rawnbsp);
|
|
214
|
-
whatWasDone.convertEntities = true;
|
|
215
|
-
} else if (opts.targetLanguage === "css" || opts.targetLanguage === "js") {
|
|
216
|
-
rangesArr.push(i, i + 6, opts.targetLanguage === "css" ? encodedNbspCss : encodedNbspJs);
|
|
217
|
-
whatWasDone.convertEntities = true;
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
if (!doNothingUntil && str[i + 4] && str[i] === "\\" && str[i + 1] === "0" && str[i + 2] === "0" && str[i + 3].toUpperCase() === "A" && str[i + 4] === "0") {
|
|
221
|
-
lastEncodedNbspStartedAt = i;
|
|
222
|
-
lastEncodedNbspEndedAt = i + 5;
|
|
223
|
-
if (str[i + 5] && str[i + 5].trim()) {
|
|
224
|
-
bumpWordCountAt = i + 5;
|
|
225
|
-
}
|
|
226
|
-
if (!opts.convertEntities) {
|
|
227
|
-
rangesArr.push(i, i + 5, rawnbsp);
|
|
228
|
-
whatWasDone.convertEntities = true;
|
|
229
|
-
} else if (opts.targetLanguage === "html" || opts.targetLanguage === "js") {
|
|
230
|
-
rangesArr.push(i, i + 5, opts.targetLanguage === "html" ? encodedNbspHtml : encodedNbspJs);
|
|
231
|
-
whatWasDone.convertEntities = true;
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
if (!doNothingUntil && str[i] === "\\" && str[i + 1] && str[i + 1].toLowerCase() === "u" && str[i + 2] === "0" && str[i + 3] === "0" && str[i + 4] && str[i + 4].toUpperCase() === "A" && str[i + 5] === "0") {
|
|
235
|
-
lastEncodedNbspStartedAt = i;
|
|
236
|
-
lastEncodedNbspEndedAt = i + 6;
|
|
237
|
-
if (str[i + 6] && str[i + 6].trim()) {
|
|
238
|
-
bumpWordCountAt = i + 6;
|
|
239
|
-
}
|
|
240
|
-
if (!opts.convertEntities) {
|
|
241
|
-
rangesArr.push(i, i + 6, rawnbsp);
|
|
242
|
-
} else if (opts.targetLanguage === "html" || opts.targetLanguage === "css") {
|
|
243
|
-
rangesArr.push(i, i + 6, opts.targetLanguage === "html" ? encodedNbspHtml : encodedNbspCss);
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
if (!doNothingUntil && str[i] === rawnbsp) {
|
|
247
|
-
lastEncodedNbspStartedAt = i;
|
|
248
|
-
lastEncodedNbspEndedAt = i + 1;
|
|
249
|
-
if (opts.convertEntities) {
|
|
250
|
-
rangesArr.push(i, i + 1, opts.targetLanguage === "css" ? encodedNbspCss : opts.targetLanguage === "js" ? encodedNbspJs : encodedNbspHtml);
|
|
251
|
-
}
|
|
252
266
|
}
|
|
253
|
-
|
|
254
|
-
|
|
267
|
+
let ceil;
|
|
268
|
+
if (opts.reportProgressFunc) {
|
|
269
|
+
ceil = Math.floor(opts.reportProgressFuncTo -
|
|
270
|
+
(opts.reportProgressFuncTo - opts.reportProgressFuncFrom) *
|
|
271
|
+
leavePercForLastStage -
|
|
272
|
+
opts.reportProgressFuncFrom);
|
|
255
273
|
}
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
if (lastWhitespaceStartedAt !== undefined && lastWhitespaceEndedAt !== undefined && lastEncodedNbspStartedAt !== undefined && lastEncodedNbspEndedAt !== undefined) {
|
|
261
|
-
if (lastWhitespaceStartedAt > lastEncodedNbspStartedAt) {
|
|
262
|
-
finalStart = lastWhitespaceStartedAt;
|
|
263
|
-
finalEnd = lastWhitespaceEndedAt;
|
|
264
|
-
} else {
|
|
265
|
-
finalStart = lastEncodedNbspStartedAt;
|
|
266
|
-
finalEnd = lastEncodedNbspEndedAt;
|
|
267
|
-
}
|
|
268
|
-
} else if (lastWhitespaceStartedAt !== undefined && lastWhitespaceEndedAt !== undefined) {
|
|
269
|
-
finalStart = lastWhitespaceStartedAt;
|
|
270
|
-
finalEnd = lastWhitespaceEndedAt;
|
|
271
|
-
} else if (lastEncodedNbspStartedAt !== undefined && lastEncodedNbspEndedAt !== undefined) {
|
|
272
|
-
finalStart = lastEncodedNbspStartedAt;
|
|
273
|
-
finalEnd = lastEncodedNbspEndedAt;
|
|
274
|
+
function push(finalStart, finalEnd) {
|
|
275
|
+
let finalWhatToInsert = rawnbsp;
|
|
276
|
+
if (opts.removeWidowPreventionMeasures) {
|
|
277
|
+
finalWhatToInsert = " ";
|
|
274
278
|
}
|
|
275
|
-
if (
|
|
276
|
-
|
|
277
|
-
|
|
279
|
+
else if (opts.convertEntities) {
|
|
280
|
+
finalWhatToInsert = encodedNbspHtml;
|
|
281
|
+
if (typeof opts.targetLanguage === "string") {
|
|
282
|
+
if (opts.targetLanguage.trim().toLowerCase() === "css") {
|
|
283
|
+
finalWhatToInsert = encodedNbspCss;
|
|
284
|
+
}
|
|
285
|
+
else if (opts.targetLanguage.trim().toLowerCase() === "js") {
|
|
286
|
+
finalWhatToInsert = encodedNbspJs;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
278
289
|
}
|
|
279
|
-
if (finalStart
|
|
280
|
-
|
|
281
|
-
whatWasDone.removeWidows = true;
|
|
290
|
+
if (str.slice(finalStart, finalEnd) !== finalWhatToInsert) {
|
|
291
|
+
rangesArr.push(finalStart, finalEnd, finalWhatToInsert);
|
|
282
292
|
}
|
|
283
|
-
}
|
|
284
|
-
resetAll();
|
|
285
|
-
}
|
|
286
|
-
if (opts.UKPostcodes && str[i] && !str[i].trim() && str[i - 1] && str[i - 1].trim() && postcodeRegexFront.test(str.slice(0, i)) && str[right(str, i)] && postcodeRegexEnd.test(str.slice(right(str, i)))) {
|
|
287
|
-
push(i, right(str, i));
|
|
288
|
-
whatWasDone.removeWidows = true;
|
|
289
293
|
}
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
294
|
+
function resetAll() {
|
|
295
|
+
wordCount = 0;
|
|
296
|
+
charCount = 0;
|
|
297
|
+
secondToLastWhitespaceStartedAt = undefined;
|
|
298
|
+
secondToLastWhitespaceEndedAt = undefined;
|
|
299
|
+
lastWhitespaceStartedAt = undefined;
|
|
300
|
+
lastWhitespaceEndedAt = undefined;
|
|
296
301
|
lastEncodedNbspStartedAt = undefined;
|
|
297
302
|
lastEncodedNbspEndedAt = undefined;
|
|
298
|
-
}
|
|
299
303
|
}
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
return true;
|
|
308
|
-
}
|
|
309
|
-
}))) {
|
|
310
|
-
doNothingUntil = undefined;
|
|
311
|
-
i += tempTailFinding.length;
|
|
312
|
-
if (isArr(opts.ignore) && opts.ignore.length && str[i + 1]) {
|
|
313
|
-
opts.ignore.some(oneOfHeadsTailsObjs => {
|
|
314
|
-
return matchRightIncl(str, i, oneOfHeadsTailsObjs.tails, {
|
|
315
|
-
trimBeforeMatching: true,
|
|
316
|
-
cb: (_char, _theRemainderOfTheString, index) => {
|
|
317
|
-
if (index) {
|
|
318
|
-
i = index - 1;
|
|
319
|
-
if (str[i + 1] && str[i + 1].trim()) {
|
|
304
|
+
resetAll();
|
|
305
|
+
for (let i = 0; i <= len; i++) {
|
|
306
|
+
if (!doNothingUntil && isArr(opts.ignore) && opts.ignore.length) {
|
|
307
|
+
opts.ignore.some((valObj, y) => {
|
|
308
|
+
if ((isArr(valObj.heads) &&
|
|
309
|
+
valObj.heads.some((oneOfHeads) => str.startsWith(oneOfHeads, i))) ||
|
|
310
|
+
(typeof valObj.heads === "string" && str.startsWith(valObj.heads, i))) {
|
|
320
311
|
wordCount += 1;
|
|
321
|
-
|
|
312
|
+
doNothingUntil = opts.ignore[y].tails;
|
|
313
|
+
return true;
|
|
322
314
|
}
|
|
323
|
-
return true;
|
|
324
|
-
}
|
|
325
315
|
});
|
|
326
|
-
});
|
|
327
316
|
}
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
317
|
+
if (!doNothingUntil && bumpWordCountAt && bumpWordCountAt === i) {
|
|
318
|
+
wordCount += 1;
|
|
319
|
+
bumpWordCountAt = undefined;
|
|
320
|
+
}
|
|
321
|
+
if (typeof opts.reportProgressFunc === "function") {
|
|
322
|
+
currentPercentageDone =
|
|
323
|
+
opts.reportProgressFuncFrom + Math.floor((i / len) * (ceil || 1));
|
|
324
|
+
if (currentPercentageDone !== lastPercentage) {
|
|
325
|
+
lastPercentage = currentPercentageDone;
|
|
326
|
+
opts.reportProgressFunc(currentPercentageDone);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
if (!doNothingUntil &&
|
|
330
|
+
i &&
|
|
331
|
+
str[i] &&
|
|
332
|
+
str[i].trim() &&
|
|
333
|
+
(!str[i - 1] || (str[i - 1] && !str[i - 1].trim()))) {
|
|
334
|
+
lastWhitespaceEndedAt = i;
|
|
335
|
+
}
|
|
336
|
+
if (!doNothingUntil && str[i] && str[i].trim()) {
|
|
337
|
+
charCount += 1;
|
|
338
|
+
}
|
|
339
|
+
if (!doNothingUntil &&
|
|
340
|
+
opts.hyphens &&
|
|
341
|
+
(`-${rawMdash}${rawNdash}`.includes(str[i]) ||
|
|
342
|
+
str.startsWith(encodedNdashHtml, i) ||
|
|
343
|
+
str.startsWith(encodedNdashCss, i) ||
|
|
344
|
+
str.startsWith(encodedNdashJs, i) ||
|
|
345
|
+
str.startsWith(encodedMdashHtml, i) ||
|
|
346
|
+
str.startsWith(encodedMdashCss, i) ||
|
|
347
|
+
str.startsWith(encodedMdashJs, i)) &&
|
|
348
|
+
str[i + 1] &&
|
|
349
|
+
(!str[i + 1].trim() || str[i] === "&")) {
|
|
350
|
+
if (str[i - 1] && !str[i - 1].trim() && str[left(str, i)]) {
|
|
351
|
+
push(left(str, i) + 1, i);
|
|
352
|
+
whatWasDone.removeWidows = true;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
if (!doNothingUntil &&
|
|
356
|
+
(str.startsWith(" ", i) || str.startsWith(" ", i))) {
|
|
357
|
+
lastEncodedNbspStartedAt = i;
|
|
358
|
+
lastEncodedNbspEndedAt = i + 6;
|
|
359
|
+
if (str[i + 6] && str[i + 6].trim()) {
|
|
360
|
+
bumpWordCountAt = i + 6;
|
|
361
|
+
}
|
|
362
|
+
if (!opts.convertEntities) {
|
|
363
|
+
rangesArr.push(i, i + 6, rawnbsp);
|
|
364
|
+
whatWasDone.convertEntities = true;
|
|
365
|
+
}
|
|
366
|
+
else if (opts.targetLanguage === "css" ||
|
|
367
|
+
opts.targetLanguage === "js") {
|
|
368
|
+
rangesArr.push(i, i + 6, opts.targetLanguage === "css" ? encodedNbspCss : encodedNbspJs);
|
|
369
|
+
whatWasDone.convertEntities = true;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
if (!doNothingUntil &&
|
|
373
|
+
str[i + 4] &&
|
|
374
|
+
str[i] === "\\" &&
|
|
375
|
+
str[i + 1] === "0" &&
|
|
376
|
+
str[i + 2] === "0" &&
|
|
377
|
+
str[i + 3].toUpperCase() === "A" &&
|
|
378
|
+
str[i + 4] === "0") {
|
|
379
|
+
lastEncodedNbspStartedAt = i;
|
|
380
|
+
lastEncodedNbspEndedAt = i + 5;
|
|
381
|
+
if (str[i + 5] && str[i + 5].trim()) {
|
|
382
|
+
bumpWordCountAt = i + 5;
|
|
383
|
+
}
|
|
384
|
+
if (!opts.convertEntities) {
|
|
385
|
+
rangesArr.push(i, i + 5, rawnbsp);
|
|
386
|
+
whatWasDone.convertEntities = true;
|
|
387
|
+
}
|
|
388
|
+
else if (opts.targetLanguage === "html" ||
|
|
389
|
+
opts.targetLanguage === "js") {
|
|
390
|
+
rangesArr.push(i, i + 5, opts.targetLanguage === "html" ? encodedNbspHtml : encodedNbspJs);
|
|
391
|
+
whatWasDone.convertEntities = true;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
if (!doNothingUntil &&
|
|
395
|
+
str[i] === "\\" &&
|
|
396
|
+
str[i + 1] &&
|
|
397
|
+
str[i + 1].toLowerCase() === "u" &&
|
|
398
|
+
str[i + 2] === "0" &&
|
|
399
|
+
str[i + 3] === "0" &&
|
|
400
|
+
str[i + 4] &&
|
|
401
|
+
str[i + 4].toUpperCase() === "A" &&
|
|
402
|
+
str[i + 5] === "0") {
|
|
403
|
+
lastEncodedNbspStartedAt = i;
|
|
404
|
+
lastEncodedNbspEndedAt = i + 6;
|
|
405
|
+
if (str[i + 6] && str[i + 6].trim()) {
|
|
406
|
+
bumpWordCountAt = i + 6;
|
|
407
|
+
}
|
|
408
|
+
if (!opts.convertEntities) {
|
|
409
|
+
rangesArr.push(i, i + 6, rawnbsp);
|
|
410
|
+
}
|
|
411
|
+
else if (opts.targetLanguage === "html" ||
|
|
412
|
+
opts.targetLanguage === "css") {
|
|
413
|
+
rangesArr.push(i, i + 6, opts.targetLanguage === "html" ? encodedNbspHtml : encodedNbspCss);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
if (!doNothingUntil && str[i] === rawnbsp) {
|
|
417
|
+
lastEncodedNbspStartedAt = i;
|
|
418
|
+
lastEncodedNbspEndedAt = i + 1;
|
|
419
|
+
if (opts.convertEntities) {
|
|
420
|
+
rangesArr.push(i, i + 1, opts.targetLanguage === "css"
|
|
421
|
+
? encodedNbspCss
|
|
422
|
+
: opts.targetLanguage === "js"
|
|
423
|
+
? encodedNbspJs
|
|
424
|
+
: encodedNbspHtml);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
if (!doNothingUntil &&
|
|
428
|
+
str[i] &&
|
|
429
|
+
str[i].trim() &&
|
|
430
|
+
(!str[i - 1] || !str[i - 1].trim())) {
|
|
431
|
+
wordCount += 1;
|
|
432
|
+
}
|
|
433
|
+
if (!doNothingUntil &&
|
|
434
|
+
(!str[i] ||
|
|
435
|
+
`\r\n`.includes(str[i]) ||
|
|
436
|
+
((str[i] === "\n" ||
|
|
437
|
+
str[i] === "\r" ||
|
|
438
|
+
(str[i] === "\r" && str[i + 1] === "\n")) &&
|
|
439
|
+
left(str, i) &&
|
|
440
|
+
punctuationCharsToConsiderWidowIssue.includes(str[left(str, i)])))) {
|
|
441
|
+
if ((!opts.minWordCount || wordCount >= opts.minWordCount) &&
|
|
442
|
+
(!opts.minCharCount || charCount >= opts.minCharCount)) {
|
|
443
|
+
let finalStart;
|
|
444
|
+
let finalEnd;
|
|
445
|
+
if (lastWhitespaceStartedAt !== undefined &&
|
|
446
|
+
lastWhitespaceEndedAt !== undefined &&
|
|
447
|
+
lastEncodedNbspStartedAt !== undefined &&
|
|
448
|
+
lastEncodedNbspEndedAt !== undefined) {
|
|
449
|
+
if (lastWhitespaceStartedAt > lastEncodedNbspStartedAt) {
|
|
450
|
+
finalStart = lastWhitespaceStartedAt;
|
|
451
|
+
finalEnd = lastWhitespaceEndedAt;
|
|
452
|
+
}
|
|
453
|
+
else {
|
|
454
|
+
finalStart = lastEncodedNbspStartedAt;
|
|
455
|
+
finalEnd = lastEncodedNbspEndedAt;
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
else if (lastWhitespaceStartedAt !== undefined &&
|
|
459
|
+
lastWhitespaceEndedAt !== undefined) {
|
|
460
|
+
finalStart = lastWhitespaceStartedAt;
|
|
461
|
+
finalEnd = lastWhitespaceEndedAt;
|
|
462
|
+
}
|
|
463
|
+
else if (lastEncodedNbspStartedAt !== undefined &&
|
|
464
|
+
lastEncodedNbspEndedAt !== undefined) {
|
|
465
|
+
finalStart = lastEncodedNbspStartedAt;
|
|
466
|
+
finalEnd = lastEncodedNbspEndedAt;
|
|
467
|
+
}
|
|
468
|
+
if (!(finalStart && finalEnd) &&
|
|
469
|
+
secondToLastWhitespaceStartedAt &&
|
|
470
|
+
secondToLastWhitespaceEndedAt) {
|
|
471
|
+
finalStart = secondToLastWhitespaceStartedAt;
|
|
472
|
+
finalEnd = secondToLastWhitespaceEndedAt;
|
|
473
|
+
}
|
|
474
|
+
if (finalStart && finalEnd) {
|
|
475
|
+
push(finalStart, finalEnd);
|
|
476
|
+
whatWasDone.removeWidows = true;
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
resetAll();
|
|
480
|
+
}
|
|
481
|
+
if (opts.UKPostcodes &&
|
|
482
|
+
str[i] &&
|
|
483
|
+
!str[i].trim() &&
|
|
484
|
+
str[i - 1] &&
|
|
485
|
+
str[i - 1].trim() &&
|
|
486
|
+
postcodeRegexFront.test(str.slice(0, i)) &&
|
|
487
|
+
str[right(str, i)] &&
|
|
488
|
+
postcodeRegexEnd.test(str.slice(right(str, i)))) {
|
|
489
|
+
push(i, right(str, i));
|
|
490
|
+
whatWasDone.removeWidows = true;
|
|
491
|
+
}
|
|
492
|
+
if (!doNothingUntil &&
|
|
493
|
+
str[i] &&
|
|
494
|
+
!str[i].trim() &&
|
|
495
|
+
str[i - 1] &&
|
|
496
|
+
str[i - 1].trim() &&
|
|
497
|
+
(lastWhitespaceStartedAt === undefined ||
|
|
498
|
+
(str[lastWhitespaceStartedAt - 1] &&
|
|
499
|
+
str[lastWhitespaceStartedAt - 1].trim())) &&
|
|
500
|
+
!"/>".includes(str[right(str, i)]) &&
|
|
501
|
+
!str.slice(0, i).trim().endsWith("br") &&
|
|
502
|
+
!str.slice(0, i).trim().endsWith("hr") &&
|
|
503
|
+
!(str.slice(0, i).endsWith("<") &&
|
|
504
|
+
knownHTMLTags.some((tag) => str.startsWith(tag, right(str, i))))) {
|
|
505
|
+
secondToLastWhitespaceStartedAt = lastWhitespaceStartedAt;
|
|
506
|
+
secondToLastWhitespaceEndedAt = lastWhitespaceEndedAt;
|
|
507
|
+
lastWhitespaceStartedAt = i;
|
|
508
|
+
lastWhitespaceEndedAt = undefined;
|
|
509
|
+
if (lastEncodedNbspStartedAt !== undefined ||
|
|
510
|
+
lastEncodedNbspEndedAt !== undefined) {
|
|
511
|
+
lastEncodedNbspStartedAt = undefined;
|
|
512
|
+
lastEncodedNbspEndedAt = undefined;
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
let tempTailFinding;
|
|
516
|
+
if (doNothingUntil) {
|
|
517
|
+
if (typeof doNothingUntil === "string" &&
|
|
518
|
+
(!doNothingUntil.length || str.startsWith(doNothingUntil, i))) {
|
|
519
|
+
doNothingUntil = undefined;
|
|
520
|
+
}
|
|
521
|
+
else if (isArr(doNothingUntil) &&
|
|
522
|
+
(!doNothingUntil.length ||
|
|
523
|
+
doNothingUntil.some((val) => {
|
|
524
|
+
if (str.startsWith(val, i)) {
|
|
525
|
+
tempTailFinding = val;
|
|
526
|
+
return true;
|
|
527
|
+
}
|
|
528
|
+
}))) {
|
|
529
|
+
doNothingUntil = undefined;
|
|
530
|
+
i += tempTailFinding.length;
|
|
531
|
+
if (isArr(opts.ignore) && opts.ignore.length && str[i + 1]) {
|
|
532
|
+
opts.ignore.some((oneOfHeadsTailsObjs) => {
|
|
533
|
+
return matchRightIncl(str, i, oneOfHeadsTailsObjs.tails, {
|
|
534
|
+
trimBeforeMatching: true,
|
|
535
|
+
cb: (_char, _theRemainderOfTheString, index) => {
|
|
536
|
+
if (index) {
|
|
537
|
+
i = index - 1;
|
|
538
|
+
if (str[i + 1] && str[i + 1].trim()) {
|
|
539
|
+
wordCount += 1;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
return true;
|
|
543
|
+
},
|
|
544
|
+
});
|
|
545
|
+
});
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
if (str[i] && `\r\n`.includes(str[i])) {
|
|
550
|
+
wordCount = 0;
|
|
551
|
+
charCount = 0;
|
|
552
|
+
}
|
|
553
|
+
if (isArr(opts.tagRanges) &&
|
|
554
|
+
opts.tagRanges.length &&
|
|
555
|
+
opts.tagRanges.some((rangeArr) => {
|
|
556
|
+
if (i >= rangeArr[0] && i <= rangeArr[1] && rangeArr[1] - 1 > i) {
|
|
557
|
+
i = rangeArr[1] - 1;
|
|
558
|
+
return true;
|
|
559
|
+
}
|
|
560
|
+
})) ;
|
|
333
561
|
}
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
i
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
562
|
+
rApply(str, rangesArr.current())
|
|
563
|
+
.split("")
|
|
564
|
+
.forEach((key, i) => {
|
|
565
|
+
});
|
|
566
|
+
return {
|
|
567
|
+
res: rApply(str, rangesArr.current(), opts.reportProgressFunc
|
|
568
|
+
? (incomingPerc) => {
|
|
569
|
+
currentPercentageDone = Math.floor((opts.reportProgressFuncTo - opts.reportProgressFuncFrom) *
|
|
570
|
+
(1 - leavePercForLastStage) +
|
|
571
|
+
(incomingPerc / 100) *
|
|
572
|
+
(opts.reportProgressFuncTo - opts.reportProgressFuncFrom) *
|
|
573
|
+
leavePercForLastStage);
|
|
574
|
+
if (currentPercentageDone !== lastPercentage) {
|
|
575
|
+
lastPercentage = currentPercentageDone;
|
|
576
|
+
opts.reportProgressFunc(currentPercentageDone);
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
: undefined),
|
|
580
|
+
ranges: rangesArr.current(),
|
|
581
|
+
log: {
|
|
582
|
+
timeTakenInMilliseconds: Date.now() - start,
|
|
583
|
+
},
|
|
584
|
+
whatWasDone,
|
|
585
|
+
};
|
|
357
586
|
}
|
|
358
587
|
|
|
359
588
|
export { defaults, removeWidows, version };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @name string-remove-widows
|
|
3
3
|
* @fileoverview Helps to prevent widow words in a text
|
|
4
|
-
* @version 3.0.
|
|
4
|
+
* @version 3.0.6
|
|
5
5
|
* @author Roy Revelt, Codsen Ltd
|
|
6
6
|
* @license MIT
|
|
7
7
|
* {@link https://codsen.com/os/string-remove-widows/}
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
/**
|
|
12
12
|
* @name arrayiffy-if-string
|
|
13
13
|
* @fileoverview Put non-empty strings into arrays, turn empty-ones into empty arrays. Bypass everything else.
|
|
14
|
-
* @version 4.0.
|
|
14
|
+
* @version 4.0.6
|
|
15
15
|
* @author Roy Revelt, Codsen Ltd
|
|
16
16
|
* @license MIT
|
|
17
17
|
* {@link https://codsen.com/os/arrayiffy-if-string/}
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
/**
|
|
20
20
|
* @name string-match-left-right
|
|
21
21
|
* @fileoverview Match substrings on the left or right of a given index, ignoring whitespace
|
|
22
|
-
* @version 8.0.
|
|
22
|
+
* @version 8.0.6
|
|
23
23
|
* @author Roy Revelt, Codsen Ltd
|
|
24
24
|
* @license MIT
|
|
25
25
|
* {@link https://codsen.com/os/string-match-left-right/}
|
|
@@ -28,7 +28,7 @@ function e(t){return t&&"object"==typeof t&&!Array.isArray(t)}function r(t){retu
|
|
|
28
28
|
/**
|
|
29
29
|
* @name string-left-right
|
|
30
30
|
* @fileoverview Looks up the first non-whitespace character to the left/right of a given index
|
|
31
|
-
* @version 5.0.
|
|
31
|
+
* @version 5.0.6
|
|
32
32
|
* @author Roy Revelt, Codsen Ltd
|
|
33
33
|
* @license MIT
|
|
34
34
|
* {@link https://codsen.com/os/string-left-right/}
|
|
@@ -37,7 +37,7 @@ const l=" ";function c(t,e=0){return function({str:t,idx:e=0,stopAtNewlines:r=!
|
|
|
37
37
|
/**
|
|
38
38
|
* @name string-collapse-leading-whitespace
|
|
39
39
|
* @fileoverview Collapse the leading and trailing whitespace of a string
|
|
40
|
-
* @version 6.0.
|
|
40
|
+
* @version 6.0.6
|
|
41
41
|
* @author Roy Revelt, Codsen Ltd
|
|
42
42
|
* @license MIT
|
|
43
43
|
* {@link https://codsen.com/os/string-collapse-leading-whitespace/}
|
|
@@ -45,7 +45,7 @@ const l=" ";function c(t,e=0){return function({str:t,idx:e=0,stopAtNewlines:r=!
|
|
|
45
45
|
/**
|
|
46
46
|
* @name ranges-push
|
|
47
47
|
* @fileoverview Gather string index ranges
|
|
48
|
-
* @version 6.0.
|
|
48
|
+
* @version 6.0.6
|
|
49
49
|
* @author Roy Revelt, Codsen Ltd
|
|
50
50
|
* @license MIT
|
|
51
51
|
* {@link https://codsen.com/os/ranges-push/}
|
|
@@ -53,7 +53,7 @@ const l=" ";function c(t,e=0){return function({str:t,idx:e=0,stopAtNewlines:r=!
|
|
|
53
53
|
/**
|
|
54
54
|
* @name ranges-sort
|
|
55
55
|
* @fileoverview Sort string index ranges
|
|
56
|
-
* @version 5.0.
|
|
56
|
+
* @version 5.0.6
|
|
57
57
|
* @author Roy Revelt, Codsen Ltd
|
|
58
58
|
* @license MIT
|
|
59
59
|
* {@link https://codsen.com/os/ranges-sort/}
|
|
@@ -61,7 +61,7 @@ const l=" ";function c(t,e=0){return function({str:t,idx:e=0,stopAtNewlines:r=!
|
|
|
61
61
|
/**
|
|
62
62
|
* @name ranges-merge
|
|
63
63
|
* @fileoverview Merge and sort string index ranges
|
|
64
|
-
* @version 8.0.
|
|
64
|
+
* @version 8.0.6
|
|
65
65
|
* @author Roy Revelt, Codsen Ltd
|
|
66
66
|
* @license MIT
|
|
67
67
|
* {@link https://codsen.com/os/ranges-merge/}
|
|
@@ -69,8 +69,8 @@ const l=" ";function c(t,e=0){return function({str:t,idx:e=0,stopAtNewlines:r=!
|
|
|
69
69
|
/**
|
|
70
70
|
* @name ranges-apply
|
|
71
71
|
* @fileoverview Take an array of string index ranges, delete/replace the string according to them
|
|
72
|
-
* @version 6.0.
|
|
72
|
+
* @version 6.0.6
|
|
73
73
|
* @author Roy Revelt, Codsen Ltd
|
|
74
74
|
* @license MIT
|
|
75
75
|
* {@link https://codsen.com/os/ranges-apply/}
|
|
76
|
-
*/function j(t,e,r){let n,o=0,i=0;if(0===arguments.length)throw new Error("ranges-apply: [THROW_ID_01] inputs missing!");if("string"!=typeof t)throw new TypeError(`ranges-apply: [THROW_ID_02] first input argument must be a string! Currently it's: ${typeof t}, equal to: ${JSON.stringify(t,null,4)}`);if(e&&!Array.isArray(e))throw new TypeError(`ranges-apply: [THROW_ID_03] second input argument must be an array (or null)! Currently it's: ${typeof e}, equal to: ${JSON.stringify(e,null,4)}`);if(r&&"function"!=typeof r)throw new TypeError(`ranges-apply: [THROW_ID_04] the third input argument must be a function (or falsey)! Currently it's: ${typeof r}, equal to: ${JSON.stringify(r,null,4)}`);if(!e||!e.filter((t=>t)).length)return t;n=Array.isArray(e)&&Number.isInteger(e[0])&&Number.isInteger(e[1])?[Array.from(e)]:Array.from(e);const s=n.length;let a=0;n.filter((t=>t)).forEach(((t,e)=>{if(r&&(o=Math.floor(a/s*10),o!==i&&(i=o,r(o))),!Array.isArray(t))throw new TypeError(`ranges-apply: [THROW_ID_05] ranges array, second input arg., has ${e}th element not an array: ${JSON.stringify(t,null,4)}, which is ${typeof t}`);if(!Number.isInteger(t[0])){if(!Number.isInteger(+t[0])||+t[0]<0)throw new TypeError(`ranges-apply: [THROW_ID_06] ranges array, second input arg. has ${e}th element, array ${JSON.stringify(t,null,0)}. Its first element is not an integer, string index, but ${typeof t[0]}, equal to: ${JSON.stringify(t[0],null,4)}.`);n[e][0]=+n[e][0]}if(!Number.isInteger(t[1])){if(!Number.isInteger(+t[1])||+t[1]<0)throw new TypeError(`ranges-apply: [THROW_ID_07] ranges array, second input arg. has ${e}th element, array ${JSON.stringify(t,null,0)}. Its second element is not an integer, string index, but ${typeof t[1]}, equal to: ${JSON.stringify(t[1],null,4)}.`);n[e][1]=+n[e][1]}a+=1}));const u=A(n,{progressFn:t=>{r&&(o=10+Math.floor(t/10),o!==i&&(i=o,r(o)))}}),l=Array.isArray(u)?u.length:0;if(l>0){const e=t.slice(u[l-1][1]);t=u.reduce(((e,n,s,a)=>{r&&(o=20+Math.floor(s/l*80),o!==i&&(i=o,r(o)));return e+t.slice(0===s?0:a[s-1][1],a[s][0])+(a[s][2]||"")}),""),t+=e}return t}const $=" ",E=" ",I="\\00A0",M="\\u00A0",R=[{heads:"{{",tails:"}}"},{heads:["{% if","{%- if"],tails:["{% endif","{%- endif"]},{heads:["{% for","{%- for"],tails:["{% endfor","{%- endfor"]},{heads:["{%","{%-"],tails:["%}","-%}"]},{heads:"{#",tails:"#}"}],W=[{heads:"{{",tails:"}}"}],F=[{heads:["<%","<%=","<%-"],tails:["%>","=%>","-%>"]}],C=["abbr","address","area","article","aside","audio","base","bdi","bdo","blockquote","body","br","button","canvas","caption","center","cite","code","col","colgroup","data","datalist","dd","del","details","dfn","dialog","div","dl","doctype","dt","em","embed","fieldset","figcaption","figure","footer","form","h1","h2","h3","h4","h5","h6","head","header","hgroup","hr","html","iframe","img","input","ins","kbd","keygen","label","legend","li","link","main","map","mark","math","menu","menuitem","meta","meter","nav","noscript","object","ol","optgroup","option","output","param","picture","pre","progress","rb","rp","rt","rtc","ruby","samp","script","section","select","slot","small","source","span","strong","style","sub","summary","sup","svg","table","tbody","td","template","textarea","tfoot","th","thead","time","title","tr","track","ul","var","video","wbr","xml"],N={removeWidowPreventionMeasures:!1,convertEntities:!0,targetLanguage:"html",UKPostcodes:!1,hyphens:!0,minWordCount:4,minCharCount:5,ignore:[],reportProgressFunc:null,reportProgressFuncFrom:0,reportProgressFuncTo:100,tagRanges:[]};t.defaults=N,t.removeWidows=function(t,e){const r=Date.now();if("string"!=typeof t)throw void 0===t?new Error("string-remove-widows: [THROW_ID_01] the first input argument is completely missing! It should be given as string."):new Error(`string-remove-widows: [THROW_ID_02] the first input argument must be string! It was given as "${typeof t}", equal to:\n${JSON.stringify(t,null,4)}`);if(e&&"object"!=typeof e)throw new Error(`string-remove-widows: [THROW_ID_03] the second input argument, options object, should be a plain object but it was given as type ${typeof e}, equal to ${JSON.stringify(e,null,4)}`);const n=Array.isArray,o=t.length,i=new v({mergeType:2}),a=["."],u=/[A-Z]{1,2}[0-9][0-9A-Z]?$/,l=/^[0-9][A-Z]{2}/,f=.06;let h,p,y,m,d,b,w,_,T,O=0,A=0,S=0;const L={...N,...e},x={removeWidows:!1,convertEntities:!1};if(!L.ignore||!n(L.ignore)&&"string"!=typeof L.ignore)L.ignore=[];else if("string"==typeof L.ignore&&(L.ignore=[L.ignore]),L.ignore.includes("all"))L.ignore=L.ignore.concat(R.concat(F));else if(L.ignore.some((t=>"string"==typeof t))){let t=[];L.ignore=L.ignore.filter((e=>"string"==typeof e&&e.length?(["nunjucks","jinja","liquid"].includes(e.trim().toLowerCase())?t=t.concat(R):["hugo"].includes(e.trim().toLowerCase())?t=t.concat(W):["hexo"].includes(e.trim().toLowerCase())&&(t=t.concat(F)),!1):"object"==typeof e||void 0)),t.length&&(L.ignore=L.ignore.concat(t))}let D;function B(e,r){let n=$;L.removeWidowPreventionMeasures?n=" ":L.convertEntities&&(n=E,"string"==typeof L.targetLanguage&&("css"===L.targetLanguage.trim().toLowerCase()?n=I:"js"===L.targetLanguage.trim().toLowerCase()&&(n=M))),t.slice(e,r)!==n&&i.push(e,r,n)}function H(){A=0,S=0,p=void 0,y=void 0,m=void 0,d=void 0,b=void 0,w=void 0}L.reportProgressFunc&&(D=Math.floor(L.reportProgressFuncTo-(L.reportProgressFuncTo-L.reportProgressFuncFrom)*f-L.reportProgressFuncFrom)),H();for(let e=0;e<=o;e++){if(!_&&n(L.ignore)&&L.ignore.length&&L.ignore.some(((r,o)=>{if(n(r.heads)&&r.heads.some((r=>t.startsWith(r,e)))||"string"==typeof r.heads&&t.startsWith(r.heads,e))return A+=1,_=L.ignore[o].tails,!0})),!_&&T&&T===e&&(A+=1,T=void 0),"function"==typeof L.reportProgressFunc&&(h=L.reportProgressFuncFrom+Math.floor(e/o*(D||1)),h!==O&&(O=h,L.reportProgressFunc(h))),!_&&e&&t[e]&&t[e].trim()&&(!t[e-1]||t[e-1]&&!t[e-1].trim())&&(d=e),!_&&t[e]&&t[e].trim()&&(S+=1),_||!L.hyphens||!("-—–".includes(t[e])||t.startsWith("–",e)||t.startsWith("\\2013",e)||t.startsWith("\\u2013",e)||t.startsWith("—",e)||t.startsWith("\\2014",e)||t.startsWith("\\u2014",e))||!t[e+1]||t[e+1].trim()&&"&"!==t[e]||t[e-1]&&!t[e-1].trim()&&t[g(t,e)]&&(B(g(t,e)+1,e),x.removeWidows=!0),_||!t.startsWith(" ",e)&&!t.startsWith(" ",e)||(b=e,w=e+6,t[e+6]&&t[e+6].trim()&&(T=e+6),L.convertEntities?"css"!==L.targetLanguage&&"js"!==L.targetLanguage||(i.push(e,e+6,"css"===L.targetLanguage?I:M),x.convertEntities=!0):(i.push(e,e+6,$),x.convertEntities=!0)),!_&&t[e+4]&&"\\"===t[e]&&"0"===t[e+1]&&"0"===t[e+2]&&"A"===t[e+3].toUpperCase()&&"0"===t[e+4]&&(b=e,w=e+5,t[e+5]&&t[e+5].trim()&&(T=e+5),L.convertEntities?"html"!==L.targetLanguage&&"js"!==L.targetLanguage||(i.push(e,e+5,"html"===L.targetLanguage?E:M),x.convertEntities=!0):(i.push(e,e+5,$),x.convertEntities=!0)),!_&&"\\"===t[e]&&t[e+1]&&"u"===t[e+1].toLowerCase()&&"0"===t[e+2]&&"0"===t[e+3]&&t[e+4]&&"A"===t[e+4].toUpperCase()&&"0"===t[e+5]&&(b=e,w=e+6,t[e+6]&&t[e+6].trim()&&(T=e+6),L.convertEntities?"html"!==L.targetLanguage&&"css"!==L.targetLanguage||i.push(e,e+6,"html"===L.targetLanguage?E:I):i.push(e,e+6,$)),_||t[e]!==$||(b=e,w=e+1,L.convertEntities&&i.push(e,e+1,"css"===L.targetLanguage?I:"js"===L.targetLanguage?M:E)),_||!t[e]||!t[e].trim()||t[e-1]&&t[e-1].trim()||(A+=1),!_&&(!t[e]||"\r\n".includes(t[e])||("\n"===t[e]||"\r"===t[e]||"\r"===t[e]&&"\n"===t[e+1])&&g(t,e)&&a.includes(t[g(t,e)]))){if((!L.minWordCount||A>=L.minWordCount)&&(!L.minCharCount||S>=L.minCharCount)){let t,e;void 0!==m&&void 0!==d&&void 0!==b&&void 0!==w?m>b?(t=m,e=d):(t=b,e=w):void 0!==m&&void 0!==d?(t=m,e=d):void 0!==b&&void 0!==w&&(t=b,e=w),t&&e||!p||!y||(t=p,e=y),t&&e&&(B(t,e),x.removeWidows=!0)}H()}let r;L.UKPostcodes&&t[e]&&!t[e].trim()&&t[e-1]&&t[e-1].trim()&&u.test(t.slice(0,e))&&t[c(t,e)]&&l.test(t.slice(c(t,e)))&&(B(e,c(t,e)),x.removeWidows=!0),_||!t[e]||t[e].trim()||!t[e-1]||!t[e-1].trim()||!(void 0===m||t[m-1]&&t[m-1].trim())||"/>".includes(t[c(t,e)])||t.slice(0,e).trim().endsWith("br")||t.slice(0,e).trim().endsWith("hr")||t.slice(0,e).endsWith("<")&&C.some((r=>t.startsWith(r,c(t,e))))||(p=m,y=d,m=e,d=void 0,void 0===b&&void 0===w||(b=void 0,w=void 0)),_&&("string"!=typeof _||_.length&&!t.startsWith(_,e)?!n(_)||_.length&&!_.some((n=>{if(t.startsWith(n,e))return r=n,!0}))||(_=void 0,e+=r.length,n(L.ignore)&&L.ignore.length&&t[e+1]&&L.ignore.some((r=>s(t,e,r.tails,{trimBeforeMatching:!0,cb:(r,n,o)=>(o&&(e=o-1,t[e+1]&&t[e+1].trim()&&(A+=1)),!0)})))):_=void 0),t[e]&&"\r\n".includes(t[e])&&(A=0,S=0),n(L.tagRanges)&&L.tagRanges.length&&L.tagRanges.some((t=>{if(e>=t[0]&&e<=t[1]&&t[1]-1>e)return e=t[1]-1,!0}))}return j(t,i.current()).split("").forEach(((t,e)=>{})),{res:j(t,i.current(),L.reportProgressFunc?t=>{h=Math.floor(.94*(L.reportProgressFuncTo-L.reportProgressFuncFrom)+t/100*(L.reportProgressFuncTo-L.reportProgressFuncFrom)*f),h!==O&&(O=h,L.reportProgressFunc(h))}:void 0),ranges:i.current(),log:{timeTakenInMilliseconds:Date.now()-r},whatWasDone:x}},t.version="3.0.
|
|
76
|
+
*/function j(t,e,r){let n,o=0,i=0;if(0===arguments.length)throw new Error("ranges-apply: [THROW_ID_01] inputs missing!");if("string"!=typeof t)throw new TypeError(`ranges-apply: [THROW_ID_02] first input argument must be a string! Currently it's: ${typeof t}, equal to: ${JSON.stringify(t,null,4)}`);if(e&&!Array.isArray(e))throw new TypeError(`ranges-apply: [THROW_ID_03] second input argument must be an array (or null)! Currently it's: ${typeof e}, equal to: ${JSON.stringify(e,null,4)}`);if(r&&"function"!=typeof r)throw new TypeError(`ranges-apply: [THROW_ID_04] the third input argument must be a function (or falsey)! Currently it's: ${typeof r}, equal to: ${JSON.stringify(r,null,4)}`);if(!e||!e.filter((t=>t)).length)return t;n=Array.isArray(e)&&Number.isInteger(e[0])&&Number.isInteger(e[1])?[Array.from(e)]:Array.from(e);const s=n.length;let a=0;n.filter((t=>t)).forEach(((t,e)=>{if(r&&(o=Math.floor(a/s*10),o!==i&&(i=o,r(o))),!Array.isArray(t))throw new TypeError(`ranges-apply: [THROW_ID_05] ranges array, second input arg., has ${e}th element not an array: ${JSON.stringify(t,null,4)}, which is ${typeof t}`);if(!Number.isInteger(t[0])){if(!Number.isInteger(+t[0])||+t[0]<0)throw new TypeError(`ranges-apply: [THROW_ID_06] ranges array, second input arg. has ${e}th element, array ${JSON.stringify(t,null,0)}. Its first element is not an integer, string index, but ${typeof t[0]}, equal to: ${JSON.stringify(t[0],null,4)}.`);n[e][0]=+n[e][0]}if(!Number.isInteger(t[1])){if(!Number.isInteger(+t[1])||+t[1]<0)throw new TypeError(`ranges-apply: [THROW_ID_07] ranges array, second input arg. has ${e}th element, array ${JSON.stringify(t,null,0)}. Its second element is not an integer, string index, but ${typeof t[1]}, equal to: ${JSON.stringify(t[1],null,4)}.`);n[e][1]=+n[e][1]}a+=1}));const u=A(n,{progressFn:t=>{r&&(o=10+Math.floor(t/10),o!==i&&(i=o,r(o)))}}),l=Array.isArray(u)?u.length:0;if(l>0){const e=t.slice(u[l-1][1]);t=u.reduce(((e,n,s,a)=>{r&&(o=20+Math.floor(s/l*80),o!==i&&(i=o,r(o)));return e+t.slice(0===s?0:a[s-1][1],a[s][0])+(a[s][2]||"")}),""),t+=e}return t}const $=" ",E=" ",I="\\00A0",M="\\u00A0",R=[{heads:"{{",tails:"}}"},{heads:["{% if","{%- if"],tails:["{% endif","{%- endif"]},{heads:["{% for","{%- for"],tails:["{% endfor","{%- endfor"]},{heads:["{%","{%-"],tails:["%}","-%}"]},{heads:"{#",tails:"#}"}],W=[{heads:"{{",tails:"}}"}],F=[{heads:["<%","<%=","<%-"],tails:["%>","=%>","-%>"]}],C=["abbr","address","area","article","aside","audio","base","bdi","bdo","blockquote","body","br","button","canvas","caption","center","cite","code","col","colgroup","data","datalist","dd","del","details","dfn","dialog","div","dl","doctype","dt","em","embed","fieldset","figcaption","figure","footer","form","h1","h2","h3","h4","h5","h6","head","header","hgroup","hr","html","iframe","img","input","ins","kbd","keygen","label","legend","li","link","main","map","mark","math","menu","menuitem","meta","meter","nav","noscript","object","ol","optgroup","option","output","param","picture","pre","progress","rb","rp","rt","rtc","ruby","samp","script","section","select","slot","small","source","span","strong","style","sub","summary","sup","svg","table","tbody","td","template","textarea","tfoot","th","thead","time","title","tr","track","ul","var","video","wbr","xml"],N={removeWidowPreventionMeasures:!1,convertEntities:!0,targetLanguage:"html",UKPostcodes:!1,hyphens:!0,minWordCount:4,minCharCount:5,ignore:[],reportProgressFunc:null,reportProgressFuncFrom:0,reportProgressFuncTo:100,tagRanges:[]};t.defaults=N,t.removeWidows=function(t,e){const r=Date.now();if("string"!=typeof t)throw void 0===t?new Error("string-remove-widows: [THROW_ID_01] the first input argument is completely missing! It should be given as string."):new Error(`string-remove-widows: [THROW_ID_02] the first input argument must be string! It was given as "${typeof t}", equal to:\n${JSON.stringify(t,null,4)}`);if(e&&"object"!=typeof e)throw new Error(`string-remove-widows: [THROW_ID_03] the second input argument, options object, should be a plain object but it was given as type ${typeof e}, equal to ${JSON.stringify(e,null,4)}`);const n=Array.isArray,o=t.length,i=new v({mergeType:2}),a=["."],u=/[A-Z]{1,2}[0-9][0-9A-Z]?$/,l=/^[0-9][A-Z]{2}/,f=.06;let h,p,y,m,d,b,w,_,T,O=0,A=0,S=0;const L={...N,...e},x={removeWidows:!1,convertEntities:!1};if(!L.ignore||!n(L.ignore)&&"string"!=typeof L.ignore)L.ignore=[];else if("string"==typeof L.ignore&&(L.ignore=[L.ignore]),L.ignore.includes("all"))L.ignore=L.ignore.concat(R.concat(F));else if(L.ignore.some((t=>"string"==typeof t))){let t=[];L.ignore=L.ignore.filter((e=>"string"==typeof e&&e.length?(["nunjucks","jinja","liquid"].includes(e.trim().toLowerCase())?t=t.concat(R):["hugo"].includes(e.trim().toLowerCase())?t=t.concat(W):["hexo"].includes(e.trim().toLowerCase())&&(t=t.concat(F)),!1):"object"==typeof e||void 0)),t.length&&(L.ignore=L.ignore.concat(t))}let D;function B(e,r){let n=$;L.removeWidowPreventionMeasures?n=" ":L.convertEntities&&(n=E,"string"==typeof L.targetLanguage&&("css"===L.targetLanguage.trim().toLowerCase()?n=I:"js"===L.targetLanguage.trim().toLowerCase()&&(n=M))),t.slice(e,r)!==n&&i.push(e,r,n)}function H(){A=0,S=0,p=void 0,y=void 0,m=void 0,d=void 0,b=void 0,w=void 0}L.reportProgressFunc&&(D=Math.floor(L.reportProgressFuncTo-(L.reportProgressFuncTo-L.reportProgressFuncFrom)*f-L.reportProgressFuncFrom)),H();for(let e=0;e<=o;e++){if(!_&&n(L.ignore)&&L.ignore.length&&L.ignore.some(((r,o)=>{if(n(r.heads)&&r.heads.some((r=>t.startsWith(r,e)))||"string"==typeof r.heads&&t.startsWith(r.heads,e))return A+=1,_=L.ignore[o].tails,!0})),!_&&T&&T===e&&(A+=1,T=void 0),"function"==typeof L.reportProgressFunc&&(h=L.reportProgressFuncFrom+Math.floor(e/o*(D||1)),h!==O&&(O=h,L.reportProgressFunc(h))),!_&&e&&t[e]&&t[e].trim()&&(!t[e-1]||t[e-1]&&!t[e-1].trim())&&(d=e),!_&&t[e]&&t[e].trim()&&(S+=1),_||!L.hyphens||!("-—–".includes(t[e])||t.startsWith("–",e)||t.startsWith("\\2013",e)||t.startsWith("\\u2013",e)||t.startsWith("—",e)||t.startsWith("\\2014",e)||t.startsWith("\\u2014",e))||!t[e+1]||t[e+1].trim()&&"&"!==t[e]||t[e-1]&&!t[e-1].trim()&&t[g(t,e)]&&(B(g(t,e)+1,e),x.removeWidows=!0),_||!t.startsWith(" ",e)&&!t.startsWith(" ",e)||(b=e,w=e+6,t[e+6]&&t[e+6].trim()&&(T=e+6),L.convertEntities?"css"!==L.targetLanguage&&"js"!==L.targetLanguage||(i.push(e,e+6,"css"===L.targetLanguage?I:M),x.convertEntities=!0):(i.push(e,e+6,$),x.convertEntities=!0)),!_&&t[e+4]&&"\\"===t[e]&&"0"===t[e+1]&&"0"===t[e+2]&&"A"===t[e+3].toUpperCase()&&"0"===t[e+4]&&(b=e,w=e+5,t[e+5]&&t[e+5].trim()&&(T=e+5),L.convertEntities?"html"!==L.targetLanguage&&"js"!==L.targetLanguage||(i.push(e,e+5,"html"===L.targetLanguage?E:M),x.convertEntities=!0):(i.push(e,e+5,$),x.convertEntities=!0)),!_&&"\\"===t[e]&&t[e+1]&&"u"===t[e+1].toLowerCase()&&"0"===t[e+2]&&"0"===t[e+3]&&t[e+4]&&"A"===t[e+4].toUpperCase()&&"0"===t[e+5]&&(b=e,w=e+6,t[e+6]&&t[e+6].trim()&&(T=e+6),L.convertEntities?"html"!==L.targetLanguage&&"css"!==L.targetLanguage||i.push(e,e+6,"html"===L.targetLanguage?E:I):i.push(e,e+6,$)),_||t[e]!==$||(b=e,w=e+1,L.convertEntities&&i.push(e,e+1,"css"===L.targetLanguage?I:"js"===L.targetLanguage?M:E)),_||!t[e]||!t[e].trim()||t[e-1]&&t[e-1].trim()||(A+=1),!_&&(!t[e]||"\r\n".includes(t[e])||("\n"===t[e]||"\r"===t[e]||"\r"===t[e]&&"\n"===t[e+1])&&g(t,e)&&a.includes(t[g(t,e)]))){if((!L.minWordCount||A>=L.minWordCount)&&(!L.minCharCount||S>=L.minCharCount)){let t,e;void 0!==m&&void 0!==d&&void 0!==b&&void 0!==w?m>b?(t=m,e=d):(t=b,e=w):void 0!==m&&void 0!==d?(t=m,e=d):void 0!==b&&void 0!==w&&(t=b,e=w),t&&e||!p||!y||(t=p,e=y),t&&e&&(B(t,e),x.removeWidows=!0)}H()}let r;L.UKPostcodes&&t[e]&&!t[e].trim()&&t[e-1]&&t[e-1].trim()&&u.test(t.slice(0,e))&&t[c(t,e)]&&l.test(t.slice(c(t,e)))&&(B(e,c(t,e)),x.removeWidows=!0),_||!t[e]||t[e].trim()||!t[e-1]||!t[e-1].trim()||!(void 0===m||t[m-1]&&t[m-1].trim())||"/>".includes(t[c(t,e)])||t.slice(0,e).trim().endsWith("br")||t.slice(0,e).trim().endsWith("hr")||t.slice(0,e).endsWith("<")&&C.some((r=>t.startsWith(r,c(t,e))))||(p=m,y=d,m=e,d=void 0,void 0===b&&void 0===w||(b=void 0,w=void 0)),_&&("string"!=typeof _||_.length&&!t.startsWith(_,e)?!n(_)||_.length&&!_.some((n=>{if(t.startsWith(n,e))return r=n,!0}))||(_=void 0,e+=r.length,n(L.ignore)&&L.ignore.length&&t[e+1]&&L.ignore.some((r=>s(t,e,r.tails,{trimBeforeMatching:!0,cb:(r,n,o)=>(o&&(e=o-1,t[e+1]&&t[e+1].trim()&&(A+=1)),!0)})))):_=void 0),t[e]&&"\r\n".includes(t[e])&&(A=0,S=0),n(L.tagRanges)&&L.tagRanges.length&&L.tagRanges.some((t=>{if(e>=t[0]&&e<=t[1]&&t[1]-1>e)return e=t[1]-1,!0}))}return j(t,i.current()).split("").forEach(((t,e)=>{})),{res:j(t,i.current(),L.reportProgressFunc?t=>{h=Math.floor(.94*(L.reportProgressFuncTo-L.reportProgressFuncFrom)+t/100*(L.reportProgressFuncTo-L.reportProgressFuncFrom)*f),h!==O&&(O=h,L.reportProgressFunc(h))}:void 0),ranges:i.current(),log:{timeTakenInMilliseconds:Date.now()-r},whatWasDone:x}},t.version="3.0.6",Object.defineProperty(t,"__esModule",{value:!0})}));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "string-remove-widows",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.6",
|
|
4
4
|
"description": "Helps to prevent widow words in a text",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"against",
|
|
@@ -39,13 +39,12 @@
|
|
|
39
39
|
"types": "types/index.d.ts",
|
|
40
40
|
"scripts": {
|
|
41
41
|
"build": "rollup -c",
|
|
42
|
-
"
|
|
43
|
-
"
|
|
42
|
+
"build:esbuild": "node '../../scripts/esbuild.js'",
|
|
43
|
+
"build:esbuild:dev": "cross-env MODE=dev node '../../scripts/esbuild.js'",
|
|
44
|
+
"ci_test": "npm run build && npm run format && tap --no-only --reporter=silent",
|
|
44
45
|
"clean_types": "../../scripts/cleanTypes.js",
|
|
45
46
|
"dev": "rollup -c --dev",
|
|
46
47
|
"devunittest": "npm run dev && tap --only -R 'base'",
|
|
47
|
-
"esbuild": "node '../../scripts/esbuild.js'",
|
|
48
|
-
"esbuild_dev": "cross-env MODE=dev node '../../scripts/esbuild.js'",
|
|
49
48
|
"format": "npm run lect && npm run prettier && npm run lint",
|
|
50
49
|
"lect": "lect",
|
|
51
50
|
"lint": "../../node_modules/eslint/bin/eslint.js . --ext .js --ext .ts --fix --config \"../../.eslintrc.json\" --quiet",
|
|
@@ -54,17 +53,14 @@
|
|
|
54
53
|
"republish": "npm publish || :",
|
|
55
54
|
"tap": "tap",
|
|
56
55
|
"pretest": "npm run build",
|
|
57
|
-
"test": "npm run
|
|
56
|
+
"test": "npm run test:ci && npm run perf",
|
|
57
|
+
"test:ci": "npm run unittest && npm run test:examples && npm run format",
|
|
58
58
|
"test:examples": "../../scripts/test-examples.js && npm run lect && npm run prettier",
|
|
59
59
|
"tsc": "tsc",
|
|
60
|
-
"unittest": "tap --no-only --
|
|
60
|
+
"unittest": "tap --no-only --reporter=terse && tsc -p tsconfig.json --noEmit"
|
|
61
61
|
},
|
|
62
62
|
"tap": {
|
|
63
63
|
"check-coverage": false,
|
|
64
|
-
"coverage-report": [
|
|
65
|
-
"json-summary",
|
|
66
|
-
"text"
|
|
67
|
-
],
|
|
68
64
|
"node-arg": [
|
|
69
65
|
"--no-warnings",
|
|
70
66
|
"--experimental-loader",
|
|
@@ -84,11 +80,11 @@
|
|
|
84
80
|
}
|
|
85
81
|
},
|
|
86
82
|
"dependencies": {
|
|
87
|
-
"@babel/runtime": "^7.16.
|
|
88
|
-
"ranges-apply": "^6.0.
|
|
89
|
-
"ranges-push": "^6.0.
|
|
90
|
-
"string-left-right": "^5.0.
|
|
91
|
-
"string-match-left-right": "^8.0.
|
|
83
|
+
"@babel/runtime": "^7.16.3",
|
|
84
|
+
"ranges-apply": "^6.0.6",
|
|
85
|
+
"ranges-push": "^6.0.6",
|
|
86
|
+
"string-left-right": "^5.0.6",
|
|
87
|
+
"string-match-left-right": "^8.0.6"
|
|
92
88
|
},
|
|
93
89
|
"devDependencies": {
|
|
94
90
|
"@babel/cli": "^7.16.0",
|
|
@@ -99,8 +95,8 @@
|
|
|
99
95
|
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.16.0",
|
|
100
96
|
"@babel/plugin-proposal-object-rest-spread": "^7.16.0",
|
|
101
97
|
"@babel/plugin-proposal-optional-chaining": "^7.16.0",
|
|
102
|
-
"@babel/plugin-transform-runtime": "^7.16.
|
|
103
|
-
"@babel/preset-env": "^7.16.
|
|
98
|
+
"@babel/plugin-transform-runtime": "^7.16.4",
|
|
99
|
+
"@babel/preset-env": "^7.16.4",
|
|
104
100
|
"@babel/preset-typescript": "^7.16.0",
|
|
105
101
|
"@babel/register": "^7.16.0",
|
|
106
102
|
"@istanbuljs/esm-loader-hook": "^0.1.2",
|
|
@@ -110,25 +106,25 @@
|
|
|
110
106
|
"@rollup/plugin-node-resolve": "^13.0.6",
|
|
111
107
|
"@rollup/plugin-strip": "^2.1.0",
|
|
112
108
|
"@rollup/plugin-typescript": "^8.3.0",
|
|
113
|
-
"@types/node": "^16.11.
|
|
109
|
+
"@types/node": "^16.11.9",
|
|
114
110
|
"@types/tap": "^15.0.5",
|
|
115
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
116
|
-
"@typescript-eslint/parser": "^5.
|
|
111
|
+
"@typescript-eslint/eslint-plugin": "^5.4.0",
|
|
112
|
+
"@typescript-eslint/parser": "^5.4.0",
|
|
117
113
|
"core-js": "^3.19.1",
|
|
118
114
|
"cross-env": "^7.0.3",
|
|
119
|
-
"eslint": "^8.
|
|
120
|
-
"lect": "^0.18.
|
|
121
|
-
"rollup": "^2.
|
|
115
|
+
"eslint": "^8.3.0",
|
|
116
|
+
"lect": "^0.18.6",
|
|
117
|
+
"rollup": "^2.60.0",
|
|
122
118
|
"rollup-plugin-ascii": "^0.0.3",
|
|
123
119
|
"rollup-plugin-banner": "^0.2.1",
|
|
124
120
|
"rollup-plugin-cleanup": "^3.2.1",
|
|
125
121
|
"rollup-plugin-dts": "^4.0.1",
|
|
126
122
|
"rollup-plugin-terser": "^7.0.2",
|
|
127
|
-
"tap": "^15.
|
|
123
|
+
"tap": "^15.1.2",
|
|
128
124
|
"tslib": "^2.3.1",
|
|
129
|
-
"typescript": "^4.
|
|
125
|
+
"typescript": "^4.5.2"
|
|
130
126
|
},
|
|
131
127
|
"engines": {
|
|
132
|
-
"node": ">=
|
|
128
|
+
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
|
|
133
129
|
}
|
|
134
130
|
}
|