-
Notifications
You must be signed in to change notification settings - Fork 1
/
filterCommentInLines.js
45 lines (37 loc) · 1.07 KB
/
filterCommentInLines.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
'use strict';
/**
* This function filters inline comments after escaped hashtag
*
* @function filterCommentInLines
*
* @param {(string|array|object)} value
* @returns {(string|array|object)} filtered **value**
*/
const filterCommentInLines = (value) => {
if (Array.isArray(value)) {
value.forEach((val, index) => {
value[index] = filterCommentInLines(val);
});
return value;
}
if (typeof value === 'object') {
const keys = Object.keys(value);
keys.forEach((key) => {
value[key] = filterCommentInLines(value[key]);
});
return value;
}
if (!value.includes('#')) {
return value;
}
const match = value.matchAll(/\\?\s?#\s?/g);
const arrayFromMatch = [...match];
const filteredMatch = arrayFromMatch.filter((val) => {
return !val[0].includes('\\');
});
if (filteredMatch.length > 0) {
value = value.slice(0, filteredMatch[0].index).trim();
}
return value.replaceAll('\\#', '#');
};
module.exports = { filterCommentInLines };