Skip to content

Commit e35d0ef

Browse files
committed
Standardize JavaScript code for tests
1 parent 8d27fa4 commit e35d0ef

File tree

2 files changed

+24
-31
lines changed

2 files changed

+24
-31
lines changed

csv2json.js

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,24 @@
1-
module.exports = function(file) {
2-
var fs = require('fs');
3-
var path = require('path');
4-
5-
if (path.extname(file) != '.csv') {
6-
console.log("Invalid file. Files with extension .csv only.")
7-
} else {
8-
var data = fs.readFileSync(file);
9-
return convertToJson(data);
10-
}
11-
12-
function convertToJson(data) {
13-
var jsonData = [];
14-
15-
var formattedData = data.toString('utf8').split('\r\n');
16-
var keys = formattedData[0].split('\t');
17-
18-
for (var i = 1; i < formattedData.length; i++) {
19-
var row = formattedData[i].split('\t');
20-
var obj = {};
21-
for (var j = 0; j < row.length; j++) {
22-
obj[keys[j]] = row[j];
23-
}
24-
25-
jsonData.push(obj);
26-
}
27-
28-
return JSON.stringify(jsonData);
1+
module.exports = function (file) {
2+
var fs = require('fs')
3+
var path = require('path')
4+
if (path.extname(file) !== '.csv') {
5+
console.log('Invalid file. Files with extension .csv only.')
6+
} else {
7+
var data = fs.readFileSync(file)
8+
return convertToJson(data)
9+
}
10+
function convertToJson (data) {
11+
var jsonData = []
12+
var formattedData = data.toString('utf8').split('\r\n')
13+
var keys = formattedData[0].split('\t')
14+
for (var i = 1; i < formattedData.length; i++) {
15+
var row = formattedData[i].split('\t')
16+
var obj = {}
17+
for (var j = 0; j < row.length; j++) {
18+
obj[keys[j]] = row[j]
19+
}
20+
jsonData.push(obj)
2921
}
22+
return JSON.stringify(jsonData)
23+
}
3024
}

test/test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
var csv2json = require('../csv2json');
2-
3-
console.log(csv2json('test/sample.csv'));
1+
var csv2json = require('../csv2json')
2+
console.log(csv2json('test/sample.csv'))

0 commit comments

Comments
 (0)