@a.bayat/csv_creator

Create Comma-Separated-Values based on metadata

Usage no npm install needed!

<script type="module">
  import aBayatCsvCreator from 'https://cdn.skypack.dev/@a.bayat/csv_creator';
</script>

README

Convert Fixed Formated File to Comma Separated Values

A generic tool to convert a fixed formated file to a comma separated values (csv). Based on a metadata file that describe fields, their length and data type, it read fixed formated file line by line (without loading the entire file in memory) and convert them to CSV

How to use

const convertToCSV = require("@a.bayat/csv_creator");
convertToCSV(
  metadataPath,
  readFrom,
  writeTo,
  (encoding = "utf-8"),
  option({
    progress: true,
    error: false
  })
);

Example of metadata file (metadata.txt)

Production Date,10,date
Item Description,30,string
Inventory,6,numeric

Metadata Structure

Fields Lengths Types
<field_1> <Integer> <type>
<field_2> <Integer> <type>
<field_3> <Integer> <type>

Supported data types

  • date
  • string
  • numeric

Example of fixed formated file (fff.txt)

2019-10-30 Milk                          22000
2019-09-15  Multi-grain Bread  !?  ^^@  320
2019-08-01 Rice                          480.9

Example of fixed formated file converted to CSV

Production Date,Item Description,Inventory
30/10/2019,milk,22000
15/09/2019,multi-grain bread,320
01/08/2019,rice,480.9