Skip to content

Commit

Permalink
Bug #6, re-implement objToCsv() in statistics Javascript [iet:10277…
Browse files Browse the repository at this point in the history
…034]
  • Loading branch information
nfreear committed Dec 11, 2017
1 parent 4fd2a39 commit acefb95
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ before_install:
# "$ npm --version // ERROR: npm is known not to run on Node.js v4.4.7"
#- nvm install v4.4
- nvm install v8.1
- npm install npm -g
- npm -v

install:
Expand All @@ -30,4 +29,4 @@ script:
after_script:
- ls -al

#End.
# End.
35 changes: 34 additions & 1 deletion js/src/tesla-statistics.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ module.exports = function ($) {

var $page = $('#page-local-tesla-views-tesla_results');
var $rows = $page.find('#page-content table tbody tr');
var $heading = $page.find('#page-content table thead').find('th:nth-child( 2 )');
var counts = {
instrument: $heading.text(),
date: (new Date()).toISOString().replace(/\.\d{3}/, ''),
total_rows: $rows.length,
with_number: 0,
just_zero: 0,
Expand Down Expand Up @@ -38,5 +41,35 @@ module.exports = function ($) {
};

function objToCsv (obj) {
return JSON.stringify(obj, null, 2).replace(/:/g, ',').replace(/_/g, ' ');
var res = iterateObject(obj);
return "\n" + JSON.stringify(res.header) + "\n" + JSON.stringify(res.value);
// Was: return JSON.stringify(obj, null, 2).replace(/:/g, ',').replace(/_/g, ' ');
}

// https://stackoverflow.com/questions/11257062/converting-json-object-to-csv-format-in-javascript
// https://jsfiddle.net/dhou6y3o/
function iterateObject(obj) {
var value = '', header = '';
for (name in obj) {
if (obj.hasOwnProperty(name)) {
if (isObject(obj[name])) {
var out = iterateObject(obj[name]);
value += out.value;
header += out.header;
} else {
value += removeNewLine(obj[name]) + '; ';
header += name + '; ';
}
}
}
return {
"value":value,
"header":header
};
}
function isObject(obj) {
return (typeof obj === 'object');
}
function removeNewLine(item) {
return item.toString().replace(/(\r\n|\n|\r)/gm,"");
}

0 comments on commit acefb95

Please sign in to comment.