-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.js
224 lines (208 loc) · 8.03 KB
/
helper.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
"use strict";
function hAdvancedEnclosersExtract(str) {
const openErs = "[\\(\\[\\{“«‹<]"; //the opening character of the enclosers, you can add more
const closeErs = "[\\)\\]\\}”»›>]"; //the closing ones
/* const regShorFormQuotes = /(?<=\w)’(?=\w)/g //for stuffs like don’t, they’re, o’clock, gov’t...
const passSFQ = regShorFormQuotes.test(str)
let strWithSFQ
if (passSFQ) {
strWithSFQ = str
str = str.replace(regShorFormQuotes, ' ') //replace with space, so as to not disrupt main processes, but will add later.
}*/
if (!hAreEnclosersBalanced(str)) return null;
const enclosersBuild = new RegExp(`${openErs}.+?${closeErs}`, "g");
let ordMatch = str.match(enclosersBuild);
if (ordMatch) {
let proValue = proceed(ordMatch);
// if (passSFQ) proValue = fixshortFormQuotes(strWithSFQ, str, proValue)
return proValue;
} else return null;
function proceed(ordMatch) {
let i = 0;
let recordStability = 0;
const fatherRunner = ordMatch.length;
const ordArrIndex = ordMatch.map((v) => str.indexOf(v));
const result = [];
while (i < fatherRunner) {
let value = ordMatch[i];
if (value === undefined) break; //it will increment out of bound, so we stop loop here.
let openEn = value.match(new RegExp(`^${openErs}`, "g"))[0];
let closeEn = value.match(new RegExp(`${closeErs}$`, "g"))[0];
value = value.slice(0, value.length - 1); //remove the closing character.
const initIndex = str.indexOf(value);
const endIndex = endIndexFind();
const res = str.slice(initIndex, endIndex + 1);
result.push(res); //update the result array
function endIndexFind() {
for (let i = initIndex; i < str.length; i++) {
const currValue = str[i];
if (currValue === openEn) recordStability++;
else if (currValue === closeEn) recordStability--;
if (recordStability === 0) return i;
}
}
for (const v of ordArrIndex) {
if (v > endIndex) {
i = ordArrIndex.indexOf(v) - 1; //update i but reduce by 1, so that we can increment it later.
break;
}
}
i++;
}
return result;
}
}
function fixshortFormQuotes(textOrig, modText, arr) {
const holder = [];
arr.map((value) => {
const modI = modText.indexOf(value);
const modI_Last = modI + value.length;
holder.push(textOrig.slice(modI, modI_Last));
});
return holder;
/*The modified text and the original text have the same length,
just that the short form single quotes are replaced with space.
We put the original text into an array, but based on indexes
provided by the array values to get accurate positions of
the values in the original string.*/
}
function hAreEnclosersBalanced(str) {
/*facing pairs are enclosers put in right other next to each other. eg. [] or ()*/
let len;
const reduceRegExp = /\[\]|\(\)|<>|{}|""|“”|''|‘’|„„|\*\*|«»|‹›|′′|″″/g; // to remove facing pairs
const regShorFormQuotes = /(?<=\w)'(?=\w)|(?<=\w)’(?=\w)/g; //for stuffs like don't, they're, o’clock, gov’t...
if (regShorFormQuotes.test(str)) str = str.replace(regShorFormQuotes, ""); //removes them so as to not disrupt main processes
str = str.replace(/[^\[\]\(\)<>{}'"“”‘’„\*«»‹›`′″]/g, ""); //remove all non parentheses
if (str.length === 0) return true; // no braces balanced
if (str.length % 2) return false; // odd length then not balanced.
do {
// Remove facing pairs until there are no more to remove.
len = str.length;
str = str.replace(reduceRegExp, ""); // remove all facing pairs
} while (len !== str.length);
if (str.length === 0) return true; // Balanced and good. :)
return false; // UnBalanced and bad. :(
}
function hRefine(text) {
if (Array.isArray(text))
return text.map((e) => (e = e.trim())).filter((e) => e !== "");
else if (typeof text === "string") return text.trim();
}
function hParaCore(text) {
const regPara = /\n/gm;
if (regPara.test(text)) {
text = text.split(regPara);
text = hRefine(text);
}
return text;
}
function hPassPCore(text) {
text = hParaCore(text);
if (Array.isArray(text)) return { value: text, status: true };
else return { value: text, status: false };
}
function hAttachEnclosers(text) {
let i = 0;
const openErs = "[\\(\\[\\{“‘«‹<]"; //the opening character of the enclosers, you can add more
const closeErs = "[\\)\\]\\}”’»›>]"; //the closing ones
if (Array.isArray(text)) {
while (i < text.length) {
const currValue = text[i];
const next = hNextValueFn(text, i);
const nextValue = next.value;
const nextIndex = next.index;
let openEnFind = currValue.match(openErs);
if (openEnFind) {
openEnFind = openEnFind.pop(); //pick the last
//console.log(openEnFind, 'fuck you')
if (currValue.lastIndexOf(openEnFind)) {
text[i] = currValue + nextValue;
if (nextIndex) text.splice(nextIndex, 1);
//console.log(text, 'jiop')
//e back to same position in next loop
}
} //console.log('no openers found')
else i++;
}
//Spaces are missing after joining together next array-value. fix it
}
}
function hNextValueFn(t, i) {
return t[i + 1]
? { value: t[i + 1], index: i + 1 }
: { value: "", index: void 0 };
}
function hEscaper(text, lookOut, mode, prevIndexes) {
if (mode === "begin") {
let escaperValue;
if (Array.isArray(text)) {
//input may be array
const jText = text.join(" ");
if (jText.indexOf(lookOut) === -1) return { text: text, status: false };
escaperValue = checkErs(jText);
const valueEscaped = lookOut[0] + escaperValue + lookOut.slice(1);
text = text.map((v) => v.replaceAll(lookOut, (_) => valueEscaped));
} else {
if (text.indexOf(lookOut) === -1) return { text: text, status: false }; //indexof is better, lookOut may have different definition if turned to regex
escaperValue = checkErs(text);
//console.log(lookOut, lookOut[0], "here+")
const valueEscaped = lookOut[0] + escaperValue + lookOut.slice(1);
text = text.replaceAll(lookOut, (_) => valueEscaped);
}
return { text: text, status: true, escaper: escaperValue };
} else if (mode === "end") {
if (Array.isArray(text)) text = text.map((v) => v.replaceAll(lookOut, ""));
else text = text.replaceAll(lookOut, "");
return { text: text };
}
function checkErs(text) {
const randMaxValue = 20;
let escaperValue = randoMan(randMaxValue);
if (text.indexOf(escaperValue) === -1) return escaperValue; //pattern enables to search even if its a sub-part of words
checkErs(text); //go recursive.
}
}
function matchedIndexes(text, regex) {
let matches = [];
let match;
while ((match = regex.exec(text)) !== null) matches.push(match.index);
if (!matches[0]) matches = null;
return matches;
}
function removeSecondCharOnIndex(str, lookOut, index) {
let str2 = str.slice(index);
str2 = str2.replace(lookOut);
//console.log(str.slice(0, index) + str2)
return str.slice(0, index) + str2;
}
function randoMan(max) {
let holder = "";
const list = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (let i = 0; i < max; i++) {
holder += list[Math.floor(Math.random() * list.length)];
}
return holder;
}
function modDepBalanced(str, enc) {
new RegExp(enc, "g");
str = str.replace(/[^\[\]\(\)<>{}'"“”‘’„\*«»‹›`′″]/g, ""); //remove all non parentheses
if (str.length === 0) return true; // no braces balanced
if (str.length % 2) return false; // odd length then not balanced.
do {
// Remove facing pairs until there are no more to remove.
len = str.length;
str = str.replace(reduceRegExp, ""); // remove all facing pairs
} while (len !== str.length);
if (str.length === 0) return true; // Balanced and good. :)
return false; // UnBalanced and bad. :(
}
module.exports = {
hAdvancedEnclosersExtract,
hAreEnclosersBalanced,
hRefine,
hParaCore,
hPassPCore,
hAttachEnclosers,
hNextValueFn,
hEscaper,
};