Code Monkey home page Code Monkey logo

csvman's Introduction

csvman

Nodejs library which helps with CSV file management.

The library acts as a lightwight database wih CSV files.

Installation

$ npm install --save csvman

Example

/*** NodeJS script ***/
const CSV = require('csvman');

const fja = async () => {

  const csvOpts = {
    filePath: './appended_arr.csv',
    encoding: 'utf8',
    mode: 0o644,

    fields: ['url', 'name', 'size'], // only these fields will be effective
    fieldDelimiter: ',',
    fieldWrapper: '"',
    rowDelimiter: '\n'
  };
  const csv = new CSV(csvOpts);

  const rows = await csv.readRows(false); // all types will be string
  // const rows = await csv.readRows(true); // or just csv.readRows()
  console.log('rows in total:: ', rows.length);
  console.log(JSON.stringify(rows, null, 4));
};

fja();

API

constructor(opts) :void

const CSV = require('csvman);

const opts = {
  filePath: './input.csv',
  encoding: 'utf8',
  mode: '0664',
  fields: ['url', 'name'], // define active CSV fields
  fieldDelimiter: ',',
  fieldWrapper: '"',
  rowDelimiter: '\n'
}
const csv = new CSV(opts);

async createFile() :void

Create CSV file defined in opts.filePath if it does not exist. If the file exists, it is NOT MODIFIED.

async addHeader() :void

Add fields into the CSV Header. Only if the file is empty.

async writeRows(rows:array) :void

Write multiple CSV rows. The rows argument is an array of objects. CAUTION: Old content will be overwritten when this method is used.

async appendRows(rows:array) :void

Append multiple CSV rows. The rows argument is an array of objects. New content will be added to the old content.

async readRows(convertType: boolean) :array

Read CSV rows and convert it in the array of objects. If convertType is true then fields will convert the type automatically, for example string '5' will become number 5. The default is true.

async updateRows(query:object, doc:object, upsert:boolean) :{count:number, rows_updated: object[]}

Find CSV row by query and update it with doc. The query is inspired by MongoDB queries so it can use.

$eq, $ne, $gt, $gte, $lt, $lte, $regex, $in, $exists

For example: {name: {$regex: /john/i}}

The doc is object whose properties are CSV fields. If the upsert is true insert a new row if the rows are not found by the query.

async findRows(query) :object[]

Find CSV rows by the query. Use MongoDb inspired queries.

async removeRows(query) :object[]

Find and remove CSV rows by the query. Use MongoDb inspired queries. The returned value is the array of removed row objects.

async extractFields() :array

Get fields array from the first (header) row.

License

The software licensed under AGPL-3.

csvman's People

Watchers

 avatar  avatar

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.