Giter Site home page Giter Site logo

csv-lite-js's Introduction

csv-lite-js

CSV Library for Browser

Install

$ npm install csv-lite-js

Usage

Simple static method:

import CSV from 'csv-lite-js'
const txt = "1,2,3\n11,22,33\n111,222,333";
const r = CSV.parse(txt);
console.log(CSV.stringify(r));

OOP method:

var csv = new CSV.CSVObject()
csv.parse("name,age\r\nAki,14\r\nHuyu,20")
console.log(csv.getCell(1,0)) // Aki
console.log(csv.getCell(2,0)) // Huyu
console.log(csv.toString())

Parse and Stringify

method:

  • CSV.parse()
  • CSV.stringify()

Parse CSV from text

var csv = "a,b,c\nd,e,f";
var a = CSV.parse(csv);
console.log(a[0][0]); // a
console.log(a[0][1]): // b
console.log(a[1][0]); // d
console.log(a[1][1]): // e

Stringify Array to CSV

var a = [[1,2,3],[4,5,6]]
var c = CSV.stringify(a);
console.log(c); // 1,2,3\n4,5,6

TSV (Tab separated value)

// set tsv option
CSV.options.delimiter = "\t";

// parse CSV string
var txt = "1\t2\t3\n11\t22\t33\n111\t222\t333";
var a = CSV.parse(txt);
console.log(a[0][0]); // 1
console.log(a[1][1]); // 22

OOP method

CSV.CSVObject has many method

  • CSVObject.toString()
  • CSVObject.parse(csv_str, delimiter)
  • CSVObject.getCell(row, col)
  • CSVObject.setCell(row, col, value)
  • CSVObject.setArray( array )
  • CSVObject.getArray()
  • CSVObject.find(colNo, keyword, offset)
  • CSVObject.findAll(colNo, keyword)
  • CSVObject.findAll(colNo, keyword, offset, limit)
  • CSVObject.filter(colNo, callback)
  • CSVObject.sort(colNo, isAsc)
  • CSVObject.sortNumber(colNo, isAsc)
  • CSVObject.insertRow(rowNo, rowArray)
  • CSVObject.insertCol(colNo, values)
  • CSVObject.deleteRow(rowNo)
  • CSVObject.deleteCol(colNo)
  • CSVObject.add(rowArray)
  • CSVObject.length
  • CSVObject.useHeader

Test module

$ mocha

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.