nolleh / simple-csv-parser

transform csv string to javascript object

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Coverage Status npm version License: MIT

Overview

simple-csv-parser for javascript / typescript.
marshal/serialize csv string to Object.

Usage

No need other dependency. super simple usage.

class Message {
  name: string;
  message: string;
  constructor(name: string, message: string) {
    this.name = name;
    this.message = message;
  }
}

const messages: Message[] = CsvParser.toObject<Message>(
  `name,message\r\nnolleh,"hello, world"`
);

// do something with messages...
const messages = CsvParser.toObject<Message>(
  `name,message\r\nnolleh,"hello, world"\nnolleh,hello2`
);
console.log({ messages });

result is

{
  messages: [
    { name: 'nolleh', message: 'hello, world' },
    { name: 'nolleh', message: 'hello2' }
  ]
}

if input string is malformed csv, then it return empty array.

About

transform csv string to javascript object

License:MIT License


Languages

Language:TypeScript 98.8%Language:Shell 1.2%