Skip to content

Commit

Permalink
v.1.0.5 ➕
Browse files Browse the repository at this point in the history
  • Loading branch information
klm127 committed Jul 25, 2021
1 parent cba5db4 commit 3b8d7bb
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/out
/node_modules
notes
notes
.vscode
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# census-csv-parser

_v 1.0.4_
_v 1.0.5_

[https://github.com/klm127/census-csv-parser](https://github.com/klm127/census-csv-parser)

Expand Down
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
2021/07/25 - v 1.0.5 -

Added mergeToHeader function to Parser. This takes a row or column (depending on how you set your headers) and merges into the header column as an array, allowing for the nesting of data.

2021/07/14 - v 1.0.4 -

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "census-csv-parser",
"version": "1.0.4",
"version": "1.0.5",
"description": "tool for parsing census csvs into .json objects",
"homepage": "https://quaffingcode.com",
"main": "parser.js",
Expand Down
37 changes: 37 additions & 0 deletions parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,43 @@ class Parser {
this.columnHeaders = propArr;
}
}
/**
* Merges another row or column into the header column so that header column becomes an array, which causes the higher indexed elements of the array to nest inside the lower indexed elements of the array in the final output json object.
*
* @param {number} index The index to merge to the header(not counting the header itself in the index). If headers set to row, merges that index row. If set to col, merges that column
*/
mergeToHeader(index) {
if(this.propArr == 'COL') {
let mergeRow = this.data[index];
for(let i = 0; i < mergeRow.length; i++) {
let header = this.columnHeaders[i];
let appendValue = mergeRow[i];
if(header instanceof Array) {
header.push(appendValue);
}
else {
this.columnHeaders[i] = [header, appendValue];
}
}
this.data.splice(index,1)
this.rowHeaders.splice(index,1)
}
else {
for(let i = 0; i < this.data.length; i++) {
let mergeValue = this.data[i][index];
let previousHeader = this.rowHeaders[i];
if(previousHeader instanceof Array) {
previousHeader.push(mergeValue);
}
else {
this.rowHeaders[i] = [previousHeader, mergeValue];
}
this.data[i].splice(index,1);
}
this.columnHeaders.splice(index,1);

}
}
/**
* Maps data to an object. The object will contain a property for each value not in the selected props array. For each array in the selected props array, a chain of sub-properties will be created. Metadata, if extant, will be added as properties to the mother object.
* So if you have this data:
Expand Down
52 changes: 52 additions & 0 deletions tests/testparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,58 @@ try {
}, 'Chopping rows with header column worked as expected')
log('Chopping rows on header regex worked as expected','green')

// test merging a column into the row headers as an array for nesting
testArray = [
["County","Industry","Number of Firms","Number of Employees"],
["Mercer","Automobile Manufacturing", 3, 112],
["Mercer","Wood Product Manufacturing", 12, 195],
["Dodd","Food Service", 50, 220],
["Dodd","Automobile Manufacturing", 1, 10]
];
parser = new Parser(testArray);
parser.setHeaders(0,0)
parser.setProps('ROW');
parser.mergeToHeader(0);
let out = parser.mapProps();
assert.deepStrictEqual(out, {
overlapHeader: 'County',
'Number of Firms': {
Mercer: { 'Automobile Manufacturing': 3, 'Wood Product Manufacturing': 12 },
Dodd: { 'Food Service': 50, 'Automobile Manufacturing': 1 }
},
'Number of Employees': {
Mercer: {
'Automobile Manufacturing': 112,
'Wood Product Manufacturing': 195
},
Dodd: { 'Food Service': 220, 'Automobile Manufacturing': 10 }
}
},'Merging a col for array-style mapping did not work as expected');
log('Merging a column into the row headers worked as expected','green')

testArray = [
["County","Franklin","Preston","Franklin","Preston","Preston"],
["Industry","Financial Services","Financial Services","Auto Repair","Auto Repair","Consulting"],
["Tax Revenue (mil)",3,8,0.4,0.3,2],
["Employees", 900, 1950, 320, 640, 50]
]
parser = new Parser(testArray);
parser.setHeaders(0,0);
parser.setProps('COL');
parser.mergeToHeader(0);
out = parser.mapProps();
assert.deepStrictEqual(out, {
overlapHeader: 'County',
'Tax Revenue (mil)': {
Franklin: { 'Financial Services': 3, 'Auto Repair': 0.4 },
Preston: { 'Financial Services': 8, 'Auto Repair': 0.3, Consulting: 2 }
},
Employees: {
Franklin: { 'Financial Services': 900, 'Auto Repair': 320 },
Preston: { 'Financial Services': 1950, 'Auto Repair': 640, Consulting: 50 }
}
}, 'Merging a row for array-style mapping did not work as expected');
log('Merging a row into the column headers worked as expected','green')



Expand Down

0 comments on commit 3b8d7bb

Please sign in to comment.