1 | 'use strict';
|
2 | var __importDefault =
|
3 | (this && this.__importDefault) ||
|
4 | function (mod) {
|
5 | return mod && mod.__esModule ? mod : { default: mod };
|
6 | };
|
7 | Object.defineProperty(exports, '__esModule', { value: true });
|
8 | exports.mergeRanges = exports.isEOLAtPosition = exports.getOccurrencesCount = exports.detectEOL = exports.getFirstRegexMatch = exports.getCommonPathPrefix = exports.formatPercent = exports.formatBytes = exports.getFileContent = void 0;
|
9 | const fs_1 = __importDefault(require('fs'));
|
10 | function getFileContent(file) {
|
11 | const buffer = Buffer.isBuffer(file) ? file : fs_1.default.readFileSync(file);
|
12 | return buffer.toString();
|
13 | }
|
14 | exports.getFileContent = getFileContent;
|
15 | const BYTE_SIZES = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
16 | const SIZE_BASE = 1024;
|
17 | function formatBytes(bytes, decimals = 2) {
|
18 | if (bytes === 0) return `0 ${BYTE_SIZES[0]}`;
|
19 | const exponent = Math.floor(Math.log(bytes) / Math.log(SIZE_BASE));
|
20 | const value = bytes / Math.pow(SIZE_BASE, exponent);
|
21 | return `${parseFloat(value.toFixed(decimals))} ${BYTE_SIZES[exponent]}`;
|
22 | }
|
23 | exports.formatBytes = formatBytes;
|
24 | function formatPercent(value, total, fractionDigits) {
|
25 | return ((100.0 * value) / total).toFixed(fractionDigits);
|
26 | }
|
27 | exports.formatPercent = formatPercent;
|
28 | const PATH_SEPARATOR_REGEX = /(\/)/;
|
29 | function getCommonPathPrefix(paths) {
|
30 | if (paths.length < 2) return '';
|
31 | const A = paths.concat().sort();
|
32 | const a1 = A[0].split(PATH_SEPARATOR_REGEX);
|
33 | const a2 = A[A.length - 1].split(PATH_SEPARATOR_REGEX);
|
34 | const L = a1.length;
|
35 | let i = 0;
|
36 | while (i < L && a1[i] === a2[i]) i++;
|
37 | return a1.slice(0, i).join('');
|
38 | }
|
39 | exports.getCommonPathPrefix = getCommonPathPrefix;
|
40 | function getFirstRegexMatch(regex, string) {
|
41 | const match = string.match(regex);
|
42 | return match ? match[0] : null;
|
43 | }
|
44 | exports.getFirstRegexMatch = getFirstRegexMatch;
|
45 | const LF = '\n';
|
46 | const CR_LF = '\r\n';
|
47 | function detectEOL(content) {
|
48 | return content.includes(CR_LF) ? CR_LF : LF;
|
49 | }
|
50 | exports.detectEOL = detectEOL;
|
51 | function getOccurrencesCount(subString, string) {
|
52 | let count = 0;
|
53 | let position = string.indexOf(subString);
|
54 | const subStringLength = subString.length;
|
55 | while (position !== -1) {
|
56 | count += 1;
|
57 | position = string.indexOf(subString, position + subStringLength);
|
58 | }
|
59 | return count;
|
60 | }
|
61 | exports.getOccurrencesCount = getOccurrencesCount;
|
62 | function isEOLAtPosition(string, [line, column]) {
|
63 | const eol = detectEOL(string);
|
64 | const eolLength = eol.length;
|
65 | let lineOffset = 0;
|
66 | for (let lineIndex = 1; lineIndex < line; lineIndex += 1) {
|
67 | lineOffset = string.indexOf(eol, lineOffset);
|
68 | if (lineOffset === -1) {
|
69 | return false;
|
70 | }
|
71 | lineOffset += eolLength;
|
72 | }
|
73 | return string.substr(lineOffset + column, eolLength) === eol;
|
74 | }
|
75 | exports.isEOLAtPosition = isEOLAtPosition;
|
76 | function mergeRanges(ranges) {
|
77 | const mergedRanges = [];
|
78 | const rangesCount = ranges.length;
|
79 | if (rangesCount === 1) {
|
80 | return ranges;
|
81 | }
|
82 | let { start, end, source } = ranges[0];
|
83 | for (let i = 1; i < rangesCount; i += 1) {
|
84 | const isSourceMatch = ranges[i].source === ranges[i - 1].source;
|
85 | const isConsecutive = ranges[i].start - ranges[i - 1].end === 1;
|
86 | if (isSourceMatch && isConsecutive) {
|
87 | end = ranges[i].end;
|
88 | } else {
|
89 | mergedRanges.push({ start, end, source });
|
90 | ({ start, end, source } = ranges[i]);
|
91 | }
|
92 | }
|
93 | mergedRanges.push({ start, end, source });
|
94 | return mergedRanges;
|
95 | }
|
96 | exports.mergeRanges = mergeRanges;
|
97 |
|