json-to-table
Advanced tools
Comparing version 2.0.0 to 2.1.0
17
index.js
@@ -17,2 +17,3 @@ var traverse = require('traverse'), | ||
function(headers, value) { | ||
var self = this; | ||
if (this.notRoot && _.isArray(value)) { | ||
@@ -27,8 +28,11 @@ if (options.includeCollectionLength) { | ||
if (this.isLeaf) { | ||
this.path = _.map(this.path, function(level) { | ||
if (level.indexOf('.') > -1 && self.level > 2) { // If a leaf contains a dot in it, then surround the whole path with ticks | ||
level = '`' + level + '`'; | ||
} | ||
return level; | ||
}); | ||
if (!(_.isPlainObject(value) && _.keys(value).length === 0)) { // Check against empty objects. Don't treat these paths as a valid header value. | ||
headers[_.rest(this.path).join('.')] = true; | ||
} | ||
// Make sure that we don't include empty objects as valid headers | ||
// There are good reasons behind this that deal with projections, and non-homogenous objects in a collection. | ||
} | ||
@@ -46,2 +50,9 @@ return headers; | ||
} | ||
if (header.indexOf('`') > -1) { // One of those special cases where a path is nested, AND has a dot in the name. | ||
var parts = header.split('.`'), | ||
head = parts[0].replace(/\`/g, ''), | ||
tail = parts[1].replace(/\`/g, ''); | ||
return dottie.get(doc, head, {})[tail]; | ||
} | ||
return dottie.get(doc, header, options.defaultVal); | ||
@@ -48,0 +59,0 @@ }) |
{ | ||
"name": "json-to-table", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Convert an array of Objects into a table format", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -302,2 +302,26 @@ var assert = require('chai').assert, | ||
}); | ||
it('Should be able to resolve the path or key even with multiple dots in the name.', function() { | ||
var jsonData = [ | ||
{ | ||
firstName: 'Scott', | ||
contact: { | ||
'phone.number': '801-999-8626' | ||
} | ||
}, | ||
{ | ||
firstName: 'Burt', | ||
contact: { | ||
'home.address': 'The Pentagon' | ||
} | ||
} | ||
]; | ||
var tableData = jsonToTable(jsonData, { defaultVal: undefined, checkKeyBeforePath: true }), | ||
expectedTableData = [ [ 'firstName', 'contact.`phone.number`', 'contact.`home.address`' ], | ||
[ 'Scott', '801-999-8626', undefined ], | ||
[ 'Burt', undefined, 'The Pentagon' ] ]; | ||
assert(_.isEqual(tableData, expectedTableData)); | ||
}); | ||
}); |
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
17625
349