-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
45 lines (38 loc) · 1.06 KB
/
utils.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
const cheerio = require("cheerio");
let utils = {
cleanWS: (str) => str.replace(/\n/g,"").replace(/[\s]{2,}/g," "),
trimSpaces: (str) => {
while (str[0] == " ") {
str = str.slice(1);
}
while (str[str.length-1] == " ") {
str = str.slice(0,-1);
}
return str;
},
querySelector: (selector,context,root) => {
return utils.querySelectorAll(selector,context,root,false);
},
querySelectorAll: (selector,context,root,all=true) => {
let query = root(selector,context);
let results = [];
for (let i in query) {
if (!isNaN(parseInt(i))) {
results.push(query[i]);
}
}
return (all) ? results : results[0];
},
render: (cheerio_object) => {
let text = cheerio.load(cheerio_object).text();
return text;
},
elementChildren: (cheerio_object) => {
return cheerio_object.children.filter(e => e.type != "text");
},
lastElementChild: (cheerio_object) => {
let child = cheerio_object.children.filter(e => e.type != "text");
return child[child.length-1];
}
}
module.exports = utils;