Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add `to.array` function

  • Loading branch information...
commit d9f0461d54eef8a0cadaa2693211a611324d4758 1 parent 2570d4b
@wdavidw authored
Showing with 73 additions and 4 deletions.
  1. +30 −2 lib/to.js
  2. +27 −2 src/to.coffee
  3. +16 −0 test/to.coffee
View
32 lib/to.js
@@ -118,8 +118,8 @@ module.exports = function(csv) {
Provide the output string to a callback.
csv()
- .from( '"1","2","3","4"\n"a","b","c","d"' )
- .to( function(data, count){} )
+ .from( '"1","2","3"\n"a","b","c"' )
+ .to.string( function(data, count){} )
Callback is called with 2 arguments:
* data Stringify CSV string
@@ -198,5 +198,33 @@ module.exports = function(csv) {
csv.to.stream(stream, null);
return csv;
};
+ /*
+
+ `to.array(path, [options])`
+ --------------------------
+
+ Provide the output string to a callback.
+
+ csv()
+ .from( '"1","2","3"\n"a","b","c"' )
+ .to.array( function(data, count){} )
+
+ Callback is called with 2 arguments:
+ * data Stringify CSV string
+ * count Number of stringified records
+ */
+
+ to.array = function(callback, options) {
+ var records;
+ this.options(options);
+ records = [];
+ csv.on('record', function(record) {
+ return records.push(record);
+ });
+ csv.on('end', function() {
+ return callback(records, csv.state.countWriten);
+ });
+ return csv;
+ };
return to;
};
View
29 src/to.coffee
@@ -103,8 +103,8 @@ module.exports = (csv) ->
Provide the output string to a callback.
csv()
- .from( '"1","2","3","4"\n"a","b","c","d"' )
- .to( function(data, count){} )
+ .from( '"1","2","3"\n"a","b","c"' )
+ .to.string( function(data, count){} )
Callback is called with 2 arguments:
* data Stringify CSV string
@@ -175,6 +175,31 @@ module.exports = (csv) ->
stream = fs.createWriteStream path, options
csv.to.stream stream, null
csv
+
+ ###
+
+ `to.array(path, [options])`
+ --------------------------
+
+ Provide the output string to a callback.
+
+ csv()
+ .from( '"1","2","3"\n"a","b","c"' )
+ .to.array( function(data, count){} )
+
+ Callback is called with 2 arguments:
+ * data Stringify CSV string
+ * count Number of stringified records
+
+ ###
+ to.array = (callback, options) ->
+ @options options
+ records = []
+ csv.on 'record', (record) ->
+ records.push record
+ csv.on 'end', ->
+ callback records, csv.state.countWriten
+ csv
to
View
16 test/to.coffee
@@ -74,6 +74,22 @@ describe 'to', ->
result.should.eql expect
fs.unlink "#{__dirname}/fromto/string_to_stream.tmp", next
+ describe 'array', ->
+
+ it 'should output an array', (next) ->
+ data = """
+ 20322051544,1979.0,8.8017226E7,ABC,45,2000-01-01
+ 28392898392,1974.0,8.8392926E7,DEF,23,2050-11-27
+ """
+ csv()
+ .from.string(data)
+ .to.array (data) ->
+ data.should.eql [
+ ['20322051544','1979.0','8.8017226E7','ABC','45','2000-01-01']
+ ['28392898392','1974.0','8.8392926E7','DEF','23','2050-11-27']
+ ]
+ next()
+
describe 'end', ->
it 'should call the end function', (next) ->
Please sign in to comment.
Something went wrong with that request. Please try again.