Socket
Socket
Sign inDemoInstall

text2json

Package Overview
Dependencies
3
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    text2json

Performant parser for textual data (CSV parser)


Version published
Weekly downloads
13
increased by333.33%
Maintainers
1
Install size
0.964 MB
Created
Weekly downloads
 

Readme

Source

text2json

Build Status npm Coverage Status Downloads

Performant parser for textual data

  • Parse 100K rows in ~550 ms (may vary with data)
  • Very low memory footprint (~10 MB)
  • Supports parsing from file, string or buffers
  • Supports streaming output
  • Passes CSV Acid Test suite csv-spectrum

Parsing the following bit of data

id,firstName,lastName,jobTitle
1,Jed,Hoppe,Customer Markets Supervisor
2,Cristian,Miller,Principal Division Specialist
3,Kenyatta,Schimmel,Product Implementation Executive

will produce

[ { id: '1',
    firstName: 'Jed',
    lastName: 'Hoppe',
    jobTitle: 'Customer Markets Supervisor' },
  { id: '2',
    firstName: 'Cristian',
    lastName: 'Miller',
    jobTitle: 'Principal Division Specialist' },
  { id: '3',
    firstName: 'Kenyatta',
    lastName: 'Schimmel',
    jobTitle: 'Product Implementation Executive' } ]

Usage

Installation

npm install text2json --save

Quick start

  • Parse the entire file into JSON
'use strict'

let Parser = require('text2json').Parser
let rawdata = './data/file_100.txt'

let parse = new Parser({hasHeader : true})

parse.text2json (rawdata, (err, data) => {
  if (err) {
    console.error (err)
  } else {
    console.log(data)
  }
})
  • If parsing a large file, stream the output
'use strict'

let Parser = require('text2json').Parser
let rawdata = './data/file_500000.txt'

let parse = new Parser({hasHeader : true})

parse.text2json (rawdata)
   .on('error', (err) => {
     console.error (err)
   })
   .on('headers', (headers) => {
     console.log(headers)
   })
   .on('row', (row) => {
     console.log(row)
   })
   .on('end', () => {
     console.log('Done')
   })

Options

The parser accepts following options through its constructor (all are optional)

{
  hasHeader?: boolean,
  headers?: string[],
  newline?: string,
  separator?: string,
  quote?: string,
  encoding?: string,
  skipRows?: number,
  filters?: Filters,
  headersOnly?: boolean
}
  • hasHeader
    • If true, first line is treated as header row.
    • Defaults to false.
  • headers
    • An array of strings to be used as headers.
    • If specified, overrides header row in data.
    • Defaults is an empty array
  • newline
    • Choose between Unix and Windows line endings (\n or \r\n)
    • Defaults to \n
  • separator
    • Specify column separator
    • Defaults is , (comma)
  • quote
    • Specify quote character
    • Default is " (double quotes)
  • encoding (see https://nodejs.org/api/buffer.html#buffer_buffers_and_character_encodings)
    • Use a different encoding while parsing
    • Defaults to utf-8
  • skipRows
    • Number of rows to skip from top (excluding header)
    • Default is zero rows
  • filters
    • Filter columns based on index or header name
  • headersOnly
    • Parse only header row

Header fill

If hasHeader is false and custom headers are not specified, parser will generate headers using a zero based index of the columns. i.e. when data has 5 columns, generated headers will be ['_1', '_2', '_3', '_4', '_5']

Header fill will also occur when number of headers given in custom headers array is less than the actual numbers of columns in the data.

Events

  • headers - emitted after parsing the header row or once header fill has completed. The payload contains an array of header names.
  • row - emitted once for every row parsed. Payload is an object with properties corresponding to the header row.
  • error - emitted once for the first error encountered. Payload is an Error object with an indicative description of the problem.
  • end - emitted once, when the parser is done parsing. No payload is provided with this event.

Roadmap

  • Return columns selectively (either by column index or header name)
  • Ignore header row in data and use custom header names provided in options
  • Skip rows (start parsing from a given row number)

Keywords

FAQs

Last updated on 07 Aug 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc