-
Notifications
You must be signed in to change notification settings - Fork 3
/
json.ts
43 lines (33 loc) · 1.08 KB
/
json.ts
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
import get from "lodash.get"
import { TScrapedData, TSelectors } from "../../types"
import { applyConditions } from "../tools/condition"
const getSelectorValue = (value: any, selector: any): any => {
if (typeof value !== "string") {
value = JSON.stringify(value)
}
if (selector.conditions && !applyConditions(value, selector.conditions)) {
return null
}
return value
}
const extractData = (j: any, selectors: TSelectors): TScrapedData => {
const data: TScrapedData = {}
if (Array.isArray(j)) {
return j
.map((obj) => {
const data: TScrapedData = {}
for (const [key, selector] of Object.entries(selectors)) {
const value = get(obj, selector.selector, "")
data[key] = getSelectorValue(value, selector)
}
return data
})
.filter((item) => Object.values(item).some((value) => value !== null))
}
for (const [key, selector] of Object.entries(selectors)) {
const value = get(j, selector.selector, "")
data[key] = getSelectorValue(value, selector)
}
return data
}
export default extractData