+27
-5
@@ -10,7 +10,7 @@ (function(global) { | ||
| var Iconv = require('iconv').Iconv; | ||
| var conv = new Iconv('cp932','utf-8'); | ||
| var conv = null; | ||
| var _ = require('lodash'); | ||
| var app = { | ||
| debug : false, | ||
| charset : 'cp932', | ||
| pretty : false, | ||
@@ -28,2 +28,19 @@ delimitter : ':', | ||
| } | ||
| try { | ||
| // pretty | ||
| if( _.isString( option.charset ) && option.charset !== '' ) { | ||
| if( /^shift_jis$/i.test(option.charset) || | ||
| /^shift.jis$/i.test( option.charset ) || | ||
| /^cp932$/.test( option.charset ) ) { | ||
| conv = new Iconv(option.charset,'utf-8'); | ||
| } else if( /^utf-8$/i.test(option.charset) ) { | ||
| conv = null; | ||
| } else { | ||
| conv = new Iconv('cp932','utf-8'); | ||
| } | ||
| } | ||
| } catch(e) { | ||
| console.log("faild.iconv charset setting."); | ||
| } | ||
@@ -119,3 +136,3 @@ // pretty | ||
| }); | ||
| if( app.debug ) console.log( createJson ); | ||
@@ -137,4 +154,9 @@ | ||
| fs.readFile( path, function(err, sjisBuf) { | ||
| var buf = conv.convert(sjisBuf); | ||
| fs.readFile( path, function(err, _buf) { | ||
| var buf; | ||
| if( conv !== null ) { | ||
| buf = conv.convert( _buf ); | ||
| } else { | ||
| buf = _buf; | ||
| } | ||
| if( app.debug ) console.log( 'CSV Loading:' + path + ' *=-'); | ||
@@ -141,0 +163,0 @@ csv.parse(buf.toString(),{comment:'#'}, function(err, data) { |
+1
-1
| { | ||
| "name": "csv2jsonic", | ||
| "version": "1.0.1", | ||
| "version": "1.0.2", | ||
| "description": "This module returns the CSV file in Json file or object.", | ||
@@ -5,0 +5,0 @@ "author": "Jun Fujimura <sv.junic@gmail.com> (http://sv.junic.jp)", |
+6
-0
@@ -65,2 +65,8 @@ [](https://travis-ci.org/svjunic/csv2jsonic) | ||
| ### charset | ||
| Type: `String` | ||
| Default: cp932 // shift_jis | ||
| Can be utf-8. | ||
| ### pretty | ||
@@ -67,0 +73,0 @@ Type: `Boolean` |
@@ -1,2 +0,2 @@ | ||
| user,field1,field2:field2Child1,field2:field2Child2,field3:field3Child:field3ChildChild | ||
| sv.junic1,field1-1Value,field2-1Value,field2-2Value,100 | ||
| "user","field1","field2:field2Child1","field2:field2Child2","field3:field3Child:field3ChildChild" | ||
| "sv.junic1","������","field2-1Value","field2-2Value","��������" |
@@ -1,1 +0,1 @@ | ||
| [{"user":"sv.junic1","field1":"field1-1Value","field2":{"field2Child1":"field2-1Value","field2Child2":"field2-2Value"},"field3":{"field3Child":{"field3ChildChild":"100"}}}] | ||
| [{"user":"sv.junic1","field1":"もげた","field2":{"field2Child1":"field2-1Value","field2Child2":"field2-2Value"},"field3":{"field3Child":{"field3ChildChild":"もげぞう"}}}] |
+84
-24
@@ -32,24 +32,46 @@ var assert = require('assert'); | ||
| output = './test/json/fileformat1.json'; | ||
| csv2jsonic.setup( { outputJsonPath: output } ); | ||
| csv2jsonic.setup( { outputJsonPath: output, charset: "utf-8" } ); | ||
| csv2jsonic.loadCSV( input ) | ||
| .then( function ( data ) { | ||
| assert( data[0].user === 'sv.junic1' ); | ||
| assert( data[0].field1 === 'field1-1Value' ); | ||
| assert( data[0].field2.field2Child1 === 'field2-1Value' ); | ||
| assert( data[0].field2.field2Child2 === 'field2-2Value' ); | ||
| assert( data[0].field3.field3Child.field3ChildChild === '100' ); | ||
| var success = { | ||
| user : 'sv.junic1', | ||
| field1 : 'field1-1Value', | ||
| field2 : { field2Child1: 'field2-1Value', | ||
| field2Child2: 'field2-2Value' | ||
| }, | ||
| field3 : { | ||
| field3Child: { | ||
| field3ChildChild: '100', | ||
| } | ||
| } | ||
| }; | ||
| assert.deepEqual( data[0], success ); | ||
| filecheck( output, function(){ done(); } ) | ||
| }); | ||
| }); | ||
| it('Shift-JIS', function( done ) { | ||
| it('cp932', function( done ) { | ||
| input = './test/csv/fileformat2.csv'; | ||
| output = './test/json/fileformat2.json'; | ||
| csv2jsonic.setup( { outputJsonPath: output } ); | ||
| csv2jsonic.setup( { outputJsonPath: output, charset: "cp932" } ); | ||
| csv2jsonic.loadCSV( input ) | ||
| .then( function ( data ) { | ||
| assert( data[0].user === 'sv.junic1' ); | ||
| assert( data[0].field1 === 'field1-1Value' ); | ||
| assert( data[0].field2.field2Child1 === 'field2-1Value' ); | ||
| assert( data[0].field2.field2Child2 === 'field2-2Value' ); | ||
| assert( data[0].field3.field3Child.field3ChildChild === '100' ); | ||
| var success = { | ||
| user : 'sv.junic1', | ||
| field1 : 'もげた', | ||
| field2 : { field2Child1: 'field2-1Value', | ||
| field2Child2: 'field2-2Value' | ||
| }, | ||
| field3 : { | ||
| field3Child: { | ||
| field3ChildChild: 'もげぞう', | ||
| } | ||
| } | ||
| }; | ||
| assert.deepEqual( data[0], success ); | ||
| filecheck( output, function(){ done(); } ) | ||
@@ -69,7 +91,18 @@ }) | ||
| .then( function ( data ) { | ||
| assert( data[0].user === 'sv.junic1' ); | ||
| assert( data[0].field1 === 'field1-1Value' ); | ||
| assert( data[0].field2.field2Child1 === 'field2-1Value' ); | ||
| assert( data[0].field2.field2Child2 === 'field2-2Value' ); | ||
| assert( data[0].field3.field3Child.field3ChildChild === '100' ); | ||
| var success = { | ||
| user : 'sv.junic1', | ||
| field1 : 'field1-1Value', | ||
| field2 : { field2Child1: 'field2-1Value', | ||
| field2Child2: 'field2-2Value' | ||
| }, | ||
| field3 : { | ||
| field3Child: { | ||
| field3ChildChild: '100', | ||
| } | ||
| } | ||
| }; | ||
| assert.deepEqual( data[0], success ); | ||
| filecheck( output, function(){ done(); } ) | ||
@@ -84,7 +117,18 @@ }) | ||
| .then( function ( data ) { | ||
| assert( data[0].user === 'sv.junic1' ); | ||
| assert( data[0].field1 === 'field1-1Value' ); | ||
| assert( data[0].field2.field2Child1 === 'field2-1Value' ); | ||
| assert( data[0].field2.field2Child2 === 'field2-2Value' ); | ||
| assert( data[0].field3.field3Child.field3ChildChild === '100' ); | ||
| var success = { | ||
| user : 'sv.junic1', | ||
| field1 : 'field1-1Value', | ||
| field2 : { field2Child1: 'field2-1Value', | ||
| field2Child2: 'field2-2Value' | ||
| }, | ||
| field3 : { | ||
| field3Child: { | ||
| field3ChildChild: '100', | ||
| } | ||
| } | ||
| }; | ||
| assert.deepEqual( data[0], success ); | ||
| filecheck( output, function(){ done(); } ) | ||
@@ -108,3 +152,19 @@ }) | ||
| csv2jsonic.setup( { pretty:true, delimitter:':' } ); | ||
| csv2jsonic.convert( testdata ); | ||
| var convarted = csv2jsonic.convert( testdata ); | ||
| var success = { | ||
| user : 'sv.junic1', | ||
| field1 : 'field1-1Value', | ||
| field2 : { field2Child1: 'field2-1Value', | ||
| field2Child2: 'field2-2Value' | ||
| }, | ||
| field3 : { | ||
| field3Child: { | ||
| field3ChildChild: '100', | ||
| } | ||
| } | ||
| }; | ||
| assert.deepEqual( convarted[0], success ); | ||
| }); | ||
@@ -111,0 +171,0 @@ }); |
19535
8.5%377
21.61%122
5.17%