Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,76 @@
|
|||
(function () {
|
||||
'use strict';
|
||||
|
||||
// splitRegex :: Regex -> String -> [String]
|
||||
function splitRegex(rgx, s) {
|
||||
return s.split(rgx);
|
||||
}
|
||||
|
||||
// lines :: String -> [String]
|
||||
function lines(s) {
|
||||
return s.split(/[\r\n]/);
|
||||
}
|
||||
|
||||
// unlines :: [String] -> String
|
||||
function unlines(xs) {
|
||||
return xs.join('\n');
|
||||
}
|
||||
|
||||
// macOS JavaScript for Automation version of readFile.
|
||||
// Other JS contexts will need a different definition of this function,
|
||||
// and some may have no access to the local file system at all.
|
||||
|
||||
// readFile :: FilePath -> maybe String
|
||||
function readFile(strPath) {
|
||||
var error = $(),
|
||||
str = ObjC.unwrap(
|
||||
$.NSString.stringWithContentsOfFileEncodingError(
|
||||
$(strPath)
|
||||
.stringByStandardizingPath,
|
||||
$.NSUTF8StringEncoding,
|
||||
error
|
||||
)
|
||||
);
|
||||
return error.code ? error.localizedDescription : str;
|
||||
}
|
||||
|
||||
// macOS JavaScript for Automation version of writeFile.
|
||||
// Other JS contexts will need a different definition of this function,
|
||||
// and some may have no access to the local file system at all.
|
||||
|
||||
// writeFile :: FilePath -> String -> IO ()
|
||||
function writeFile(strPath, strText) {
|
||||
$.NSString.alloc.initWithUTF8String(strText)
|
||||
.writeToFileAtomicallyEncodingError(
|
||||
$(strPath)
|
||||
.stringByStandardizingPath, false,
|
||||
$.NSUTF8StringEncoding, null
|
||||
);
|
||||
}
|
||||
|
||||
// EXAMPLE - appending a SUM column
|
||||
|
||||
var delimCSV = /,\s*/g;
|
||||
|
||||
var strSummed = unlines(
|
||||
lines(readFile('~/csvSample.txt'))
|
||||
.map(function (x, i) {
|
||||
var xs = x ? splitRegex(delimCSV, x) : [];
|
||||
|
||||
return (xs.length ? xs.concat(
|
||||
// 'SUM' appended to first line, others summed.
|
||||
i > 0 ? xs.reduce(
|
||||
function (a, b) {
|
||||
return a + parseInt(b, 10);
|
||||
}, 0
|
||||
).toString() : 'SUM'
|
||||
) : []).join(',');
|
||||
})
|
||||
);
|
||||
|
||||
return (
|
||||
writeFile('~/csvSampleSummed.txt', strSummed),
|
||||
strSummed
|
||||
);
|
||||
|
||||
})();
|
||||
|
|
@ -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