Skip to content

Commit

Permalink
Add some core tree math tools to save importing them all the time.
Browse files Browse the repository at this point in the history
  • Loading branch information
Max Murphy-Skvorzov committed Aug 13, 2015
1 parent 6e9fe24 commit d0f95f5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
30 changes: 28 additions & 2 deletions bin/foreach.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,39 @@ function setpath(subject,path,value){
function getpath(subject,path){
return path.reduce(function(s,n){if(s!==undefined)return s[n];},subject);
}
function find(data, options, callback) {
if (typeof(options)==='function'){
callback = options;
options = {};
}
var prefix = options.prefix ? options.prefix : [];
var maxdepth = options.maxdepth === undefined? -1: options.maxdepth;
return flatten(data, callback, prefix, maxdepth);
}
function flatten(data, callback, prefix, maxdepth) {
if (maxdepth === 0){
return callback(prefix, data);
} else {
var path, val, ans;
for (var key in data) {
path = prefix.slice(0);
path.push(key);
val = data[key];
ans = (typeof(val) === 'object')
? flatten(val, callback, path, maxdepth-1)
: callback(path, val);
if (ans) return ans;
}
}
}


var source = require('./clean')(process.stdin);
source.on('comment', function(c){console.log(c);});
eventHandlers.forEach(function(tuple){
var event = tuple[0].toLowerCase();
var code = Function('require','emit','keyvals','setpath','getpath', 'record', 'lineNumber', 'line', 'recordNumber', tuple[1])
.bind(null,req, emit, keyvals , setpath , getpath);
var code = Function('require','emit','keyvals','setpath','getpath','find','flatten', 'record', 'lineNumber', 'line', 'recordNumber', tuple[1])
.bind(null,req, emit, keyvals , setpath , getpath , find , flatten);
code.name = event;
if (['beg','begin','start','beginning'].indexOf(event)!== -1) code();
else source.on(event, code);
Expand Down
8 changes: 8 additions & 0 deletions bin/foreach.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,11 @@ All standard nodejs functions are available and in addition:
* `require(path)` - looks in the current working directory first for relative paths.

* `keyvals(dict, callback(key,val))` - iterates over a dictionary.

* `setpath(dict, [x1,x2,x3,...],val)` - sets `dict.x1.x2.x3=val`, creating any intermediate objects as necessary.

* `getpath(dict, [x1,x2,x3,...])` - returns `dict.x1.x2.x3` or `undefined`

* `find(dict,{options},callback(path,value){....})` Find all paths in a dictionary. Useful for converting trees into tabular form. Options are: `{maxdepth:3,prefix:["each","row","starts","with"]}`


2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jline",
"version": "0.0.38",
"version": "0.0.39",
"description": "Sort, grep and join files where every line is JSON.",
"main": "index.js",
"bin": {
Expand Down

0 comments on commit d0f95f5

Please sign in to comment.