Skip to content

Instantly share code, notes, and snippets.

@jrgm
Created June 15, 2013 03:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrgm/5786804 to your computer and use it in GitHub Desktop.
Save jrgm/5786804 to your computer and use it in GitHub Desktop.
mutate some stdin json
var convertTimes = true;
function handleEmail(obj) {
delete obj.pub;
delete obj.priv;
var cert = obj.cert;
if (cert) {
delete cert.header;
var payload = cert.payload;
if (payload) {
delete payload.header;
delete payload.principal;
delete payload['public-key'];
}
}
}
function walkTimes(obj) {
Object.keys(obj).forEach(function(key) {
var elt = obj[key];
if (typeof elt === 'object') {
walkTimes(elt);
} else {
if (elt.match(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z/)) {
var delta = ((Date.now() - Date.parse(elt)) / 3600 / 1000).toFixed(2);
obj[key] = obj[key] + " : " + delta;
}
}
});
}
function digest(data) {
var emails = data.emails;
if (!emails) return;
Object.keys(emails).forEach(function(key) {
if (key.match(/@/)) {
handleEmail(emails[key]);
} else {
Object.keys(emails[key]).forEach(function(k2) {
handleEmail(emails[key][k2]);
});
}
});
if (convertTimes) {
walkTimes(emails);
}
console.log(JSON.stringify(emails, null, 2));
}
(function run() {
var data = '';
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(chunk) {
data += chunk;
});
process.stdin.on('end', function() {
try {
data = JSON.parse(data);
digest(data);
} catch(e) {
console.log(e);
process.exit(1);
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment