5
0
mirror of https://github.com/cwinfo/hyperboria-peers.git synced 2024-11-22 13:00:38 +00:00

indent. implement map and filter

This commit is contained in:
ansuz 2016-06-12 11:52:46 +02:00
parent 2d6580df52
commit 2d1398f85d

View File

@ -1,6 +1,10 @@
var Fs = require("fs"),
R = function (p) {
return JSON.parse(Fs.readFileSync(p, 'utf-8'));
var content = Fs.readFileSync(p, 'utf-8');
if (content.charAt(content.length - 1) !== '\n') {
throw new Error("file at " + p + " did not end with a newline character");
}
return JSON.parse(content);
},
P = module.exports.peers = {
AS: {
@ -90,6 +94,42 @@ var Fs = require("fs"),
},
},
},
};
},
map = module.exports.map = function (f) {
var L = [];
//console.log(JSON.stringify(P, null, 2));
// t/f is the object a credential
var isCred = function (k) {
// creds end in .k
return /\.k/.test(k);
return true;
};
var walk = function (o, p, f) {
// walk the tree of objects
if (typeof(o) === 'object') {
// for each key in o, walk the key
Object.keys(o).forEach(function (k) {
var path = p.slice(0).concat(k);
if (isCred(k)) {
//console.log(o[k]);
L.push(f(o[k], path));
}
walk(o[k], path, f);
});
}
};
walk(P, [], f);
return L;
},
filter = module.exports.filter = function (f) {
var L = [];
map(function (x, p) {
if (f(x,p)) { L.push(x); }
});
return L;
};