GithubHelp home page GithubHelp logo

isabella232 / buffer-js-csv-exporter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bufferapp/buffer-js-csv-exporter

0.0 0.0 0.0 2.8 MB

Client side sanitization, generation and zipped CSV files.

JavaScript 100.00%

buffer-js-csv-exporter's Introduction

๐Ÿ›  CSV Exporter Build Status Coverage Status

This module is to generate and download CSV files on the client side.

How to use

Export Files

The CSV Exporter takes a n number of folders and files to be downloaded to the client.

import {exportFiles} from 'csv-exporter';

const CSVData = {
  headers: ['Friends', 'Fruit'],
  rows: [
    ['Bob', 'Banana'],
    ['Jim', 'Apple'],
    ['Joe', 'Strawberry']
  ]
};

const folder1Files = [
  {
    name: subFolderFile,
    content: CSVData
  }
];

const files = {
  name: 'Folder_1',
  files: folder1Files
};

exportFiles(files);

Export File

Or you can export a single CSV file.

import {exportFiles} from 'csv-exporter';

const CSVData = {
  headers: ['Friends', 'Fruit'],
  rows: [
    ['Bob', 'Banana'],
    ['Jim', 'Apple'],
    ['Joe', 'Strawberry']
  ]
};

const file = {
  name: 'csv_file.csv',
  content:
};

exportFile(file);

If the file has a name and files values then the files is considered a folder. If the file has a name and content values then it is considered a file.

the content value must be an object with a header and rows fields. The headers will be used as the CSV files headers and the rows will be used as the CSV rows content.

Data Formatting

There are two helper functions to formatter you data for CSV generation.

mapListToCSVArray

The mapListToCSVArray function takes three arguments. The first argument is an array of javascript object representing rows of CSV data as key value pairs in an ordered list. The Second argument is an array of string that represent the CSV headers as an ordered list. The last argument is an array of string keys that will be used to get the CSV rows value from the first arguments data object as an ordered list. For nested values the key can be represented as an array with a nested strings mapping to that value.

import {mapListToCSVArray, exportFile} form 'csv-exporter';

let headers = ['Foo'];
let rowData = [{foo: 'bar'}, {foo: 'baz'}];
let rowKeys = ['foo'];

let csvArray = mapListToCSVArray(rowData, headers, rowKeys);

let file = {
  name: 'exporter.csv',
  content: csvArray
};

exportFile(file);

csvArray will now have two fields, headers and rows that you can pass as the value of a files content into exportFile.

csvArray => {
  headers: ['Foo'],
  rows: [
    ['bar'],
    ['baz']
  ]
}

mapToCSVRow

The mapListToCSVRow function takes two arguments.

This method is used internally by mapListToCSVArray to generate the csv rows.

The first argument is an Object that represents a row of CSV data as a key value pairs. The seconds is an Array of csv data keys in an ordered list that will represent that row.

import {mapListToCSVArray, exportFile} form 'csv-exporter';

let data = {
  foo: 'bar',
  baz: 1
};

let csvRow = mapToCSVRow(data, ['foo', 'baz']);

which will generate the following output

[['bar', 1], ['bar', 1]]

Tests

To test simply npm install make sure you have mocha install globally npm install -g mocha and run npm test : )

buffer-js-csv-exporter's People

Contributors

alex-ray 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.