Coder Social home page Coder Social logo

vweevers / tabular-stream Goto Github PK

View Code? Open in Web Editor NEW
36.0 3.0 2.0 239 KB

Detects tabular data (spreadsheets, dsv or json, 20+ different formats) and emits normalized objects.

License: MIT License

JavaScript 100.00%
nodejs stream spreadsheets csv dsv tsv json ndjson xls xlsx

tabular-stream's Introduction

tabular-stream

Note: the core of tabular-stream 1.0 has moved to detect-tabular. This is now a data normalizer on top of that.

Detects tabular data (spreadsheets, dsv or json, 20+ different formats) and emits objects. Ensures all rows have the same keys, optionally transforms keys and tries to coerce values to numbers. Spreadsheets and DSV must have a header.

npm status node Test Standard

Example

npm i tabular-stream snake-case format-data
const tabular = require('tabular-stream')
const fs = require('fs')
const snake = require('snake-case').snakeCase
const format = require('format-data')

fs.createReadStream('test/air_pollution_nl.xlsx')
  .pipe(tabular(snake))
  .pipe(format('json'))
  .pipe(process.stdout)

Need a CLI doing just this? Jump to tabular-cli, which pairs tabular-stream with format-data to convert tabular data to json, ndjson, dsv or sse. For example:

tabular -k snake-case -o tsv < input.xls > output.tsv

API

tabular([keys || options])

Returns a duplex stream - give it any tabular data, get back objects. (keys) is a shorthand for ({ keys: keys }). The available options are:

function keys

An optional function to transform and/or filter keys. Receives a single argument, for every key of the first row. Everything at change-case works well. If it returns an empty string or anything other than a string, the key is ignored (i.e. not included in the emitted objects).

function keys(key) {
  if (key === 'useless') return false
  return key.toUpperCase()
}

mixed defaultValue

Fallback value to use for null and undefined values. Default is 0.

boolean bare

Whether to emit null prototype objects via Object.create(null) or plain javascript objects (the default).

Other

Other options are passed as-is to spreadsheet-stream (if applicable). NB. Because the binary spreadsheets formats are not streamable, spreadsheet-stream will buffer the whole thing in memory. As a safe-guard you can set the maxSize option (in bytes): tabular({ maxSize: 1024 * 1024 }). See spreadsheet-stream for details.

Install

With npm do:

npm install tabular-stream

License

MIT.

Test data © Statistics Netherlands, The Hague/Heerlen.

tabular-stream's People

Contributors

dependabot[bot] avatar greenkeeper[bot] avatar vweevers avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

tabular-stream's Issues

Tests fail with excel-stream (incorrect output)

Three problems:

  • Object keys weirdly include a " character (courtesy of csv-stream, not an issue if csv-parser is used)
  • excel-stream coerces to numbers..
    • but does not account for european notation (1,23 instead of 1.23)
    • and my test does not expect numbers
  • Output of j ends with two newlines, and both csv-stream and csv-parser turn that into an object with empty values

Data omitted if not in first row of XLS

Using an XLS file (not a CSV), if columns are missing from the first data row (second row) then you only get the data on subsequent rows for the columns present in the first data row.

Consider the following, but saved as an XLS file:

A,B,C
1,,
1,2,3
4,5,6

You get:

{ A: 1 }
{ A: 1 }
{ A: 4 }

Using detect-tabular instead you get:

{ A: 1 }
{ A: 1, B: 2, C: 3 }
{ A: 4, B: 5, C: 6 }

Reading a CSV file gives you:

{ A: 1, B: 0, C: 0 }
{ A: 1, B: 2, C: 3 }
{ A: 4, B: 5, C: 6 }

Ignore the issue with the zeros.

It may be that the underlying Excel reader could be considered faulty and should emit consistent objects with null values for missing data, but I'm not sufficiently familiar with it all to make a judgement.

Test code that I used:

var tabular = require("tabular-stream"); // or "detect-tabular" for comparison.
var fs = require("fs");
var stream = tabular();
stream.on("data", console.log);
stream.end(fs.readFileSync(__dirname+"/test.xls"));

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.