New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@coolgk/csv

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@coolgk/csv

read and write csv files

  • 2.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
8
increased by166.67%
Maintainers
1
Weekly downloads
 
Created
Source

@coolgk/csv

a javascript / typescript module

npm install @coolgk/csv

read and write csv files

Examples

import { Csv } from '@coolgk/csv';
// OR
// const { Csv } = require('@coolgk/csv');

const csv = new Csv({
    tmpConfig: { dir: '/tmp/csv' } // optional
});

const arrayData = [
    [1,2,3,4,5],
    [6,7,7,8,9],
    [0,5,8,90,65]
];

const objectData = [
    {col1: 'ab', col2: 'cd', col3: 'ef'},
    {col1: '2ab', col2: '2cd', col3: '2ef'},
    {col1: '3ab', col2: '3cd', col3: '3ef'}
];

csv.createFile(
    arrayData,
    {
        columns: ['column 1', 'column 2', 'column 3', 'h4', 'h5'],
        formatter: (row) => {
            return row.map((value) => 'formatted-' + value);
        }
    }
).then((csvFilePath) => {
    console.log(csvFilePath); // /tmp/csv/151229255018910356N9qKqUgrpzG2.csv
    read(csvFilePath, ['column 1', 'column 2', 'column 3', 'h4', 'h5']);
});

csv.createFile(
    objectData,
    {
        columns: ['col1', 'col2', 'col3'],
        formatter: (row) => {
            return [row.col1 + '+format', row.col2 + '+format', row.col3 + '+format'];
        }
    }
).then((csvFilePath) => {
    console.log(csvFilePath); // /tmp/csv/151229255019910356AlO9kbzkdqjq.csv
    read(csvFilePath, ['col1', 'col2', 'col3']);
});

function read (file, columns) {
    // with columns/headers
    // read lines as object
    const lines = csv.readFile(file, {columns: columns});
    lines.forEach(
        (lineArray, index) => {
            console.log(lineArray, index);
            // {
                // 'column 1': 'formatted-1',
                // 'column 2': 'formatted-2',
                // 'column 3': 'formatted-3',
                // h4: 'formatted-4',
                // h5: 'formatted-5'
            // } 1
        },
        (total) => {
            console.log('read done, total:', total); // read done, total: 4
        }
    );

    // without columns/headers
    // read lines as array
    const lines2 = csv.readFile(file);
    lines2.forEach(
        (lineArray, index) => {
            console.log(lineArray, index); // [ 'formatted-1', 'formatted-2', 'formatted-3', 'formatted-4', 'formatted-5' ] 1
        },
        (total) => {
            console.log('read done, total:', total); // read done, total: 4
        }
    );
}

Csv

Kind: global class

new Csv([options])

ParamTypeDefaultDescription
[options]object
[options.tmpConfig]objectconfig for the generated file
[options.tmpConfig.mode]number0600the file mode to create with, defaults to 0600 on file and 0700 on directory
[options.tmpConfig.prefix]string"Date.now()"the optional prefix
[options.tmpConfig.dir]string"os.tmpdir()"the optional temporary directory, fallbacks to system default

csv.parse(value, [options]) ⇒ promise.<array>

parse a string as csv data and returns an array promise

Kind: instance method of Csv

ParamTypeDefaultDescription
valuestringa csv string
[options]object
[options.columns]Array.<string>array of headers e.g. ['id', 'name', ...] if headers is defined, the row value will be objects
[options.limit]number0number of rows to read, 0 = unlimited
[options.delimiter]string"','"csv delimiter

csv.readFile(file, [options]) ⇒ object

read a csv file. the return value can ONLY be used in a forEach() loop e.g. readFile('abc.csv').forEach((row, index) => { console.log(row, index) })

Kind: instance method of Csv
Returns: object - - { forEach: ((row, index) => void, (totalCount) => void) => void }

ParamTypeDefaultDescription
filestringfile path
[options]object
[options.columns]Array.<string>array of headers e.g ['id', 'name', ...] if defined, rows become objects instead of arrays
[options.limit]number0number of rows to read, 0 = unlimited
[options.delimiter]string"','"csv delimiter

csv.createFile(data, [options]) ⇒ promise.<string>

Kind: instance method of Csv
Returns: promise.<string> - - file path of the csv file generated

ParamTypeDefaultDescription
dataarray | cursormongo cursor or array of data
[options]object
[options.columns]Array.<string>array of headers e.g. ['id', 'name', 'email']
[options.formatter]functioncallback for formatting row data. It takes one row from data as parameter and should return an array e.g. (rowData) => [rowData.id, rowData.name, 'formatted data'],
[options.delimiter]string"','"Set the field delimiter, one character only, defaults to a comma.
[options.filepath]stringfile path is automatically generated if empty

Keywords

FAQs

Package last updated on 20 Jan 2018

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc