1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
|
/**
* @name ranges-push
* @fileoverview Gather string index ranges
* @version 5.1.0
* @author Roy Revelt, Codsen Ltd
* @license MIT
* {@link https://codsen.com/os/ranges-push/}
*/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.rangesPush = {}));
}(this, (function (exports) { 'use strict';
/**
* @name string-collapse-leading-whitespace
* @fileoverview Collapse the leading and trailing whitespace of a string
* @version 5.1.0
* @author Roy Revelt, Codsen Ltd
* @license MIT
* {@link https://codsen.com/os/string-collapse-leading-whitespace/}
*/
function collWhitespace(str, originallineBreakLimit = 1) {
const rawNbsp = "\u00A0";
function reverse(s) {
return Array.from(s).reverse().join("");
}
function prep(whitespaceChunk, limit, trailing) {
const firstBreakChar = trailing ? "\n" : "\r";
const secondBreakChar = trailing ? "\r" : "\n";
if (!whitespaceChunk) {
return whitespaceChunk;
}
let crlfCount = 0;
let res = "";
for (let i = 0, len = whitespaceChunk.length; i < len; i++) {
if (whitespaceChunk[i] === firstBreakChar || whitespaceChunk[i] === secondBreakChar && whitespaceChunk[i - 1] !== firstBreakChar) {
crlfCount++;
}
if (`\r\n`.includes(whitespaceChunk[i]) || whitespaceChunk[i] === rawNbsp) {
if (whitespaceChunk[i] === rawNbsp) {
res += whitespaceChunk[i];
} else if (whitespaceChunk[i] === firstBreakChar) {
if (crlfCount <= limit) {
res += whitespaceChunk[i];
if (whitespaceChunk[i + 1] === secondBreakChar) {
res += whitespaceChunk[i + 1];
i++;
}
}
} else if (whitespaceChunk[i] === secondBreakChar && (!whitespaceChunk[i - 1] || whitespaceChunk[i - 1] !== firstBreakChar) && crlfCount <= limit) {
res += whitespaceChunk[i];
}
} else {
if (!whitespaceChunk[i + 1] && !crlfCount) {
res += " ";
}
}
}
return res;
}
if (typeof str === "string" && str.length) {
let lineBreakLimit = 1;
if (typeof +originallineBreakLimit === "number" && Number.isInteger(+originallineBreakLimit) && +originallineBreakLimit >= 0) {
lineBreakLimit = +originallineBreakLimit;
}
let frontPart = "";
let endPart = "";
if (!str.trim()) {
frontPart = str;
} else if (!str[0].trim()) {
for (let i = 0, len = str.length; i < len; i++) {
if (str[i].trim()) {
frontPart = str.slice(0, i);
break;
}
}
}
if (str.trim() && (str.slice(-1).trim() === "" || str.slice(-1) === rawNbsp)) {
for (let i = str.length; i--;) {
if (str[i].trim()) {
endPart = str.slice(i + 1);
break;
}
}
}
return `${prep(frontPart, lineBreakLimit, false)}${str.trim()}${reverse(prep(reverse(endPart), lineBreakLimit, true))}`;
}
return str;
}
/**
* @name ranges-sort
* @fileoverview Sort string index ranges
* @version 4.1.0
* @author Roy Revelt, Codsen Ltd
* @license MIT
* {@link https://codsen.com/os/ranges-sort/}
*/
const defaults$2 = {
strictlyTwoElementsInRangeArrays: false,
progressFn: null
};
function rSort(arrOfRanges, originalOptions) {
if (!Array.isArray(arrOfRanges) || !arrOfRanges.length) {
return arrOfRanges;
}
const opts = { ...defaults$2,
...originalOptions
};
let culpritsIndex;
let culpritsLen;
if (opts.strictlyTwoElementsInRangeArrays && !arrOfRanges.filter(range => range).every((rangeArr, indx) => {
if (rangeArr.length !== 2) {
culpritsIndex = indx;
culpritsLen = rangeArr.length;
return false;
}
return true;
})) {
throw new TypeError(`ranges-sort: [THROW_ID_03] The first argument should be an array and must consist of arrays which are natural number indexes representing TWO string index ranges. However, ${culpritsIndex}th range (${JSON.stringify(arrOfRanges[culpritsIndex], null, 4)}) has not two but ${culpritsLen} elements!`);
}
if (!arrOfRanges.filter(range => range).every((rangeArr, indx) => {
if (!Number.isInteger(rangeArr[0]) || rangeArr[0] < 0 || !Number.isInteger(rangeArr[1]) || rangeArr[1] < 0) {
culpritsIndex = indx;
return false;
}
return true;
})) {
throw new TypeError(`ranges-sort: [THROW_ID_04] The first argument should be an array and must consist of arrays which are natural number indexes representing string index ranges. However, ${culpritsIndex}th range (${JSON.stringify(arrOfRanges[culpritsIndex], null, 4)}) does not consist of only natural numbers!`);
}
const maxPossibleIterations = arrOfRanges.filter(range => range).length ** 2;
let counter = 0;
return Array.from(arrOfRanges).filter(range => range).sort((range1, range2) => {
if (opts.progressFn) {
counter += 1;
opts.progressFn(Math.floor(counter * 100 / maxPossibleIterations));
}
if (range1[0] === range2[0]) {
if (range1[1] < range2[1]) {
return -1;
}
if (range1[1] > range2[1]) {
return 1;
}
return 0;
}
if (range1[0] < range2[0]) {
return -1;
}
return 1;
});
}
/**
* @name ranges-merge
* @fileoverview Merge and sort string index ranges
* @version 7.1.0
* @author Roy Revelt, Codsen Ltd
* @license MIT
* {@link https://codsen.com/os/ranges-merge/}
*/
const defaults$1 = {
mergeType: 1,
progressFn: null,
joinRangesThatTouchEdges: true
};
function rMerge(arrOfRanges, originalOpts) {
function isObj(something) {
return something && typeof something === "object" && !Array.isArray(something);
}
if (!Array.isArray(arrOfRanges) || !arrOfRanges.length) {
return null;
}
let opts;
if (originalOpts) {
if (isObj(originalOpts)) {
opts = { ...defaults$1,
...originalOpts
};
if (opts.progressFn && isObj(opts.progressFn) && !Object.keys(opts.progressFn).length) {
opts.progressFn = null;
} else if (opts.progressFn && typeof opts.progressFn !== "function") {
throw new Error(`ranges-merge: [THROW_ID_01] opts.progressFn must be a function! It was given of a type: "${typeof opts.progressFn}", equal to ${JSON.stringify(opts.progressFn, null, 4)}`);
}
if (opts.mergeType && +opts.mergeType !== 1 && +opts.mergeType !== 2) {
throw new Error(`ranges-merge: [THROW_ID_02] opts.mergeType was customised to a wrong thing! It was given of a type: "${typeof opts.mergeType}", equal to ${JSON.stringify(opts.mergeType, null, 4)}`);
}
if (typeof opts.joinRangesThatTouchEdges !== "boolean") {
throw new Error(`ranges-merge: [THROW_ID_04] opts.joinRangesThatTouchEdges was customised to a wrong thing! It was given of a type: "${typeof opts.joinRangesThatTouchEdges}", equal to ${JSON.stringify(opts.joinRangesThatTouchEdges, null, 4)}`);
}
} else {
throw new Error(`emlint: [THROW_ID_03] the second input argument must be a plain object. It was given as:\n${JSON.stringify(originalOpts, null, 4)} (type ${typeof originalOpts})`);
}
} else {
opts = { ...defaults$1
};
}
const filtered = arrOfRanges
.filter(range => range).map(subarr => [...subarr]).filter(
rangeArr => rangeArr[2] !== undefined || rangeArr[0] !== rangeArr[1]);
let sortedRanges;
let lastPercentageDone;
let percentageDone;
if (opts.progressFn) {
sortedRanges = rSort(filtered, {
progressFn: percentage => {
percentageDone = Math.floor(percentage / 5);
if (percentageDone !== lastPercentageDone) {
lastPercentageDone = percentageDone;
opts.progressFn(percentageDone);
}
}
});
} else {
sortedRanges = rSort(filtered);
}
if (!sortedRanges) {
return null;
}
const len = sortedRanges.length - 1;
for (let i = len; i > 0; i--) {
if (opts.progressFn) {
percentageDone = Math.floor((1 - i / len) * 78) + 21;
if (percentageDone !== lastPercentageDone && percentageDone > lastPercentageDone) {
lastPercentageDone = percentageDone;
opts.progressFn(percentageDone);
}
}
if (sortedRanges[i][0] <= sortedRanges[i - 1][0] || !opts.joinRangesThatTouchEdges && sortedRanges[i][0] < sortedRanges[i - 1][1] || opts.joinRangesThatTouchEdges && sortedRanges[i][0] <= sortedRanges[i - 1][1]) {
sortedRanges[i - 1][0] = Math.min(sortedRanges[i][0], sortedRanges[i - 1][0]);
sortedRanges[i - 1][1] = Math.max(sortedRanges[i][1], sortedRanges[i - 1][1]);
if (sortedRanges[i][2] !== undefined && (sortedRanges[i - 1][0] >= sortedRanges[i][0] || sortedRanges[i - 1][1] <= sortedRanges[i][1])) {
if (sortedRanges[i - 1][2] !== null) {
if (sortedRanges[i][2] === null && sortedRanges[i - 1][2] !== null) {
sortedRanges[i - 1][2] = null;
} else if (sortedRanges[i - 1][2] != null) {
if (+opts.mergeType === 2 && sortedRanges[i - 1][0] === sortedRanges[i][0]) {
sortedRanges[i - 1][2] = sortedRanges[i][2];
} else {
sortedRanges[i - 1][2] += sortedRanges[i][2];
}
} else {
sortedRanges[i - 1][2] = sortedRanges[i][2];
}
}
}
sortedRanges.splice(i, 1);
i = sortedRanges.length;
}
}
return sortedRanges.length ? sortedRanges : null;
}
var version$1 = "5.1.0";
/* eslint @typescript-eslint/explicit-module-boundary-types: 0 */
const version = version$1;
function existy(x) {
return x != null;
}
function isNum(something) {
return Number.isInteger(something) && something >= 0;
}
function isStr(something) {
return typeof something === "string";
}
const defaults = {
limitToBeAddedWhitespace: false,
limitLinebreaksCount: 1,
mergeType: 1,
};
// -----------------------------------------------------------------------------
class Ranges {
//
// O P T I O N S
// =============
constructor(originalOpts) {
const opts = { ...defaults, ...originalOpts };
if (opts.mergeType && opts.mergeType !== 1 && opts.mergeType !== 2) {
if (isStr(opts.mergeType) && opts.mergeType.trim() === "1") {
opts.mergeType = 1;
}
else if (isStr(opts.mergeType) &&
opts.mergeType.trim() === "2") {
opts.mergeType = 2;
}
else {
throw new Error(`ranges-push: [THROW_ID_02] opts.mergeType was customised to a wrong thing! It was given of a type: "${typeof opts.mergeType}", equal to ${JSON.stringify(opts.mergeType, null, 4)}`);
}
}
// so it's correct, let's get it in:
this.opts = opts;
this.ranges = [];
}
add(originalFrom, originalTo, addVal) {
if (originalFrom == null && originalTo == null) {
// absent ranges are marked as null - instead of array of arrays we can receive a null
return;
}
if (existy(originalFrom) && !existy(originalTo)) {
if (Array.isArray(originalFrom)) {
if (originalFrom.length) {
if (originalFrom.some((el) => Array.isArray(el))) {
originalFrom.forEach((thing) => {
if (Array.isArray(thing)) {
// recursively feed this subarray, hopefully it's an array
this.add(...thing);
}
// just skip other cases
});
return;
}
if (originalFrom.length &&
isNum(+originalFrom[0]) &&
isNum(+originalFrom[1])) {
// recursively pass in those values
this.add(...originalFrom);
}
}
// else,
return;
}
throw new TypeError(`ranges-push/Ranges/add(): [THROW_ID_12] the first input argument, "from" is set (${JSON.stringify(originalFrom, null, 0)}) but second-one, "to" is not (${JSON.stringify(originalTo, null, 0)})`);
}
else if (!existy(originalFrom) && existy(originalTo)) {
throw new TypeError(`ranges-push/Ranges/add(): [THROW_ID_13] the second input argument, "to" is set (${JSON.stringify(originalTo, null, 0)}) but first-one, "from" is not (${JSON.stringify(originalFrom, null, 0)})`);
}
const from = +originalFrom;
const to = +originalTo;
if (isNum(addVal)) {
// eslint-disable-next-line no-param-reassign
addVal = String(addVal);
}
// validation
if (isNum(from) && isNum(to)) {
// This means two indexes were given as arguments. Business as usual.
if (existy(addVal) && !isStr(addVal) && !isNum(addVal)) {
throw new TypeError(`ranges-push/Ranges/add(): [THROW_ID_08] The third argument, the value to add, was given not as string but ${typeof addVal}, equal to:\n${JSON.stringify(addVal, null, 4)}`);
}
// Does the incoming "from" value match the existing last element's "to" value?
if (existy(this.ranges) &&
Array.isArray(this.last()) &&
from === this.last()[1]) {
// The incoming range is an exact extension of the last range, like
// [1, 100] gets added [100, 200] => you can merge into: [1, 200].
this.last()[1] = to;
// console.log(`addVal = ${JSON.stringify(addVal, null, 4)}`)
if (this.last()[2] === null || addVal === null) ;
if (this.last()[2] !== null && existy(addVal)) {
let calculatedVal = this.last()[2] &&
this.last()[2].length > 0 &&
(!this.opts || !this.opts.mergeType || this.opts.mergeType === 1)
? this.last()[2] + addVal
: addVal;
if (this.opts.limitToBeAddedWhitespace) {
calculatedVal = collWhitespace(calculatedVal, this.opts.limitLinebreaksCount);
}
if (!(isStr(calculatedVal) && !calculatedVal.length)) {
// don't let the zero-length strings past
this.last()[2] = calculatedVal;
}
}
}
else {
if (!this.ranges) {
this.ranges = [];
}
const whatToPush = addVal !== undefined && !(isStr(addVal) && !addVal.length)
? [
from,
to,
addVal && this.opts.limitToBeAddedWhitespace
? collWhitespace(addVal, this.opts.limitLinebreaksCount)
: addVal,
]
: [from, to];
this.ranges.push(whatToPush);
}
}
else {
// Error somewhere!
// Let's find out where.
// is it first arg?
if (!(isNum(from) && from >= 0)) {
throw new TypeError(`ranges-push/Ranges/add(): [THROW_ID_09] "from" value, the first input argument, must be a natural number or zero! Currently it's of a type "${typeof from}" equal to: ${JSON.stringify(from, null, 4)}`);
}
else {
// then it's second...
throw new TypeError(`ranges-push/Ranges/add(): [THROW_ID_10] "to" value, the second input argument, must be a natural number or zero! Currently it's of a type "${typeof to}" equal to: ${JSON.stringify(to, null, 4)}`);
}
}
}
push(originalFrom, originalTo, addVal) {
this.add(originalFrom, originalTo, addVal);
}
// C U R R E N T () - kindof a getter
// ==================================
current() {
if (Array.isArray(this.ranges) && this.ranges.length) {
// beware, merging can return null
this.ranges = rMerge(this.ranges, {
mergeType: this.opts.mergeType,
});
if (this.ranges && this.opts.limitToBeAddedWhitespace) {
return this.ranges.map((val) => {
if (existy(val[2])) {
return [
val[0],
val[1],
collWhitespace(val[2], this.opts.limitLinebreaksCount),
];
}
return val;
});
}
return this.ranges;
}
return null;
}
// W I P E ()
// ==========
wipe() {
this.ranges = [];
}
// R E P L A C E ()
// ==========
replace(givenRanges) {
if (Array.isArray(givenRanges) && givenRanges.length) {
// Now, ranges can be array of arrays, correct format but also single
// range, an array of two natural numbers might be given.
// Let's put safety latch against such cases
if (!(Array.isArray(givenRanges[0]) && isNum(givenRanges[0][0]))) {
throw new Error(`ranges-push/Ranges/replace(): [THROW_ID_11] Single range was given but we expected array of arrays! The first element, ${JSON.stringify(givenRanges[0], null, 4)} should be an array and its first element should be an integer, a string index.`);
}
else {
this.ranges = Array.from(givenRanges);
}
}
else {
this.ranges = [];
}
}
// L A S T ()
// ==========
last() {
if (Array.isArray(this.ranges) && this.ranges.length) {
return this.ranges[this.ranges.length - 1];
}
return null;
}
}
exports.Ranges = Ranges;
exports.defaults = defaults;
exports.version = version;
Object.defineProperty(exports, '__esModule', { value: true });
})));
|