Transform stream that lets you peek the first line before deciding how to parse it

Overview

streampecker

Transform stream that lets you peek the first line before deciding how to parse it.

npm install streampecker

This module was forked from peak-stream at version v1.1.3.

Usage

const pecker = require('streampecker')
const ldjson = require('ldjson-stream')
const csv = require('csv-parser')
const { Readable } = require('stream')

const isCSV = function (data) {
  return data.toString().indexOf(',') > -1
}

const isJSON = function (data) {
  try {
    JSON.parse(data)
    return true
  } catch (err) {
    return false
  }
}

const parser = function (stream) {
  return pecker(stream, function (data) {
    // maybe it is JSON?
    if (isJSON(data)) return ldjson()

    // maybe it is CSV?
    if (isCSV(data)) return csv()

    // we do not know - bail
    throw new Error('No parser available')
  })
}

let parse

parse = parser(Readable.from('{"hello":"world"}\n{"hello":"another"}\n'))

parse.on('data', function (data) {
  console.log('from ldj:', data)
})

parse = parser(Readable.from('test,header\nvalue-1,value-2\n'))

parse.on('data', function (data) {
  console.log('from csv:', data)
})

License

MIT

You might also like...

The Cassandra/Scylla library you didn't want but got anyways.

The Cassandra/Scylla library you didn't want but got anyways.

Installation Using npm: npm install scyllo or if you prefer to use the yarn package manager: yarn add scyllo Usage import { ScylloClient } from 'scyll

Jul 20, 2022

ConnectNOW - Live web application that allows you to connect with people around the world!

connectNOW Live web application that allows you to connect with people around the world! You can share images, quotes, and anything on your mind! It a

Jan 3, 2022

The Wholesome App. A project that allows you to upload images directly to MongoDB Atlas into your collection, a faster cloud database.

The Wholesome App. A project that allows you to upload images directly to MongoDB Atlas into your collection, a faster cloud database.

The Wholesome App. A project that allows you to upload images directly to MongoDB Atlas into your collection, a faster cloud database. To upload your cute and wholesome images.

Jul 17, 2022

Tools that eat glass so you don't have to

GlassEater Tools that eat glass so you don't have to. Docs coming soon... Features generates the most precise and optimal getProgramAccounts queries p

Sep 27, 2022

✏️ Extended Writer supercharges Kirby's built-in Writer field with useful marks, nodes and features you wish were built-in

✏️ Extended Writer supercharges Kirby's built-in Writer field with useful marks, nodes and features you wish were built-in

Extended Writer Extended Writer supercharges Kirby's built-in Writer field and block with useful marks, nodes and features you wish were built-in. Fea

Nov 10, 2022

A postgraphile plugin that allows you to expose only a single direction of connections exposed by foreign key constraints

A postgraphile plugin that allows you to expose only a single direction of connections exposed by foreign key constraints

Mar 13, 2022

A JSON Database that saves your Json data in a file and makes it easy for you to perform CRUD operations.

What is dbcopycat A JSON Database that saves your Json data in a file and makes it easy for you to perform CRUD operations. ⚡️ Abilities Creates the f

Jan 8, 2023

A remote nodejs Cached sqlite Database Server, for you to have your perfect MAP Cache Saved and useable remotely.

A remote nodejs Cached sqlite Database Server, for you to have your perfect MAP Cache Saved and useable remotely.

A remote nodejs Cached sqlite Database Server, for you to have your perfect MAP Cache Saved and useable remotely. Easy Server and Client Creations, fast, stores the Cache before stopping and restores it again! it uses ENMAP

Dec 18, 2022

⚡️The Fullstack React Framework — built on Next.js

⚡️The Fullstack React Framework — built on Next.js

The Fullstack React Framework "Zero-API" Data Layer — Built on Next.js — Inspired by Ruby on Rails Read the Documentation “Zero-API” data layer lets y

Jan 4, 2023

Parse, validate and transform data with confidence in nuxt using zod

nuxt-parse A nuxt focused package to make data validation and parsing easy. This package follows the design philosophy of the article parse, don't val

Jan 7, 2023

a babel plugin that can transform generator function to state machine, which is a ported version of typescript generator transform

Babel Plugin Lite Regenerator intro This babel plugin is a ported version of TypeScript generator transform. It can transform async and generator func

Jul 8, 2022

A C++ based command-line (CLI) program that lets you manage your tasks

A C++ based command-line (CLI) program that lets you manage your tasks

COMMAND LINE INTERFACE TODO APP a command-line (CLI) program that lets you manage your tasks. The specification for this project is written down as te

Dec 25, 2021

A cool tool that saves you time if you want to remove node_modules before running 'npm i'

rmnpm A cool tool that saves you time if you want to remove your node_modules folder before running the npm install command. How does it do it? By fir

Jul 16, 2022

A community-centric site like you've never seen before.

Kleptonix A community-centric site like you've never seen before. Overview This section will be updated when basic posting and account creation functi

Apr 19, 2022

A CLI tool that allows you to ensure a database is live before closing the process

Wait for a database to be available prior to launching subsequent commands. 📙⌛

Apr 16, 2022

Useful userscript, allowing you to steal NFTs from Twitter even easier than before!

Adds missing feature that even Twitter Blue doesn't have: click on hexagonal avatar to open it in a new tab and save yourself a couple of clicks while stealing it!

Jan 21, 2022

A remote nodejs Cache Server, for you to have your perfect MAP Cache Saved and useable remotely. Easy Server and Client Creations, fast, stores the Cache before stopping and restores it again!

A remote nodejs Cache Server, for you to have your perfect MAP Cache Saved and useable remotely. Easy Server and Client Creations, fast, stores the Cache before stopping and restores it again!

remote-map-cache A remote nodejs Cache Server, for you to have your perfect MAP Cache Saved and useable remotely. Easy Server and Client Creations, fa

Oct 31, 2022

This package will help parse OData strings (only the Microsoft Dataverse subset). It can be used as a validator, or you can build some javascript library which consumes the output of this library.

@albanian-xrm/dataverse-odata This package will help parse OData strings (only the Microsoft Dataverse subset). It can be used as a validator, or you

Oct 22, 2022
Releases(v2.0.0)
Owner
Matteo Collina
TSC member @nodejs, Lead Maintainer @fastify, Conference Speaker, Ph.D.
Matteo Collina
Parse and disassemble .metallib files in browser

MetalLibraryExplorer Parse and disassemble .metallib files in browser. https://yuao.github.io/MetalLibraryExplorer This is a WebAssembly port of Metal

Yu Ao 5 Dec 1, 2022
Get JSON RPC on a stream

json-rpc-on-a-stream Get JSON RPC on a stream npm install json-rpc-on-a-stream

Mathias Buus 13 May 31, 2022
Share an abstract-level database over the network or other kind of stream.

many-level Share an abstract-level database over the network or other kind of stream. The successor to multileveldown. If you are upgrading, please se

Level 15 Mar 27, 2022
Mobile-first website to match refugees with available hosts

⚠️ This project has been archived ⚠️ Host a Refugee Host a refugee aims to be a small mobile friendly web app that allows people to sign up as either

Tech for Ukraine - Centralized Information 14 Apr 8, 2022
First NestJS project powered by TypeScript (Simple CRUD)

First Nest TS (TypeScript) First NestJS project powered by TypeScript (Simple CRUD) Routes Get All GET http://localhost:3000/products/ Get one GET htt

Max Base 3 Feb 22, 2022
If you are a Slack browser user, this userscript will allow you to auto-redirect without Slack asking if you want to open on the desktop app.

Auto-redirect for browser Slack users If you are a Slack browser user, this userscript will allow you to auto-redirect without Slack asking if you wan

Felipe Santos (he/him) 6 Aug 23, 2022
Azure Data Studio is a data management tool that enables you to work with SQL Server, Azure SQL DB and SQL DW from Windows, macOS and Linux.

Azure Data Studio is a data management tool that enables working with SQL Server, Azure SQL DB and SQL DW from Windows, macOS and Linux.

Microsoft 7k Dec 31, 2022
In how many ways can you print in the console 50 times?

In how many ways can you print in the console 50 times I was asked at an interview how to print in the console "Hello World" 50 times in as few lines

Bruno Noriller 3 Oct 4, 2021
Welcome to the LEGO Games Repository, where you can enjoy anytime, anywhere. This is the 2021 KNU Advanced Web Programming team project.

Welcome to LEGO git repository! Here are some useful information about LEGO service. 0. Docker image Link : https://hub.docker.com/r/leibniz21c/legoga

Heesung Yang 16 Jul 21, 2022
A Gmail Clone which built with ReactJS and Redux. You can sign in with your Google Account, compose a new e-mail and send realtime emails to the project.

Gmail Clone with ReactJS A Gmail Clone that you can sign in with your Google Account, compose a new e-mail and send realtime emails to the project. Cl

Özge Coşkun Gürsucu 49 Nov 14, 2022