Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,41 @@
|
|||
const fs = require('fs');
|
||||
|
||||
// formats for the data parameter in the function below: {col1: array | function, col2: array | function}
|
||||
|
||||
function addCols(path, data) {
|
||||
let csv = fs.readFileSync(path, 'utf8');
|
||||
csv = csv.split('\n').map(line => line.trim());
|
||||
let colNames = Object.keys(data);
|
||||
for (let i = 0; i < colNames.length; i++) {
|
||||
let c = colNames[i];
|
||||
if (typeof data[c] === 'function') {
|
||||
csv = csv.map((line, idx) => idx === 0
|
||||
? line + ',' + c
|
||||
: line + ',' + data[c](line, idx)
|
||||
);
|
||||
} else if (Array.isArray(data[c])) {
|
||||
csv = csv.map((line, idx) => idx === 0
|
||||
? line + ',' + c
|
||||
: line + ',' + data[c][idx - 1]
|
||||
);
|
||||
}
|
||||
}
|
||||
fs.createWriteStream(path, {
|
||||
flag: 'w',
|
||||
defaultEncoding: 'utf8'
|
||||
}).end(csv.join('\n'));
|
||||
}
|
||||
|
||||
addCols('test.csv', {
|
||||
sum: function (line, idx) {
|
||||
let s = 0;
|
||||
line = line.split(',').map(d => +(d.trim()));
|
||||
for (let i = 0; i < line.length; i++) {
|
||||
s += line[i];
|
||||
}
|
||||
return s;
|
||||
},
|
||||
id: function(line, idx) {
|
||||
return idx;
|
||||
}
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue