Last active
June 5, 2016 15:19
-
-
Save fernandocamargo/44f3703e7f2427402ece5ebd70d27d32 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _ = require('lodash'); | |
module.exports.arrayDiffToHtmlTable = function () { | |
var occurrences = _.flatten(_.toArray(arguments)); | |
var cache = {}; | |
var results = occurrences.reduce(function (stack, item) { | |
var rewrite = {}; | |
var flatten = Object.assign(_.omit(item, ['meta']), item.meta); | |
var data = rewrite[item._id] = flatten; | |
cache[item._id] = (cache.hasOwnProperty(item._id) ? cache[item._id] : flatten); | |
return Object.assign(stack, rewrite); | |
}, {}); | |
var attributes = _.uniq(_.flatten(_.map(results, _.keys))); | |
var head = '<tr>' + attributes.reduce(function (stack, attribute) { | |
return stack.concat('<td>' + attribute + '</td>'); | |
}, []).join("\n") + '</tr>'; | |
var body = Object.keys(results).map(function (id) { | |
var result = results[id]; | |
return '<tr>' + attributes.reduce(function (_stack, attribute) { | |
var bolded = (_.values(cache)[0][attribute] !== result[attribute]); | |
var bold = { | |
begin: (bolded ? '**' : ''), | |
end: (bolded ? '**' : '') | |
}; | |
return _stack.concat('<td>' + bold.begin + (result[attribute] || '') + bold.end + '</td>'); | |
}, []).join("\n") + '</tr>'; | |
}).join("\n"); | |
return ('<table>' + head + body + '</table>'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment