Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: added some changes and refactoring to code #37

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 17 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,58 +24,36 @@ npm install --save csv2json

## Usage

### CLI

```
Usage: csv2json [OPTIONS] [<input file> [<output file>]]

-d, --dynamic-typing
Convert booleans and numeric to their type instead of strings.

-s <separator>, --separator=<separator>
Field separator to use (default to comma “,”).

-t, --tsv
Use tab as separator, overrides separator flag.

<input file>
CSV file to read data from.
If unspecified or a dash (“-”), use the standard input.

<output file>
JSON file to write data to.
If unspecified or a dash (“-”), use the standard output.
```

### Stream
### Json Object

```javascript
var csv2json = require('csv2json');
var fs = require('fs');

fs.createReadStream('data.csv')
.pipe(csv2json({
// Defaults to comma.
separator: ';'
}))
.pipe(fs.createWriteStream('data.json'));
var csv2json = require("csvtojson-parser");
var fs = require("fs");
csv2json("example.csv", options, function (err, jsonData) {
if (err) {
console.error("Error:", err);
} else {
result = jsonData;
console.log(JSON.stringify(jsonData, null, 2));
}
});
```

## Contributions

Contributions are *very* welcomed, either on the documentation or on
Contributions are _very_ welcomed, either on the documentation or on
the code.

You may:

- report any [issue](https://github.com/julien-f/csv2json/issues)
you've encountered;
- fork and create a pull request.
- report any [issue](https://github.com/kunalburangi/csv2json/issues)
you've encountered;
- fork and create a pull request.

## Note

Thanks to @twilson63 for letting me use the *csv2json* name on [npm](https://www.npmjs.org/).
Thanks to @twilson63 for letting me use the _csv2json_ name on [npm](https://www.npmjs.org/).

## License

ISC © [Julien Fontanet](http://julien.isonoe.net)
[Kunal Burangi]
60 changes: 29 additions & 31 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,19 @@

'use strict'

var createReadStream = require('fs').createReadStream
var createWriteStream = require('fs').createWriteStream
var PassThrough = require('stream').PassThrough

var eventToPromise = require('promise-toolbox/fromEvent')
var minimist = require('minimist')
var pump = require('pump')

var csv2json = require('./')
var pkg = require('./package.json')

// ===================================================================

function createInputStream (path) {
return path === undefined || path === '-'
? process.stdin
: createReadStream(path)
}

function createOutputStream (path) {
if (path !== undefined && path !== '-') {
return createWriteStream(path)
return require('fs').createWriteStream(path)
}

// introduce a through stream because stdout is not a normal stream!
var stream = new PassThrough()
stream.pipe(process.stdout)
return stream
// If path is unspecified or a dash (“-”), use process.stdout
return process.stdout
}

// ===================================================================
Expand Down Expand Up @@ -71,22 +56,35 @@ function main (args) {
})

if (args.help) {
return usage
console.log(usage)
return
}

return eventToPromise(pump([
createInputStream(args._[0]),
csv2json({
dynamicTyping: args['dynamic-typing'],
separator: args.tsv ? '\t' : args.separator
}),
createOutputStream(args._[1])
]), 'finish')
}
exports = module.exports = main
const inputFilePath = args._[0]
const outputFilePath = args._[1]

// ===================================================================
if (!inputFilePath) {
console.error('Please provide an input file path.')
return
}

const outputStream = createOutputStream(outputFilePath)

csv2json(inputFilePath, { // Pass the file path
dynamicTyping: args['dynamic-typing'],
separator: args.tsv ? '\t' : args.separator
}, function (err, jsonArray) {
if (err) {
console.error('An error occurred:', err)
process.exit(1)
} else {
// Convert the jsonArray to JSON string and write to the output stream
outputStream.write(JSON.stringify(jsonArray, null, 2))
outputStream.end()
}
})
}

if (!module.parent) {
require('exec-promise')(exports)
main(process.argv.slice(2))
}
3 changes: 3 additions & 0 deletions example.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name,age,email
John,25,[email protected]
Jane,30,[email protected]
9 changes: 9 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var csv2json = require('./index2')

csv2json('example.csv', { dynamicTyping: true, separator: ',' }, function (err, jsonData) {
if (err) {
console.error('Error:', err)
} else {
console.log(JSON.stringify(jsonData, null, 2))
}
})
93 changes: 37 additions & 56 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,76 +1,57 @@
'use strict'

// ===================================================================

var fs = require('fs')
var parseCsv = require('csv-parser')
var pumpify = require('pumpify')
var through2 = require('through2')
var stripBomStream = require('strip-bom-stream')

// ===================================================================

var noop = Function.prototype

var hasOwn = Object.prototype.hasOwnProperty
/**
* Parses dynamic values in an object.
* @param {Object} data - The object containing dynamic values.
*/
function parseDynamic (data) {
var name, value
for (name in data) {
if (hasOwn.call(data, name)) {
value = data[name].toLowerCase()
for (var name in data) {
if (Object.prototype.hasOwnProperty.call(data, name)) {
var value = data[name].toLowerCase()
if (value === 'true') {
data[name] = true
} else if (value === 'false') {
data[name] = false
} else if (value !== '') {
value = +value
if (!isNaN(value)) {
data[name] = value
var numericValue = +value
if (!isNaN(numericValue)) {
data[name] = numericValue
}
}
}
}
}

function removeUndefinedProps (obj) {
Object.keys(obj).forEach(function (key) {
if (obj[key] === undefined) {
delete obj[key]
}
})
return obj
/**
* Converts CSV to JSON.
* @param {string} filePath - The path to the CSV file.
* @param {Object} options - The options for CSV to JSON conversion.
* @param {boolean} options.dynamicTyping - Whether to parse dynamic values.
* @param {string} options.separator - The separator used in the CSV.
* @param {function} callback - Callback function to handle the JSON data.
*/
function csv2json (filePath, options, callback) {
options = options || {}
var process = options.dynamicTyping ? parseDynamic : function () { }

var jsonArray = []
fs.createReadStream(filePath)
.pipe(parseCsv({ separator: options.separator }))
.on('data', function (data) {
process(data)
jsonArray.push(data)
})
.on('end', function () {
callback(null, jsonArray)
})
.on('error', function (error) {
callback(error)
})
}

function csv2json (opts) {
opts || (opts = {})

var process = opts.dynamicTyping
? parseDynamic
: noop
// Example usage:

return pumpify([
stripBomStream(),
parseCsv(removeUndefinedProps({
separator: opts.separator
})),
(function () {
var notFirst = false
var proxy = through2.obj(function (chunk, _, done) {
if (notFirst) {
this.push(',\n')
}
notFirst = true

process(chunk)

done(null, JSON.stringify(chunk))
}, function (done) {
this.push('\n]\n')
done()
})
proxy.push('[\n')

return proxy
})()
])
}
exports = module.exports = csv2json
Loading