2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 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
|
||||
);
|
||||
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue