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
|
/**
* @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/}
*/
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
var _typeof = require('@babel/runtime/helpers/typeof');
var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
var _classCallCheck = require('@babel/runtime/helpers/classCallCheck');
var _createClass = require('@babel/runtime/helpers/createClass');
var stringCollapseLeadingWhitespace = require('string-collapse-leading-whitespace');
var rangesMerge = require('ranges-merge');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var _toConsumableArray__default = /*#__PURE__*/_interopDefaultLegacy(_toConsumableArray);
var _typeof__default = /*#__PURE__*/_interopDefaultLegacy(_typeof);
var _objectSpread__default = /*#__PURE__*/_interopDefaultLegacy(_objectSpread);
var _classCallCheck__default = /*#__PURE__*/_interopDefaultLegacy(_classCallCheck);
var _createClass__default = /*#__PURE__*/_interopDefaultLegacy(_createClass);
var version$1 = "5.1.0";
var 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";
}
var defaults = {
limitToBeAddedWhitespace: false,
limitLinebreaksCount: 1,
mergeType: 1
};
var Ranges = function () {
function Ranges(originalOpts) {
_classCallCheck__default['default'](this, Ranges);
var opts = _objectSpread__default['default'](_objectSpread__default['default']({}, 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: \"".concat(_typeof__default['default'](opts.mergeType), "\", equal to ").concat(JSON.stringify(opts.mergeType, null, 4)));
}
}
this.opts = opts;
this.ranges = [];
}
_createClass__default['default'](Ranges, [{
key: "add",
value: function add(originalFrom, originalTo, addVal) {
var _this = this;
if (originalFrom == null && originalTo == null) {
return;
}
if (existy(originalFrom) && !existy(originalTo)) {
if (Array.isArray(originalFrom)) {
if (originalFrom.length) {
if (originalFrom.some(function (el) {
return Array.isArray(el);
})) {
originalFrom.forEach(function (thing) {
if (Array.isArray(thing)) {
_this.add.apply(_this, _toConsumableArray__default['default'](thing));
}
});
return;
}
if (originalFrom.length && isNum(+originalFrom[0]) && isNum(+originalFrom[1])) {
this.add.apply(this, _toConsumableArray__default['default'](originalFrom));
}
}
return;
}
throw new TypeError("ranges-push/Ranges/add(): [THROW_ID_12] the first input argument, \"from\" is set (".concat(JSON.stringify(originalFrom, null, 0), ") but second-one, \"to\" is not (").concat(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 (".concat(JSON.stringify(originalTo, null, 0), ") but first-one, \"from\" is not (").concat(JSON.stringify(originalFrom, null, 0), ")"));
}
var from = +originalFrom;
var 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 ".concat(_typeof__default['default'](addVal), ", equal to:\n").concat(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)) {
var 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 = stringCollapseLeadingWhitespace.collWhitespace(calculatedVal, this.opts.limitLinebreaksCount);
}
if (!(isStr(calculatedVal) && !calculatedVal.length)) {
this.last()[2] = calculatedVal;
}
}
} else {
if (!this.ranges) {
this.ranges = [];
}
var whatToPush = addVal !== undefined && !(isStr(addVal) && !addVal.length) ? [from, to, addVal && this.opts.limitToBeAddedWhitespace ? stringCollapseLeadingWhitespace.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 \"".concat(_typeof__default['default'](from), "\" equal to: ").concat(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 \"".concat(_typeof__default['default'](to), "\" equal to: ").concat(JSON.stringify(to, null, 4)));
}
}
}
}, {
key: "push",
value: function push(originalFrom, originalTo, addVal) {
this.add(originalFrom, originalTo, addVal);
}
}, {
key: "current",
value: function current() {
var _this2 = this;
if (Array.isArray(this.ranges) && this.ranges.length) {
this.ranges = rangesMerge.rMerge(this.ranges, {
mergeType: this.opts.mergeType
});
if (this.ranges && this.opts.limitToBeAddedWhitespace) {
return this.ranges.map(function (val) {
if (existy(val[2])) {
return [val[0], val[1], stringCollapseLeadingWhitespace.collWhitespace(val[2], _this2.opts.limitLinebreaksCount)];
}
return val;
});
}
return this.ranges;
}
return null;
}
}, {
key: "wipe",
value: function wipe() {
this.ranges = [];
}
}, {
key: "replace",
value: function 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, ".concat(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 = [];
}
}
}, {
key: "last",
value: function last() {
if (Array.isArray(this.ranges) && this.ranges.length) {
return this.ranges[this.ranges.length - 1];
}
return null;
}
}]);
return Ranges;
}();
exports.Ranges = Ranges;
exports.defaults = defaults;
exports.version = version;
|