npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dwtechs/csvx

v0.3.1

Published

CSV Export library written in TypeScript.

Downloads

3,197

Readme

License: MIT npm version last version release date Jest:coverage minified size

Synopsis

CSVx.js is an open source CSV library written in TypeScript.

Motivation

The main purpose of this library is to provide an easy way to export your data as CSV or transform your CSV data to something else.

Installation

npm

$ npm i @dwtechs/csvx

yarn

$ yarn add @dwtechs/csvx

Usage

ES6

<button id="csv">Export CSV</button>
<div id="table"></div>
import { Export, Convert } from '@dwtechs/csvx';

// Convert an array to CSV file
const array = [
  {
    firstname:'Galileo',
    lastname:'Galiléi',
    born:1564,
    died:1642
  },
  {
    firstname:'Nikola',
    lastname:'Tesla',
    city:'Smiljan',
    born:1856,
    died:1943
  },
  {
    firstname:'Albert',
    born:1879,
    lastname:'Einstein',
    died:1955
  }
];
const customLabels = {
  firstname: 'First name',
  lastname: 'Last name', 
  city: 'City',
  born: 'Born',
  died: 'Died'
};
const exportButton = document.getElementById('csv');
exportButton.addEventListener('click', function() {
  Export.data('scientists',array,{separator: ';', customLabels: customLabels});// ; separator for excel friendly imports
});

// Convert CSV data to HTML table
const data = '"Firstname";"Lastname";"Born";"Died"\r\n\
"Galileo";"Galilei";"1564";"1642"\r\n\
"Nikola";"Tesla";"1856";"1943"\r\n\
"Albert";"Einstein";"1879";"1955"';

document.getElementById("table").innerHTML = Convert.table(data,{separator: ';'}, {table: 'table table-striped'});

TypeScript

<button (click)="exportCSV()">Export CSV</button> <!-- ANGULAR -->
import { Export } from '@dwtechs/csvx';

public exportCSV():void {

  const array: { [key: string]: string | number; }[] = [
    {
      firstname:'Galileo',
      lastname:'Galiléi',
      born:1564,
      died:1642
    },
    {
      firstname:'Nikola',
      lastname:'Tesla',
      city:'Smiljan',
      born:1856,
      died:1943
    },
    {
      firstname:'Albert',
      born:1879,
      lastname:'Einstein',
      died:1955
    }
  ];
  const customLabels = {
    firstname: 'First name',
    lastname: 'Last name', 
    city: 'City',
    born: 'Born',
    died: 'Died'
  };

  Export.data('scientists', array, {separator: ';', customLabels: customLabels});

}

IIFE

<script src="node-modules/@dwtechs/csvx/dist/csvx.iife.min.js"></script>
<button id="csv">Export CSV</button>
<div id="table"></div>

// Convert an array to CSV file
var array = [
 {
    firstname:'Galileo',
    lastname:'Galiléi',
    born:1564,
    died:1642
  },
  {
    firstname:'Nikola',
    lastname:'Tesla',
    city:'Smiljan',
    born:1856,
    died:1943
  },
  {
    firstname:'Albert',
    born:1879,
    lastname:'Einstein',
    died:1955
  }
];
var customLabels = {
  firstname: 'First name',
  lastname: 'Last name', 
  city: 'City',
  born: 'Born',
  died: 'Died'
};
var exportButton = document.getElementById('csv');
exportButton.addEventListener('click', function() {
  CSVx.Export.data('scientists',array,{separator: ';', customLabels: customLabels});// ; separator for excel friendly imports
});

// Convert CSV data to HTML table
var data = '"Firstname";"Lastname";"Born";"Died"\r\n\
"Galileo";"Galilei";"1564";"1642"\r\n\
"Nikola";"Tesla";"1856";"1943"\r\n\
"Albert";"Einstein";"1879";"1955"';

document.getElementById("table").innerHTML = CSVx.Convert.table(data,{separator: ';'}, {table: 'table table-striped'});

API Reference


class Convert {
  static setOptions(options: Partial<Options>): void;
  static setCSS(css: Partial<CSS>): void;
  // convert CSV to Javascript array
  static array(data: string, options?: Partial<Options>, css?: Partial<CSS>): Array<Array<string>> | false;
  // convert CSV to HTML table
  static table(data: string, options?: Partial<Options>, css?: Partial<CSS>): string | false;
}

class Export {
  // Export CSV file
  static data(filename: string, data: { [key: string]: number|string }[], options?: Partial<Options>): boolean;
  static setOptions(options: Partial<Options>): void;
}

interface Options {
  data?: string; // default : 'text/csv'
  charset?: string; // default : 'utf-8'
  labels?: boolean; // default : true
  quote?: string; // default : '"'
  separator?: string; // default : ','
  CRLF?: string; // default : '\r\n'
  customLabels: { [key: string]: string }; // default : {}
  save: boolean; //**
}

interface CSS {
  table?: string; // default : ''
  th?: string; // default : ''
}

** With the "save" option to true the user will be prompted to select where to save the file. Please note this option will only work on a https website or on Chrome browser.

Contributors

CSVx.js is still in early development and I would be glad to get all the help you can provide for this project. To contribute you can clone the project on GitHub and See NOTICE.md for detailed installation walkthrough.

License

MIT License

Copyright (c) 2018 Ludovic CLUBER

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.