How do I remove this symbol from the CSV file that is generated by Puppeteer? - node.js

When I generate the CSV file each "item" output comes out with a  symbol. How would I go about removing this with my code. I tried to change it to utf-8 because I read that might be what's causing it. Any ideas? Example:
const products = await page.$$('.item-row');
Promise.all(products.map(async product => {
// Inside of each product find product SKU, it's human-readable name, and it's price
let productId = await product.$eval(".custom-body-copy", el => el.innerText.trim().replace(/,/g,' -').replace('Item ', ''));
let productName = await product.$eval(".body-copy-link", el => el.innerText.trim().replace(/,/g,' -'));
let productPrice = await product.$eval(".product_desc_txt div span", el => el.innerText.trim().replace(/,/g,' -'));
// Format them as a csv line
return productId + ',' + productName + ',' + productPrice + ',';
})).then(lines => {
// Write the lines to a file
fs.writeFileSync("products.csv", lines.join('\n'), 'utf-8');
browser.close();
});
});

There are probably better solutions, but the first thing that comes to mind to to change the string to an array with split() and then .map through and test for the à ascii code and change back to a string with join() like this:
const strToChange = 'My string with an à char';
const charWeDoNotWant = 'Ã'.charCodeAt();
const toFixArr = strToChange.split('');
fixedArr = toFixArr.map(char =>
char.charCodeAt() === charWeDoNotWant ? '' : char
);
const fixedStr = fixedArr.join('');
console.log(`String without Ã: ${fixedStr}`);

Related

Encoding Text to Base64 and decoding back to JSON

I am trying to encode a text to Base64 and using NodeJS, and then while getting the data I am decoding it back from Base64. Now I need to change the data into JSON so I could fill the relevant fields but its not converting to JSON.
Here is my code:
fs.readFile('./setting.txt', 'utf8', function(err, data) {
if (err) throw err;
var encodedData = base64.encode(data);
var decoded = base64.decode(encodedData).replace(/\n/g, '').replace(/\s/g, " ");
return res.status(200).send(decoded);
});
In setting.txt I have the following text:
LENGTH=1076
CRC16=28653
OFFSET=37
MEASUREMENT_SAMPLING_RATE=4
MEASUREMENT_RANGE=1
MEASUREMENT_TRACE_LENGTH=16384
MEASUREMENT_PRETRIGGER_LENGTH=0
MEASUREMENT_UNIT=2
MEASUREMENT_OFFSET_REMOVER=1
This decodes the result properly but when I use JSON.parse(JSON.stringify(decoded ) its not converting to JSON.
Can someone help me with it.
Try below snippet
let base64Json= new Buffer(JSON.stringify({}),"base64").toString('base64');
let json = new Buffer(base64Json, 'ascii').toString('ascii');
What does base-64 encoding/decoding have to do with mapping a list of tuples (key/value pairs) like this:
LENGTH=1076
CRC16=28653
OFFSET=37
MEASUREMENT_SAMPLING_RATE=4
MEASUREMENT_RANGE=1
MEASUREMENT_TRACE_LENGTH=16384
MEASUREMENT_PRETRIGGER_LENGTH=0
MEASUREMENT_UNIT=2
MEASUREMENT_OFFSET_REMOVER=1
into JSON?
If you want to "turn it (the above) into JSON", you need to:
Decide on what its JSON representation should be, then
Parse it into its component bits, convert that into an appropriate data struct, and then
use JSON.stringify() to convert it to JSON.
For instance:
function jsonify( document ) {
const tuples = document
.split( /\n|\r\n?/ )
.map( x => x.split( '=', 2) )
.map( ([k,v]) => {
const n = Number(n);
return [ k , n === NaN ? v : n ];
});
const obj = Object.fromEntries(tuples);
const json = JSON.stringify(obj);
return json;
}

How to concat string in node js to call column

I wish to make call different column if user using different language, example in one collection I have description_en for English and description_ind for Indonesia.
The problem if I concat it is not working.
This is example code
let lang = req.params.lang;
order_status_description:element.orderstatus.description + lang,
this is my complete code
const MyOrderResource = (req,res,next)=>{
let lang = req.params.lang;
let arrayResult = [];
req.order.forEach(element => {
let arrayItem =[];
element.orderitem.forEach(item=>{
arrayItem.push({
_id:item._id,
product_id:item.product_id,
product_name:item.product_name,
description:item.description,
unit:item.unit,
price:item.price,
fprice:'Rp ' + new Intl.NumberFormat().format(item.price),
quantity:item.quantity,
amount:item.amount,
fprice:'Rp ' + new Intl.NumberFormat().format(item.amount),
});
});
arrayResult.push({
_id:element._id,
user_id:element.user_id,
date:element.date,
store_id:element.store_id,
store_name:element.profile[0].name,
store_address:element.profile[0].address,
total:element.total,
ftotal:'Rp ' + new Intl.NumberFormat().format(element.total),
order_status_code:element.orderstatus.code,
order_status_description:element.orderstatus.description + lang,
order_item:arrayItem,
});
});
res.status(201).json({
data:arrayResult,
status : 200,
})
}
module.exports = MyOrderResource;
For this code, I wish to call, column description_en, but the result is undefined
Thanks for helping
You can use something like
order_status_description : element.orderstatus["description" + lang]
In the posted snipet, it instructs to concat the value of key- 'element.orderstatus.description' to the value of key - 'lang'

Translate simple text to json with node.js

today i'd like to translate a text to json data
exemple :
1 kokoa#0368's Discord Profile Avatar kokoa#0000 826
2 Azzky 陈东晓#7475's Discord Profile Avatar Azzky 陈东晓#0000 703
3 StarKleey 帅哥#6163's Discord Profile Avatar StarKleey 帅哥#0000 640
to =>
{
"kokoa#0000": "826",
"Azzky 陈东晓#0000": "703",
"StarKleey 帅哥#0000": "640"
}
for the moment i have this one :
fs.appendFile(`./Text/mess.txt`, `${body.replace(/s Discord Profile Avatar/g, '').split(/[\n \t ' ']/)}`, function (err) {
So the result is this one =>
1,kokoa#0368,,kokoa#0000,826
2,Azzky 陈东晓#7475,,Azzky 陈东晓#0000,703
3,StarKleey 帅哥#6163,,StarKleey 帅哥#0000,640
But i would like delete firsts numbers, delete the duplicate array
and make this one on .json
Someone know how can i do this ? And if i need new method to make this one, it's not a problem.
Thank you.
You can read file line by line and convert each line to a 2-elements array with key and value. For example, first line can be converted to:
['kokoa#0000','826']
As you can se 0 index is a key and 1 index is a value. When you convert all lines to this kind of array you can reduce it to an object and convert it to JSON.
See example method:
function convertLinesToJson(lines) {
const dpaText = "Discord Profile Avatar";
const jsonObject = lines
//only with 'Discord ...' text
.filter(line => line.includes(dpaText))
//get what is after 'Discord ...' text and split into 2-elements array
.map(line => {
const index = line.indexOf(dpaText) + dpaText.length;
const keyValue = line.substr(index).trim().split(/[ ]{4}/);
return keyValue;
})
// convert [key, value] array into an object
.reduce((accumulator, currentValue) => {
accumulator[currentValue[0]] = currentValue[1];
return accumulator;
}, {});
//convert object to JSON
return JSON.stringify(jsonObject);
}
Usage:
let lines = [
"1 kokoa#0368's Discord Profile Avatar kokoa#0000 826",
"2 Azzky 陈东晓#7475's Discord Profile Avatar Azzky 陈东晓#0000 703",
"3 StarKleey 帅哥#6163's Discord Profile Avatar StarKleey 帅哥#0000 640"
];
const json = convertLinesToJson(lines);
console.log(json);
Above example code prints:
{"kokoa#0000":"826","Azzky 陈东晓#0000":"703","StarKleey 帅哥#0000":"640"}
See also:
Read a file one line at a time in node.js?

How do I cleanse the data (removing parenthesis and quotes) in NodeJS?

I have a csv sheet where some of the cells could have either single record or multiple records like an array. A sample cell would look like either ["London"] or ["Cairo", "Montreal", "Paris"] - with the parenthesis included. I'm using sever.js to show these records in a visualization on a webpage.
Everything's fine except the parenthesis and quotes are being shown in the visualization. How do I write a rule/logic in nodeJS such that both parenthesis and quotes are not shown in the final visualization?
There's no point in cleansing the data in the csv file itself because I'm gonna have to use this code for hundreds of csv files, so it's better to write a rule in nodeJS.
The following's the server.js code I've used so far:
/**
* API for getting country Names
*/
app.get('/get-country-names.json', (req, res) => {
william.william().then(response => {
res.json(response);
}).catch(message => res.send("Failed to read file" + message));
});
/**
* API to read file and return response in json
* Takes country id as parameter
*/
app.post('/get-city-sources.json', (req, res) => {
let countryName = req.body.countryName;
let city = req.body.city;
william.william().then(sources => {
let filteredSources = [{ name: city, parent: null, color: 'red' }];
Array.from(new Set(sources.filter(source => source.countryName === countryName && source.city === city).map(src => src.source)))
.forEach(source => {
let sourceArr = source.split(',');
console.log(sourceArr);
sourceArr.forEach(src => filteredSources.push({ name: src, parent: city, color: 'blue' }));
});
res.json(filteredSources);
}).catch(message => res.send("Failed to read file" + message));
});
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`))
Regular expression should do the trick. I'm not exactly sure where the stripping should happen within your code but this is a sample function that uses regular expression to strip out square brackets and quotes:
const formatCellString = (cell = '') => {
return cell
// strip out the square brackets
.replace(/^\[(.*?)\]$/, "$1")
// strip out the quotes
.replace(/("\b)|(\b")/g, '')
}
formatCellString('["Cairo", "Montreal", "Paris"]'); => // Cairo, Montreal, Paris
formatCellString('["London"]'); => // London

Error: Timezone "gmt+0200" is not recognized

I've got a problem.
I'm trying to "transfer" some data from one table to each other through *.js file. Everything goes fine, except one thing. This function shows me:
Error: Timezone "gmt+0200" is not recognized
This is my function
async function submitCompletationCG(database, ulogin, idc) {
await connectToSpecificDatabase(database);
const AC_ID = `SELECT * FROM public.cg_actualcompletations WHERE UserLogin = '`+ulogin+`';`;
let AC_IDs = await client.query(AC_ID);
const ACC_ID = `SELECT * FROM public.cg_actualcompletationscondensed WHERE UserLogin = '`+ulogin+`';`;
let ACC_IDs = await client.query(ACC_ID);
const DEPOT = `SELECT * FROM public.cg_actualdepots WHERE UserLogin = '`+ulogin+`';`;
let DEPOTs = await client.query(DEPOT);
let depot_from, depot_to, code_from, code_to;
for(let Depot of DEPOTs.rows) {
depot_from = Depot.depot_from;
depot_to = Depot.depot_to;
code_from = Depot.code_from;
code_to = Depot.code_to;
}
for(let Completation of ACC_IDs.rows) { //Transfer all Completations Condensed
const ACC_Copy = `INSERT INTO public.cg_completationscondensed(
id_c, userlogin, barcode, quantity, adate)
VALUES ('`+idc+`', '`+ulogin+`', '`+Completation.barcode+`', '`+Completation.quantity+`', '`+Completation.adate+`');`;
await client.query(ACC_Copy);
const ACC_Delete = `DELETE FROM public.cg_actualcompletationscondensed
WHERE id = `+Completation.id+`;`;
await client.query(ACC_Delete);
}
for(let Completation of AC_IDs.rows) { //Transfer all Completations
const AC_Copy = `INSERT INTO public.cg_completations(
id_c, userlogin, barcode, quantity, adate)
VALUES ('`+idc+`', '`+ulogin+`', '`+Completation.barcode+`', '`+Completation.quantity+`', '`+Completation.adate+`');`;
await client.query(AC_Copy);
const AC_Delete = `DELETE FROM public.cg_actualcompletations
WHERE id = `+Completation.id+`;`;
await client.query(AC_Delete);
}
const SUB_UArch = `INSERT INTO public.cg_userarch(
userlogin, id_c, depot_from, depot_to, code_from, code_to)
VALUES ('`+ulogin+`', '`+idc+`', '`+depot_from+`', '`+depot_to+`', '`+code_from+`', '`+code_to+`');`;
await client.query(SUB_UArch);
const SUB_DKill = `DELETE FROM public.cg_actualdepots WHERE UserLogin = '`+ulogin+`';`;
await client.query(SUB_DKill);
return true;
}
Sould I set timezone somewhere in angular files? Or it's problem with database? Forgot to say I'm using PostgreSQL. ADate column is type "timestamp without time zone", earlier it was "timestamp with time zone" but I thought it causes the problem and I changed it.
I get this problem in this line:
for(let Completation of ACC_IDs.rows) { //Transfer all Completations Condensed
const ACC_Copy = `INSERT INTO public.cg_completationscondensed(
id_c, userlogin, barcode, quantity, adate)
VALUES ('`+idc+`', '`+ulogin+`', '`+Completation.barcode+`', '`+Completation.quantity+`', '`+Completation.adate+`');`;
await client.query(ACC_Copy);
const ACC_Delete = `DELETE FROM public.cg_actualcompletationscondensed
WHERE id = `+Completation.id+`;`;
await client.query(ACC_Delete);
}
and in next for loop, because there are operation on date too.
I solved this problem. I cannot find answer anywhere on the internet so I do it on my own. I added this two lines at the start of the for loop:
let date = new Date(Completation.adate);
date = date.toLocaleDateString() + " " + date.toLocaleTimeString();
and then changed db query to:
const ACC_Copy = `INSERT INTO public.cg_completationscondensed(
id_c, userlogin, barcode, quantity, adate)
VALUES ('`+idc+`', '`+ulogin+`', '`+Completation.barcode+`', '`+Completation.quantity+`', '`+date+`');`;
and now it works fine, finally!
You are injecting the result of Date() type/value to a query. Converting it ISO string solves my issue.

Resources