1 | 'use strict';
|
2 |
|
3 | const isEqual = require('lodash/isEqual');
|
4 | const unique = require('lodash/uniqWith');
|
5 |
|
6 | const COLUMN = ' ';
|
7 |
|
8 | const RE_COMMENT_SINGLE_LINE = /^\s*(?:\/\/|#).+$/gm;
|
9 |
|
10 | const RE_COMMENT_MULTI_LINES = /((?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/))$/gm;
|
11 | const RE_LINE_BEGIN = /^/gm;
|
12 | const RE_SOURCE_MAPPING_URL = /\n?\/\/# sourceMappingURL=[^\s]+/;
|
13 | const SEG_LENGTH = 30;
|
14 |
|
15 | module.exports = {
|
16 | commentStrip,
|
17 | sourceMapCommentStrip,
|
18 | commentWrap,
|
19 | indent,
|
20 | uniqueMatch,
|
21 | regexpEscape,
|
22 | truncate,
|
23 | getLocationFromIndex
|
24 | };
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | function commentStrip(string) {
|
32 |
|
33 | string = string.replace(RE_COMMENT_SINGLE_LINE, '');
|
34 | string = string.replace(RE_COMMENT_MULTI_LINES, '');
|
35 | return string;
|
36 | }
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | function sourceMapCommentStrip(string) {
|
44 | return string.replace(RE_SOURCE_MAPPING_URL, '');
|
45 | }
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | function commentWrap(string, type) {
|
54 | let open, close;
|
55 |
|
56 | if (type == 'html') {
|
57 | open = '<!-- ';
|
58 | close = ' -->';
|
59 | } else {
|
60 | open = '/* ';
|
61 | close = ' */';
|
62 | }
|
63 |
|
64 | return open + string + close;
|
65 | }
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | function indent(string, column) {
|
74 | const spaces = new Array(++column).join(COLUMN);
|
75 |
|
76 | return string.replace(RE_LINE_BEGIN, spaces);
|
77 | }
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | function uniqueMatch(string, regexp) {
|
86 | const results = [];
|
87 | let match;
|
88 |
|
89 | while ((match = regexp.exec(string))) {
|
90 | results.push({
|
91 | context: match[0],
|
92 | match: match[1] || ''
|
93 | });
|
94 | }
|
95 |
|
96 |
|
97 | return unique(results, isEqual);
|
98 | }
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | function regexpEscape(string) {
|
106 | return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
|
107 | }
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 | function truncate(string) {
|
115 | if (string.length > SEG_LENGTH * 2 + 3) {
|
116 | return string.slice(0, SEG_LENGTH) + '...' + string.slice(-SEG_LENGTH);
|
117 | }
|
118 |
|
119 | return string;
|
120 | }
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 | function getLocationFromIndex(string, index) {
|
130 | const lines = string.split('\n');
|
131 | let destructure = false;
|
132 | let idx = 0;
|
133 | const results = [];
|
134 | let found = [];
|
135 |
|
136 |
|
137 | if (!Array.isArray(index)) {
|
138 | index = [index];
|
139 | destructure = true;
|
140 | }
|
141 |
|
142 | for (let i = 0, l = lines.length; i < l; i++) {
|
143 | const m = index.length;
|
144 | const line = lines[i];
|
145 |
|
146 | const lineLength = line.length + 1;
|
147 |
|
148 |
|
149 | if (!m) break;
|
150 |
|
151 |
|
152 | for (let j = 0; j < m; j++) {
|
153 |
|
154 | if (idx + lineLength > index[j]) {
|
155 | results.push({
|
156 | line: i + 1,
|
157 | column: index[j] - idx
|
158 | });
|
159 |
|
160 | found.push(j);
|
161 | }
|
162 | }
|
163 |
|
164 | if (found.length) {
|
165 | for (let k = 0, n = found.length; k < n; k++) {
|
166 |
|
167 | index.splice(found[k] - k, 1);
|
168 | }
|
169 | found = [];
|
170 | }
|
171 | idx += lineLength;
|
172 | }
|
173 |
|
174 | if (results.length) return destructure ? results[0] : results;
|
175 | return { line: 0, column: 0 };
|
176 | }
|