forked from apiaryio/dredd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblueprint-utils.js
76 lines (64 loc) · 1.95 KB
/
blueprint-utils.js
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
const NEWLINE_RE = /\n/g;
function characterIndexToPosition(charIndex = 0, code = '') {
const codeFragment = code.substring(0, charIndex);
const match = codeFragment.match(NEWLINE_RE) || [];
const row = match.length + 1;
return { row };
}
const sortNumbersAscending = (a, b) => a - b;
function warningLocationToRanges(warningLocation = [], text = '') {
if (!warningLocation.length) {
// No start-end ranges, nothing to return
return [];
}
const rowsIndexes = [];
let position = characterIndexToPosition(warningLocation[0][0], text);
// Add this warning position row into ranges array
rowsIndexes.push(position.row);
if (warningLocation.length > 0) {
// More lines
for (let locKey = 0; locKey < warningLocation.length; locKey++) {
const loc = warningLocation[locKey];
if (locKey > 0) {
position = characterIndexToPosition(loc[0], text);
rowsIndexes.push(position.row);
}
}
}
rowsIndexes.sort(sortNumbersAscending);
const ranges = [];
let range = { start: rowsIndexes[0], end: rowsIndexes[0] };
for (const rowIndex of rowsIndexes) {
if ((rowIndex === range.end) || (rowIndex === (range.end + 1))) { // Moving end of known range
range.end = rowIndex;
} else {
ranges.push(range); // non-continuous range
range = { start: rowIndex, end: rowIndex };
}
}
// Push the last edited range to ranges-array
ranges.push(range);
return ranges;
}
function rangesToLinesText(ranges) {
let pos = '';
const iterable = ranges || [];
for (let rangeIndex = 0; rangeIndex < iterable.length; rangeIndex++) {
const range = iterable[rangeIndex];
if (rangeIndex > 0) {
pos += ', ';
}
if (range.start !== range.end) {
pos += `lines ${range.start}-${range.end}`;
} else {
pos += `line ${range.start}`;
}
}
return pos;
}
module.exports = {
characterIndexToPosition,
rangesToLinesText,
sortNumbersAscending,
warningLocationToRanges
};