Commit e1da2149 authored by Milan Wikarski's avatar Milan Wikarski
Browse files

Added tests and readCSV method

parent 5366e57c
node_modules
\ No newline at end of file
{
"singleQuote": true,
"printWidth": 80,
"tabWidth": 2,
"useTabs": false
}
......@@ -52,17 +52,60 @@ function readJSON(filename, silent = false) {
read(filename, 'utf8', false)
.then(data => {
try {
return JSON.parse(data);
return resolve(JSON.parse(data));
} catch (err) {
silent ? resolve({}) : reject(err);
return silent ? resolve({}) : reject(err);
}
})
.catch(err => (silent ? resolve({}) : reject(err)));
});
}
/**
*
* @param {string} filename
* @param {string} [separator = ',']
* @param {Array.<string>} [header = null]
* @param {boolean} silent
* @returns {Promise.<Array.<Object>>} data
*/
function readCSV(filename, separator = ',', header = null, silent = false) {
return new Promise((resolve, reject) => {
read(filename, 'utf8', false)
.then(data => {
try {
const res = [];
const lines = data.split('\n');
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
if (i == 0 && header == null) {
header = line.split(separator);
} else {
const record = {};
line
.split(separator)
.forEach((item, i) => (record[header[i]] = item));
res.push(record);
}
}
return resolve(res);
} catch (err) {
return silent ? resolve([]) : reject(err);
}
})
.catch(err => (silent ? resolve([]) : reject(err)));
});
}
module.exports = {
write,
read,
readJSON
readJSON,
readCSV
};
This diff is collapsed.
......@@ -14,5 +14,8 @@
"files"
],
"author": "Milan Wikarski",
"license": "ISC"
"license": "ISC",
"devDependencies": {
"mocha": "^6.2.2"
}
}
[
{ "name": "Milan Wikarski", "age": "19", "gender": "M" },
{ "name": "John Smith", "age": "18", "gender": "M" },
{ "name": "Summer Smith", "age": "17", "gender": "F" }
]
name|||age|||gender
Milan Wikarski|||19|||M
John Smith|||18|||M
Summer Smith|||17|||F
\ No newline at end of file
Milan Wikarski,19,M
John Smith,18,M
Summer Smith,17,F
\ No newline at end of file
name,age,gender
Milan Wikarski,19,M
John Smith,18,M
Summer Smith,17,F
\ No newline at end of file
const assert = require('assert');
const { readCSV, readJSON } = require('../index');
const data = JSON.stringify([
{ name: 'Milan Wikarski', age: '19', gender: 'M' },
{ name: 'John Smith', age: '18', gender: 'M' },
{ name: 'Summer Smith', age: '17', gender: 'F' }
]);
describe('readJSON', function() {
it('should be able to read .json file', async function() {
assert.equal(
JSON.stringify(await readJSON(__dirname + '/files/data.json')),
data
);
});
});
describe('readCSV', function() {
it('should be able to read .csv file with header', async function() {
assert.equal(
JSON.stringify(await readCSV(__dirname + '/files/with-header.csv')),
data
);
});
it('should be able to read .csv file without header', async function() {
assert.equal(
JSON.stringify(
await readCSV(__dirname + '/files/no-header.csv', ',', [
'name',
'age',
'gender'
])
),
data
);
});
it('should be able to read .csv file with different separator', async function() {
assert.equal(
JSON.stringify(
await readCSV(__dirname + '/files/exotic-separator.csv', '|||')
),
data
);
});
});
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment