Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
csv_node_parser
Advanced tools
Readme
The basic usage of this library consist in a simple csv file reader where you can provide a file path to read and parse and it will output the data based on the file headers. For exmaple
addresses.csv
nombre,apellido,calle,distrito,estado,codigo_postal
John,Doe,120 jefferson st.,Riverside, NJ, 08075
Jack,McGinnis,220 hobo Av.,Phila, PA,09119
"John ""Da Man""",Repici,120 Jefferson St.,Riverside, NJ,08075
Stephen,Tyler,"7452 Terrace ""At the Plaza"" road",SomeTown,SD, 91234
,Blankman,,SomeTown, SD, 00298
"Joan ""the bone"", Anne",Jet,"9th, at Terrace plc",Desert City,CO,00123
You can use the following code:
const CsvParser = require('csv_node_parser');
CsvParser.parse('./addresses.csv')
.then(data => console.log(data));
And the expected output will look like this:
{
rows: [
{
nombre: 'John',
apellido: 'Doe',
calle: '120 jefferson st.',
distrito: 'Riverside',
estado: ' NJ',
codigo_postal: ' 08075'
},
{
nombre: 'Jack',
apellido: 'McGinnis',
calle: '220 hobo Av.',
distrito: 'Phila',
estado: ' PA',
codigo_postal: '09119'
},
...
],
errors: []
}
The errors arr will be filled when in certain row some of the fields are missing, this happens because of performance reasons, the "place" or "position" of the data source is calculated just once based on the headers, so, if the algorithm can't find one field within one specific row it will be returned in the errors array indicating the line that is lacking that field.
Now, let's say that you are willing to use a library that not just read and return the raw rows but also return the desired data contract so you can simply call your ORM bulk create method or perform any other data manipulation sentences.
To accomplish this, we must provide a shema that represents the desired data contract to our previous function, for example, if you provide the following schema
const CsvParser = require('csv_node_parser');
//---addresses.csv----//
nombre,apellido,calle,distrito,estado,codigo_postal
John,Doe,120 jefferson st.,Riverside, NJ, 08075
Jack,McGinnis,220 hobo Av.,Phila, PA,09119
const schema = {
Person: {
name: {
//source must match the header in the csv
source: 'nombre',
},
lastname: {
source: 'apellido'
}
},
Address: {
Street: {
name: {
source: 'calle'
}
},
district: {
source: 'distrito'
}
}
};
CsvParser.parse('./addresses.csv', schema)
.then(res => console.log(res));
You will get the following output
[
{
"Person": {
"name": "John",
"lastname": "Doe"
},
"Address": {
"Street": {
"name": "120 jefferson st."
},
"district": "Riverside"
}
},
...
]
Now, since it's a parser you should be able to perform some data manipulation in runtime, let's say that now our csv file contains the born year of each person, but what you really need is the age, so, instead of having to implement an algorithm from scratch you can simply pass a parsing function to your source field and it will perform any data manipulation you want. For example:
//---addresses.csv----//
nombre,apellido,calle,distrito,estado,codigo_postal,birth_year
John,Doe,120 jefferson st.,Riverside, NJ, 08075,1999
//Now your schema will look something like this
const schema = {
Person: {
name: {
source: 'nombre',
},
lastname: {
source: 'apellido'
},
age: {
source: 'birth_year',
//parse key and indicate a function to execute with the value
parse: (birth_year) => new Date().getFullYear() - parseInt(birth_year)
}
},
Address: {
Street: {
name: {
source: 'calle'
}
},
district: {
source: 'distrito'
}
}
};
And you'll get an output like:
[
{
"Person": {
"name": "John",
"lastname": "Doe",
//parsed age
"age": 21
},
"Address": {
"Street": {
"name": "120 jefferson st."
},
"district": "Riverside"
}
},
...
]
This project is licensed under the MIT License.
FAQs
Library that aims to read and parse csv file on the go based on specified contract
The npm package csv_node_parser receives a total of 3 weekly downloads. As such, csv_node_parser popularity was classified as not popular.
We found that csv_node_parser demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.