Skip to content

Commit

Permalink
Update by to use the latest tree-math.
Browse files Browse the repository at this point in the history
  • Loading branch information
Max Murphy-Skvorzov committed Sep 28, 2015
1 parent d0f95f5 commit 84d3eb8
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 7 deletions.
10 changes: 6 additions & 4 deletions bin/by.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,19 @@ var outvals = values.map(function(a){return a.length===1?a[0]:a[1];});
var all = {};
parseStream(process.stdin)
.on('jline', function(record){
var storepath = inpaths.map(function(path){return getPath(record, [].concat(path));});
var recordPath = getPath.bind(null,record);
var storepath = inpaths.map(recordPath);
var storenode = storepath.reduce(function(d,k){if(d[k]===undefined)d[k]={};return d[k];},all);
invals.forEach(function(inpath, index){
incrementPath(all, storepath.concat(outvals[index]), getPath(record, [].concat(inpath)));
incrementPath(storenode, outvals[index], getPath(record, inpath));
});
})
.on('end', function(){
var maxdepth = inpaths.length;
var path = [];
find(all, function(path, val){
find(all,{maxdepth:maxdepth}, function(path, val){
outpaths.forEach(function(outpath, index){
setPath(val, outpath.concat([]), path[index]);
setPath(val, outpath, path[index]);
});
console.log(JSON.stringify(val));
}, path, maxdepth);
Expand Down
7 changes: 6 additions & 1 deletion bin/by.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ Reduce values to some breakdown. E.g. if you have a whole lot of records that l

You may want to get the total number of sightings by year. You can do this with:

cat data | jline-breakdown year -- sightings
cat data | jline-by year -- sightings

This will output one JSON line for each year together with the total number of sightings for that year:

{"sightings":135,"year":"1982"}
{"sightings":172,"year":"1983"}
{"sightings":211,"year":"1984"}

## Command line:

Expand Down
2 changes: 1 addition & 1 deletion bin/foreach.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function keyvals(dict, callback){

function setpath(subject,path,value){
var c=path.length;
path.reduce(function(s,n){return s[n]=--c?(s[n]||{}):value;var after=s[n];},subject);
var ans = path.reduce(function(s,n){return s[n]=--c?(s[n]||{}):value;var after=s[n];},subject);
}
function getpath(subject,path){
return path.reduce(function(s,n){if(s!==undefined)return s[n];},subject);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"dependencies": {
"docopt": "^0.4.1",
"split": "^0.3.0",
"tree-math": "0.0.3",
"tree-math": "0.0.16",
"ya-csv": "^0.9.4"
}
}
5 changes: 5 additions & 0 deletions test/by.jsonl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{"state":"Georgia", "year":1982, "sightings":27, "validatedSightings":19}
{"state":"New Jersey", "year":1982, "sightings":26, "validatedSightings":17}
{"state":"Georgia", "year":1982, "sightings":28, "validatedSightings":21}
{"state":"California", "year":1982, "sightings":25, "validatedSightings":4}
{"state":"Georgia", "year":1982, "sightings":29, "validatedSightings":1}
9 changes: 9 additions & 0 deletions test/by.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

cd "$(dirname "$0")/.."

set -eux

./bin/by.js --help || (($? == 2))
./bin/by.js year -- sightings <test/by.jsonl

echo OK

0 comments on commit 84d3eb8

Please sign in to comment.