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
|
/**
* @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/}
*/
import { collWhitespace } from 'string-collapse-leading-whitespace';
import { rMerge } from 'ranges-merge';
var version$1 = "5.1.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 {
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)}`);
}
}
this.opts = opts;
this.ranges = [];
}
add(originalFrom, originalTo, addVal) {
if (originalFrom == null && originalTo == 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)) {
this.add(...thing);
}
});
return;
}
if (originalFrom.length && isNum(+originalFrom[0]) && isNum(+originalFrom[1])) {
this.add(...originalFrom);
}
}
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)) {
addVal = String(addVal);
}
if (isNum(from) && isNum(to)) {
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)}`);
}
if (existy(this.ranges) && Array.isArray(this.last()) && from === this.last()[1]) {
this.last()[1] = to;
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)) {
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 {
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 {
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);
}
current() {
if (Array.isArray(this.ranges) && this.ranges.length) {
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;
}
wipe() {
this.ranges = [];
}
replace(givenRanges) {
if (Array.isArray(givenRanges) && givenRanges.length) {
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 = [];
}
}
last() {
if (Array.isArray(this.ranges) && this.ranges.length) {
return this.ranges[this.ranges.length - 1];
}
return null;
}
}
export { Ranges, defaults, version };
|