Skip to content

Commit

Permalink
Merge pull request #22 from IWSLLC/this
Browse files Browse the repository at this point in the history
Adding  keyword to support multi-field filters.
  • Loading branch information
nathanb committed Apr 2, 2016
2 parents e152fad + 8355137 commit 40862a0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: node_js
node_js:
- '5.0'
- '4.2'
- '5'
- '4'
- '0.10'
script: "mocha"
cache:
Expand Down
18 changes: 12 additions & 6 deletions exporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ exporter.prototype.prepValue = function(arg, forceQuoted) {

exporter.prototype.getHeaderRow = function() {
var self = this
var header = _.reduce(this.options.fields, function(line, field) {
var header = _.reduce(self.options.fields, function(line, field) {
var label = field.label || field.field
if (line === 'START') {
line = '';
} else {
line += this.fieldSeparator
line += self.fieldSeparator
}
line += self.prepValue(label)
return line
}, 'START', this)
}, 'START')
header += '\r\n'
return header
}
Expand All @@ -68,7 +68,7 @@ exporter.prototype.getBodyRow = function(data) {
if (line === 'START') {
line = '';
} else {
line += this.fieldSeparator
line += self.fieldSeparator
}
var val = self.getValue(data, field.name)
if (field.filter) {
Expand All @@ -79,7 +79,7 @@ exporter.prototype.getBodyRow = function(data) {
line += self.prepValue(val.toString(), quoted)
}
return line
}, 'START', this)
}, 'START', self)

row += '\r\n'
return row
Expand All @@ -89,17 +89,23 @@ exporter.prototype.getValue = function(data, arg) {
var args = arg.split('.')
if (args.length > 0)
return this.getValueIx(data, args, 0)
return data[args[0]];
return ""
}

exporter.prototype.getValueIx = function(data, args, ix) {
if (!data)
return ''

//for filtered fields using the whole row as a source.
//`this` is a keyword here; hoping not to conflict with existing fields.
if (args[0] === "this")
return data

var val = data[args[ix]]
if (typeof val === 'undefined')
return ''

//walk the dot-notation recursively to get the remaining values.
if ((args.length-1) > ix)
return this.getValueIx(val, args, ix+1);

Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "json-csv",
"version": "1.2.0",
"version": "1.3.0",
"description": "Export a richly structured, JSON array to CSV",
"homepage": "https://github.com/IWSLLC/json-csv",
"author": "Nathan Bridgewater <info@iws.io> (http://iws.io/)",
Expand All @@ -19,8 +19,8 @@
},
"devDependencies": {
"coffee-script": "^1.10.0",
"mocha": "^2.3.3",
"should": "^7.1.1"
"mocha": "^2.4.5",
"should": "^8.3.0"
},
"bugs": {
"url": "https://github.com/IWSLLC/json-csv/issues"
Expand All @@ -31,7 +31,7 @@
"dependencies": {
"concat-stream": "^1.5.1",
"event-stream": "^3.3.2",
"lodash": "^3.10.1"
"lodash": "^4.7.0"
},
"scripts": {
"test": "mocha"
Expand Down
21 changes: 21 additions & 0 deletions test/issue-21.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
jsoncsv = require '../index'
should = require "should"

describe "Issue 21", ->
describe "When exporting a column, concatenated from two source fields", ->
before (done) ->
@items = [{a: "first", b: "second"}, {a: "third", b:"fourth"}, {a: "fifth", b: "sixth"}]
jsoncsv.csvBuffered @items, {
fields: [
{
name: "this"
label: 'c'
filter: (v) ->
return "#{v?.a}#{v?.b}"
}
]}, (err, csv) =>
@csv = csv
@err = err
done()

it "should export concatenated a and b", -> @csv.should.equal "c\r\nfirstsecond\r\nthirdfourth\r\nfifthsixth\r\n"

0 comments on commit 40862a0

Please sign in to comment.