-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScrapeBit.js
91 lines (65 loc) · 2.63 KB
/
ScrapeBit.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
module.exports = { FirstString, AllString };
function FirstString(SourceText, ToFindText, ReadUntilString) {
const dIndex = SourceText.indexOf(ToFindText); // Get Index in string of the find Text.
if (dIndex === -1) return null // If Find Text is not found return.
SourceText = SourceText.substring(dIndex + ToFindText.length);
let FoundTerminators = 0; // ? 0 index based
let ReadTextChars = []; //Where to store the good chars.
SourceText = [...SourceText]; // To Char Array
const TerminatorChars = [...ReadUntilString]; //To Char Array
for (let i = 0; i < SourceText.length; i++) {
if (FoundTerminators === TerminatorChars.length) break;
if (SourceText[i] === TerminatorChars[FoundTerminators]) {
FoundTerminators++;
continue;
}
FoundTerminators = 0;
ReadTextChars.push(SourceText[i]); // Add the good char
}
if (ReadTextChars.length <= 0) return null;
else return ReadTextChars.join("");
}
function AllString(Source, Find, ReadUntil) {
var Indexes = getIndicesOf(Find, Source, false);
var Results = [];
for (let index of Indexes) {
Results.push(FromIndex(Source, Find, ReadUntil, index))
}
return Results;
}
function FromIndex(SourceText, ToFindText, ReadUntilString, CustomIndex) {
const dIndex = CustomIndex; // Get Index in string of the find Text.
if (dIndex === -1) return null // If Find Text is not found return.
SourceText = SourceText.substring(dIndex + ToFindText.length);
let FoundTerminators = 0; // ? 0 index based
let ReadTextChars = []; //Where to store the good chars.
SourceText = [...SourceText]; // To Char Array
const TerminatorChars = [...ReadUntilString]; //To Char Array
for (let i = 0; i < SourceText.length; i++) {
if (FoundTerminators === TerminatorChars.length) break;
if (SourceText[i] === TerminatorChars[FoundTerminators]) {
FoundTerminators++;
continue;
}
FoundTerminators = 0;
ReadTextChars.push(SourceText[i]); // Add the good char
}
if (ReadTextChars.length <= 0) return null;
else return ReadTextChars.join("");
}
function getIndicesOf(searchStr, str, caseSensitive) {
var searchStrLen = searchStr.length;
if (searchStrLen == 0) {
return [];
}
var startIndex = 0, index, indices = [];
if (!caseSensitive) {
str = str.toLowerCase();
searchStr = searchStr.toLowerCase();
}
while ((index = str.indexOf(searchStr, startIndex)) > -1) {
indices.push(index);
startIndex = index + searchStrLen;
}
return indices;
}