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

@nielsen-oss/yap-csv

v1.0.0

Published

Node.js package that manages CSV to JSON conversion supporting complex field mapping

Downloads

4

Readme

yap-csv

JavaScript Style Guide The purpose of this module is to support converting a csv (with delimiter support) to an array of json objects. Using a custom map that can handle both simple fields renames and more complicated fields that are a result of calculations

Usage

Add to project

yarn add yap-csv

Structure

The module exposes two variables a Parser and ConverterConsts. One is used for the actual parsing process and one provide consts helpers.

Simple Usage

const {Parser} = require('yap-csv')
// parserPromise is a Promise
const parserPromise = Parser.parseSingle(FILE_NAME, FILE_LOCATION, DELIMITER, FIELDS_MAP, EXTRA_FIELDS_MAP)
parserPromise
.then((formattedData) => {
  // Do what you want with formattedData
})
.catch((e) => {
  console.error(e.stack)
})

Stream Usage

const {Parser} = require('yap-csv')
const formattedData = []
// parserStream is a stream
const parserStream = Parser.parseSingleStream(FILE_NAME, FILE_LOCATION, DELIMITER, FIELDS_MAP, EXTRA_FIELDS_MAP)
parserStream.on('error', (e) => {
  console.error(e.stack)
})
parserStream.on('data', (chunk) => {
  formattedData.push(chunk)
})
parserStream.on('end', () => {
  // Do what you want with formattedData
})

Fields Specification

  • FILE_NAME - The name of the file being parsed
  • FILE_LOCATION - The location of the file being parsed
  • DELIMITER - The delimiter of the text
  • FIELDS_MAP - A conversion map of the fields
  • EXTRA_FIELDS_MAP - A conversion map for new fields that you would like to add

FIELDS_MAP

The following is an example of a map

const convertConsts = require('yap-csv').ConverterConsts

const FIELDS_MAP = {
  'FIELD1': 'simple_name_change',
  'FIELD2': {
    name: 'my_custom_number',
    type: convertConsts.NUMBER_TYPE
  },
  'FIELD3': {
    name: 'functional_change',
    type: (dataObj) => {
      return dataObj['FIELD3'].toLowerCase()
    }
  }
}

As you can see using the fields map you can:

  • Preform simple field name changes
  • Convert to the helper types that are provided in this module
  • Preform conversions using a specified function that receives the original data object (before conversion)

EXTRA_FIELDS_MAP

The following is an example of a map of extra fields

const EXTRA_FIELDS_MAP = {
  complicatedField: (convertedDataObj) => {
    return convertedDataObj['simple_name_change']
  }
}

As you can see using the fields map you can add fields that are not in the original data. Each field should be a function that receives that data after it passed the original formatting using the FIELDS_MAP.

ConverterConsts

The exported consts are:

  • NUMBER_TYPE
  • NUMBER_DOUBLE_TYPE

According the the types we parse the results to a parseInt or parseFloat