Commit 4cff4a83 authored by gustawdaniel's avatar gustawdaniel
Browse files

JSDOM replaced by Cheerio (3.4) times faster

parent 0e96ff56
import fs from 'fs'
import { JSDOM } from 'jsdom'
import cheerio from 'cheerio';
interface FileInput {
type: string,
......@@ -15,16 +15,16 @@ const getFiles = (): FileInput[] => fs.readdirSync(process.cwd() + `/raw`).map(n
content: fs.readFileSync(process.cwd() + '/raw/' + name).toString()
}))
const processFile = ({ type, content }: FileInput): Output => ({
[type]: [...new JSDOM(content).window.document.querySelectorAll('.tabela.big.m0.tlo_biel>tbody>tr')].reduce((p, n) => ({
const processFile = ({type, content}: FileInput): Output => ({
[type]: cheerio.load(content)('.tabela.big.m0.tlo_biel>tbody>tr').toArray().reduce((p, n) => ({
...p,
[n.querySelector('td')?.textContent || '']: parseFloat((n.querySelector('td.ar')?.textContent || '').replace(',', '.'))
...((el) => ({[el.find('td').first().text()]: parseFloat(el.find('td.ar').first().text().replace(',', '.'))}))(cheerio(n))
}), {})
})
const reducer = (p: Output, n: Output): Output => {
Object.keys(n).forEach(k => {
Object.keys(p).includes(k) ? p[k] = { ...p[k], ...n[k] } : p[k] = n[k];
Object.keys(p).includes(k) ? p[k] = {...p[k], ...n[k]} : p[k] = n[k];
})
return p
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment