file-to-objects

1.0.4 • Public • Published

file-to-objects Travis XO code style

Parse a property delimited file, line-by-line, and return the line in a JavaScript array in object representation.

fileToObjects(📄) => [ {}, {}, {} ]

Install

$ npm install --save file-to-objects OR $ yarn add file-to-objects

Usage

// CATS_DATA.csv:
// Bubbles,Persian,24,honey-brown
// Garfield,Tabby,33,orange with stripes
const fileToObjects = require('file-line-parse');
const input = './data/CATS_DATA.csv';
const keys = [ 'name', 'breed', 'age', 'color'];
fileToObjects(input, { keys: keys })
    .then(cats => {
        console.log(cats[0])
        // { name: 'Bubbles', breed: 'Persian', age: '24', color: 'honey-brown' }
 
        console.log(cats[1])
        // { name: 'Garfield', breed: 'Tabby', age: '33', color: 'orange with stripes' }
    });

API

fileToObjects(input, [options])

Return a set of parsed objects dictacted by sequence of keys

input | <string>

Path to file to be input.

options | <object>

options.keys | <array>[<string> | <int>]

An array of keys to serve as a mapping for objects, this will override any column header to serve as a mapping for object creation.

options.delimiter | <string>

The delimiter to used for parsing. Default: ","

options.encoding | <string>

The encoding for reading the file. Default: "utf8"

Related

🔄 objects-to-file - Create a delimited value, output file from an array of objects.

License

MIT

Readme

Keywords

Package Sidebar

Install

npm i file-to-objects

Weekly Downloads

4

Version

1.0.4

License

MIT

Last publish

Collaborators

  • brh55