New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

@dilanluna/csv-parser

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dilanluna/csv-parser

Parse csv files into javascript objects

latest
Source
npmnpm
Version
1.0.1
Version published
Maintainers
1
Created
Source

CSV Parser

Build Version Node Version License

Simple and lightweight package to parse csv files into javascript objects.

Installation

Package require node.js version 10 or above, and can be installed with npm:

$ npm install @dilanluna/csv-parser

Usage

Import the module in your app.

import csvParser from '@dilanluna/csv-parser';

Or if you're using commonjs:

const csvParser = require('@dilanluna/csv-parser').default;

The csvParser is a function that accepts the following configuration object:

{
  filePath: string;
  separator?: string;
  extractHeaderFromFirstLine?: boolean;
}

Configuration in detail.

PropertyDescriptionDefault
filePathPath to csv file. This property is required.
separatorSeparator character of the csv file.";"
extractHeaderFromFirstLineDetermines if headers are extracted from the first line of the filefalse

Once called to csvParser function with apropiate config, it returns a Promise resolved with an array of parsed objects or rejected if something went wrong.

Example

Supose you have a csv file called fruits.csv like this:

banana;12
apple;5
melon;4
watermelon;2

In your code you parse this file something like this:

import csvParser from '@dilanluna/csv-parser';

const fruits = await csvParser({
  filePath: 'fruits.csv',
});

console.log(fruits);
// Output
// [
//   { 0: 'banana', 1: '12' },
//   { 0: 'apple', 1: '5' },
//   { 0: 'melon', 1: '4' },
//   { 0: 'watermelon', 1: '2' }
// ]

You can map keys in the parsed object from the first line of the csv file.

Adding headers in the first line

name;count
banana;12
apple;5
melon;4
watermelon;2

Now add extractHeaderFromFirstLine to configuration object.

import csvParser from '@dilanluna/csv-parser';

const fruits = await csvParser({
  filePath: 'fruits.csv',
  extractHeaderFromFirstLine: true,
});

console.log(fruits);
// Output
// [
//   { name: 'banana', count: '12' },
//   { name: 'apple', count: '5' },
//   { name: 'melon', count: '4' },
//   { name: 'watermelon', count: '2' }
// ]

License

MIT

Keywords

parser

FAQs

Package last updated on 14 Jul 2022

Did you know?

Socket

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.

Install

Related posts