-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
37 lines (32 loc) · 941 Bytes
/
index.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
const fs = require("fs");
const path = require("path");
const { getCache, addCache } = require("../lib/caches.js");
async function getAddressInfoByCEP(cep) {
try {
const filePath = path.resolve(__dirname, "./ceps.csv");
let data = await getCache();
console.log(data?.length);
if (!data) {
console.log("n");
data = fs.readFileSync(filePath, "utf8");
await addCache(data);
}
const lines = data.split("\n");
for (let line of lines) {
const fields = line.split(";");
if (fields.length >= 4 && fields[0] === cep.replace("-", "")) {
return {
state: fields[1],
city: fields[2],
neighborhood: fields[3],
street: fields[4].replace("\r", ""),
};
}
}
return "NOT_FOUND";
} catch (error) {
console.error("Error reading file:", error);
return "FILE_NOT_FOUND";
}
}
module.exports = { getAddressInfoByCEP };