New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

gulp-csvtojson

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gulp-csvtojson - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

73

index.js
'use strict';
var path = require('path');
var gutil = require('gulp-util');
var through = require('through2');
var Converter=require("csvtojson").core.Converter;
var path = require('path');
var Converter = require("csvtojson").Converter;
var processStream = function(file,options,cb) {
var csvConverter = new Converter(options);
file.contents = file.contents.pipe(csvConverter);
cb();
};
var processBuffer = function(file,options,cb) {
var csvConverter = new Converter(options);
csvConverter.fromString(file.contents.toString(), function(err, jsonObj) {
if (err) {
return cb(err);
}
var output = JSON.stringify(jsonObj);
file.contents = new Buffer(output);
cb();
});
};
module.exports = function (options) {
if (typeof(options) === "undefined") {
options = { genjs: false };
options = { };
}
// if (!options.foo) {
// throw new gutil.PluginError('gulp-csvtojson', '`foo` required');
// }
return through.obj(function (file, enc, cb) {
var self = this;
if (file.isNull()) {
cb(null, file);
return;
return cb(null, file);
}
try {
var done = function(err) {
if (err) { throw err; }
file.path = gutil.replaceExtension(file.path,'.json');
self.push(file);
return cb();
};
if (file.isStream()) {
cb(new gutil.PluginError('gulp-csvtojson', 'Streaming not supported'));
return;
}
if (file.isStream()) {
return processStream(file,options,done);
}
try {
if (file.isBuffer()) {
return processBuffer(file,options,done);
}
var csvConverter = new Converter(options);
var gulpobj = this;
csvConverter.fromString(file.contents.toString(), function(err, jsonObj) {
var output = JSON.stringify(jsonObj);
var variablename = path.basename(file.path, path.extname(file.path));
file.path = file.path.slice(0, file.path.indexOf('.')) + ".json"
if (typeof(options.globalvariable) !== "undefined" && options.globalvariable !== null) {
variablename = options.globalvariable;
}
if (options.genjs) {
file.path = file.path.slice(0, file.path.indexOf('.')) + ".js"
output = variablename + "=" + output + ";";
}
file.contents = new Buffer(output);
gulpobj.push(file);
cb();
});
} catch (err) {

@@ -52,4 +60,3 @@ this.emit('error', new gutil.PluginError('gulp-csvtojson', err));

}
});
};
{
"name": "gulp-csvtojson",
"version": "0.1.0",
"version": "0.2.0",
"description": "convert csv to json",

@@ -28,9 +28,11 @@ "license": "MIT",

"dependencies": {
"csvtojson": "^0.3.21",
"gulp-util": "^3.0.4",
"through2": "^0.6.5"
"csvtojson": "^0.5.14",
"gulp-util": "^3.0.7",
"through2": "^2.0.1",
"vinyl": "^1.1.1"
},
"devDependencies": {
"concat-stream": "^1.5.1",
"mocha": "*"
}
}

@@ -15,2 +15,4 @@ # gulp-csvtojson [![Build Status](https://travis-ci.org/wizicer/gulp-csvtojson.svg?branch=master)](https://travis-ci.org/wizicer/gulp-csvtojson)

Here is the code snippet for general usage which generate json for you.
```js

@@ -22,3 +24,3 @@ var gulp = require('gulp');

return gulp.src('src/file.csv')
.pipe(csvtojson({genjs: true}))
.pipe(csvtojson({ toArrayString: true }))
.pipe(gulp.dest('dist'));

@@ -28,24 +30,31 @@ });

Here is the code snippet for generating js file.
## API
```js
var gulp = require('gulp');
var csvtojson = require('gulp-csvtojson');
var insert = require('gulp-insert');
var ext_replace = require('gulp-ext-replace');
### csvtojson(options)
gulp.task('default', function () {
return gulp.src('src/file.csv')
.pipe(csvtojson({ toArrayString: true }))
.pipe(insert.prepend('var anyVariable = '))
.pipe(insert.append(';'))
.pipe(ext_replace('.js'))
.pipe(gulp.dest('dist'));
});
```
#### options
## Options
##### genjs
Refer to [this list](https://github.com/Keyang/node-csvtojson#params) for full options.
Type: `bool`
Default: `false`
**Note**: to get a valid json file, you need set `toArrayString` to `true`.
When set true, output `.js` file with global variable assignment.
## API
##### globalvariable
Since v0.2.0, API no longer supported, to achieve the same result, please adapt your code according
to the second code snippet.
Type: `string`
Default: `null`
The name which global variable will be assigned the json object.
Only available when `genjs` is set to `true`.
## License

@@ -52,0 +61,0 @@

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc