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:
parent
2d6580df52
commit
2d1398f85d
44
index.js
44
index.js
@ -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 = [];
|
||||
|
||||
// t/f is the object a credential
|
||||
var isCred = function (k) {
|
||||
// creds end in .k
|
||||
return /\.k/.test(k);
|
||||
return true;
|
||||
};
|
||||
|
||||
//console.log(JSON.stringify(P, null, 2));
|
||||
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;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user