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

csv2x

v0.3.2

Published

convert csv to anything

Downloads

5

Readme

csv2x

command line usage

csv content is specified by --csv

$ node csv2x --csv="A,B
C,D"

<tr>
<td>A<td/>
<td>B<td/>
<tr>
<tr>
<td>C<td/>
<td>D<td/>
<tr>

with input filename --if (CLI-only):

$ node csv2x --if=./test/test.csv

with template filename --tf (CLI-only):

$ node csv2x --if=./test/test.csv --tf=./test/test-template.html

with template option --template to use a predefined template:

$ node csv2x --if=./test/test.csv --template=test

with header option --header to handle the first csv row as field names:

$ node csv2x --header --if=./test/test.csv --template=test

with debug option:

$ DEBUG=csv2x node csv2x --if=./test/test.csv
$ DEBUG=csv2x,csv2x-test npm test

node usage

const csv2x = require('csv2x');

csv2x(
  {csv: 'A,B,C\nX,Y,Z'},
  (err, html) => {
    console.log(html)
    // => '\n<tr>\n<td>A<td/>\n<td>B<td/>\n<td>C<td/>\n<tr>\n<tr>\n<td>X<td/>\n<td>Y<td/>\n<td>Z<td/>\n<tr>'
  }
);

convert to user defined template (applied per row, uses underscore micro-template)

csv2x(
  {
    csv: 'A,B,C\nX,Y,Z', 
    template: 'bla bla: <%=JSON.stringify(row)%>\n'
  },
  (err, result) => {
    // => result: 'bla bla: ["A","B","C"]\nbla bla: ["X","Y","Z"]\n'
  }
);

node example: work with files

const fs = require('fs');

csv2x(
  {
    csv: fs.readFileSync('./test/test.csv', 'utf8'), 
    template: fs.readFileSync('./test/test-template.html', 'utf8'),
    header: true
  },
  (err, result) => {
    // process result..
  }
);

example template file

(test/test-template.html)

<% if (rowIndex === 0) { %>
<html>
  <head>
    <meta charset="utf-8" />
  </head><%
} 

if(!isEmptyRow) { %>
  <h2><%-row.Title %></h2>

  <dl>
    
    <dt>id</dt>
    <dd>XX-<%-optional(row.id, 'none') %></dd>

    <dt>parent id</dt>
    <dd><%-(isEmpty(row['parent id']) ? 'none' : 'XX-' + row['parent id'])%></dd>  

    <dt>Components</dt>
    <dd><%-optional(row.Components, 'N/A') %></dd>  

    <dt>Target Version</dt>
    <dd><%-optional(row['Target Version'], 'none') %></dd>  

    <dt>Implemented Version</dt>
    <dd><%-optional(row['Implemented Version'], 'none') %></dd>  

    <dt>Issue Type</dt>
    <dd><%-optional(row['Issue Type']) %></dd>  

  </dl>

  <h3>Acceptance Criteria</h3>
  <p><pre><%-row['Acceptance Criteria'] %></pre></p>

  <h3>Details</h3>
  <p><pre><%-row['Details'] %></pre></p>

  <h3>Comments</h3>
  <p><pre><%-row['Comments'] %></pre></p><%
} 

if (isLastRow) { %>
</html><%
}
%>

available template values

rowIndex

int current csv row index.

isLastRow

boolean indicating the last csv row.

isEmptyRow

boolean indicating that all cells are empty.

isEmpty

function to check if cell value is empty; white spaces are considered as empty, but not numbers 0.

optional

function returns a an optional value (default empty string) set as the second arg; pass test value to the first argument.

argv

option hash set in csv2x(), minimist argv when running from cli.

parser

csv-parser (papaparse/babyparse) instance.

row

the current csv row data (array or object).

errors

csv-parser errors.

meta

csv-parser meta data

in CommonJs/node context, babyparse will be used

for parser config see http://papaparse.com/docs

AMD support

requires underscore and papaparse to be defined earlier.

Browser support

requires underscore and papaparse loaded before, csv2x expects

window._, window.Papa