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

vizabi-csv-reader

v0.4.4

Published

vizabi-csv-reader

Downloads

2

Readme

Vizabi CSV reader

The main goal of this repo is to provide ability for Vizabi to work with CSV data.

Install

npm i vizabi-csv-reader

Usage

Usage on backend

import * as fs from 'fs';
import * as path from 'path';
import { csvReaderObject as csvReaderPlainObject } from 'vizabi-csv-reader';

global.d3 = require('d3');
global.Vizabi = require('vizabi');

const readText = (filePath, onFileRead) => {
  fs.stat(filePath, (fileErr, stat: any) => {
    if (fileErr) {
      return onFileRead(fileErr);
    }

    if (stat.code === 'ENOENT') {
      return onFileRead('No such file: ' + filePath);
    }

    fs.readFile(filePath, 'utf-8', (readErr, content) => {
      if (readErr) {
        return onFileRead(readErr);
      }

      onFileRead(null, content.toString());
    });
  });
};

const CsvReader = global.Vizabi.Reader.extend(csvReaderPlainObject);
const csvReaderObject = new CsvReader({
  path: path.resolve('path to csv file'),
  additionalTextReader: readText
});
const result = await csvReaderObject.load();

console.log(result);

Usage on frontend

<script src="node_modules/vizabi-csv-reader/dist/vizabi-csv-reader.js"></script>
<script>
  // CsvReader global variable was imported by script above
  Vizabi.Reader.extend("csv-reader", CsvReader.csvReaderObject);
  // use "csv-reader" as a Vizabi init parameter
  // .....
</script>

Get assets on backend

import * as fs from 'fs';
import { csvReaderObject as csvReaderPlainObject } from 'vizabi-csv-reader';

global.d3 = require('d3');
global.Vizabi = require('vizabi');

const readJson = (filePath, onFileRead) => {
  fs.stat(filePath, (fileErr, stat) => {
    if (fileErr) {
      return onFileRead(fileErr);
    }

    if (stat.code === 'ENOENT') {
      return onFileRead('No such file: ' + filePath);
    }

    fs.readFile(filePath, 'utf-8', (readErr, content) => {
      if (readErr) {
        return onFileRead(readErr);
      }

      try {
        onFileRead(null, JSON.parse(content.toString()));
      } catch (e) {
        onFileRead(e);
      }
    });
  });
};

const CsvReader = global.Vizabi.Reader.extend(csvReaderPlainObject);
const csvReaderObject = new CsvReader({
  assetsPath: 'path to asset file',
  additionalJsonReader: readJson
});
const result = await csvReaderObject.getAsset('path to asset');

console.log(result);

Initial parameters

  • path - path to csv file that would be processed
  • timeInColumns - a flag that indicates that CSV file contains data in time-in-columns format (false by default)
  • lastModified - last modification date (optional)
  • delimiter - CSV delimiter character (optional)
  • keySize - key size (1 by default)
  • hasNameColumn - an extra column that contains country name (should not be processed, optional, false by default)
  • nameColumnIndex - index of nameColumn (default value is 0)
  • assetsPath - path to assets JSON file (optional)
  • additionalTextReader - function that should replace tenured text reading function (optional, see examples above, could be useful on frontend or testing)
  • additionalJsonReader - function that should replace tenured JSON reading function (optional, see examples above, could be useful on frontend or testing)

Run tests

npm test

Build

git clone https://github.com/vizabi/vizabi-csv-reader.git
cd vizabi-csv-reader
npm i
npm run build

The result is a couple of directories: dist and lib.

dist contain two files: vizabi-csv-reader.js and vizabi-csv-reader.js.map. These files could be used on frontend: see Usage on frontend

lib (lib/index.js) - is used 'by default' (see main section of package.json) and could be used on backend (see Usage on backend)