csv-builder
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -192,3 +192,3 @@ /** | ||
for (var i = 0; i < hlen; i++) { | ||
row[i] = obj[(_constraints[_headers[i]] || _headers[i])] || ''; | ||
row[i] = _.get(obj, (_constraints[_headers[i]] || _headers[i]), ''); | ||
} | ||
@@ -195,0 +195,0 @@ return row; |
{ | ||
"name": "csv-builder", | ||
"author": "Nick Pisacane <pisacanen@gmail.com> (http://nickpisacane.com)", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Create csv formated streams from Arrays of Objects.", | ||
@@ -24,4 +24,8 @@ "license": "MIT", | ||
"scripts": { | ||
"test": "node ./test/main.test.js" | ||
"test": "mocha test/**/*.test.js" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^3.2.0", | ||
"testable-stream": "0.0.1" | ||
} | ||
} |
# Csvbuilder | ||
![travis](https://travis-ci.org/nickpisacane/CsvBuilder.svg?branch=master) | ||
Create csv formated streams from Arrays of Objects. CsvBuilder is one of many Csv stream/generator implementations | ||
on npm. The goal of CsvBuilder is to create Schema's for csv output and let the consumer spawn as many streams | ||
as needed from a single instance to maintain a specific format. This means the user gets control of the headers, the | ||
order of the headers, how the headers correspond to consumed objects, virtual properties, value delimiters, and line | ||
order of the headers, how the headers correspond to consumed objects, virtual properties, value delimiters, and line | ||
terminators. | ||
@@ -13,9 +15,14 @@ | ||
// assuming we have an array of objects that's | ||
// in the format of {fullname: String, email: String, zip: Number} | ||
// but we need csv formated as such: Firstname, Lastname, Email | ||
// Assuming data takes the following form | ||
var data = [ | ||
name: 'Example User', | ||
email: 'example@gmail.com', | ||
meta: { | ||
active: true | ||
} | ||
] | ||
var usersBuilder = new CsvBuilder({ | ||
// define headers and order of headers | ||
headers: 'Firstname Lastname Email', | ||
headers: 'Firstname Lastname Email Active', | ||
// define object to header correspondance | ||
@@ -26,14 +33,16 @@ constraints: { | ||
// correspond with a virtual property | ||
'Lastname': 'lastname' | ||
'Lastname': 'lastname', | ||
// Access a nested property | ||
'Active': 'meta.active' | ||
} | ||
}) | ||
// create virtual 'Firstname' | ||
.virtual('Firstname', function(obj) { | ||
return obj.name.split(' ')[0]; | ||
}) | ||
// virtual properties are treated like any propery, | ||
// if it is not defined in the headers, it still needs a constraint | ||
.virtual('lastname', function(obj) { | ||
return obj.name.split(' ')[1]; | ||
}); | ||
// create virtual 'Firstname' | ||
.virtual('Firstname', function(obj) { | ||
return obj.name.split(' ')[0]; | ||
}) | ||
// virtual properties are treated like any propery, | ||
// if it is not defined in the headers, it still needs a constraint | ||
.virtual('lastname', function(obj) { | ||
return obj.name.split(' ')[1]; | ||
}); | ||
@@ -83,5 +92,5 @@ // From the `usersBuilder` instance we can now spawn readable or tranform streams. | ||
Create's a readable stream and consumes the payload. | ||
* payload Array<Object> | ||
* payload Array<Object> | ||
##### CsvBuilder#createTransformStream() | ||
Create's a transform stream. The stream expects either Objects or JSON. |
@@ -5,2 +5,3 @@ /** | ||
var path = require('path'); | ||
var CsvBuilder = require('../lib/csvBuilder'); | ||
@@ -10,2 +11,3 @@ var fs = require('fs'); | ||
var Stream = require('stream'); | ||
var TestStream = require('testable-stream') | ||
@@ -15,3 +17,3 @@ // test data | ||
// expected csv value | ||
var expected = fs.readFileSync('./expected.csv'); | ||
var expected = fs.readFileSync(path.join(__dirname, 'expected.csv')); | ||
@@ -86,2 +88,37 @@ // test instance | ||
it('supports nested properties', function (done) { | ||
var data = [ | ||
{ | ||
name: 'Test', | ||
age: 48, | ||
email: 'test@gmail.com', | ||
meta: { | ||
active: true | ||
} | ||
} | ||
] | ||
var expected = [ | ||
'Name,Age,Email,Active', | ||
'Test,48,test@gmail.com,true\n' | ||
].join('\n') | ||
var builder = new CsvBuilder({ | ||
headers: 'Name Age Email Active', | ||
constraints: { | ||
Name: 'name', | ||
Age: 'age', | ||
Email: 'email', | ||
Active: 'meta.active' | ||
} | ||
}) | ||
builder.createReadStream(data) | ||
.pipe(TestStream()) | ||
.on('testable', function (data) { | ||
assert.equal(data.toString(), expected) | ||
done() | ||
}) | ||
}) | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17280
10
389
94
2