helpers.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. Copyright 2012-2015, Yahoo Inc.
  3. Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
  4. */
  5. function registerHelpers(handlebars) {
  6. handlebars.registerHelper('show_picture', function (opts) {
  7. var num = Number(opts.fn(this)),
  8. rest,
  9. cls = '';
  10. if (isFinite(num)) {
  11. if (num === 100) {
  12. cls = ' cover-full';
  13. }
  14. num = Math.floor(num);
  15. rest = 100 - num;
  16. return '<div class="cover-fill' + cls + '" style="width: ' + num + '%;"></div>' +
  17. '<div class="cover-empty" style="width:' + rest + '%;"></div>';
  18. } else {
  19. return '';
  20. }
  21. });
  22. handlebars.registerHelper('if_has_ignores', function (metrics, opts) {
  23. return (metrics.statements.skipped +
  24. metrics.functions.skipped +
  25. metrics.branches.skipped) === 0 ? '' : opts.fn(this);
  26. });
  27. handlebars.registerHelper('show_ignores', function (metrics) {
  28. var statements = metrics.statements.skipped,
  29. functions = metrics.functions.skipped,
  30. branches = metrics.branches.skipped,
  31. result;
  32. if (statements === 0 && functions === 0 && branches === 0) {
  33. return '<span class="ignore-none">none</span>';
  34. }
  35. result = [];
  36. if (statements > 0) {
  37. result.push(statements === 1 ? '1 statement' : statements + ' statements');
  38. }
  39. if (functions > 0) {
  40. result.push(functions === 1 ? '1 function' : functions + ' functions');
  41. }
  42. if (branches > 0) {
  43. result.push(branches === 1 ? '1 branch' : branches + ' branches');
  44. }
  45. return result.join(', ');
  46. });
  47. handlebars.registerHelper('show_lines', function (opts) {
  48. var maxLines = Number(opts.fn(this)),
  49. i,
  50. array = [];
  51. for (i = 0; i < maxLines; i += 1) {
  52. array[i] = i + 1;
  53. }
  54. return array.join('\n');
  55. });
  56. handlebars.registerHelper('show_line_execution_counts', function (context) {
  57. var array = [];
  58. context.forEach(function (data) {
  59. array.push('<span class="cline-any cline-' + data.covered + '">' + data.hits + '</span>');
  60. });
  61. return array.join('\n');
  62. });
  63. handlebars.registerHelper('show_code', function (context /*, opts */) {
  64. return context.join('\n');
  65. });
  66. }
  67. module.exports = {
  68. registerHelpers: registerHelpers
  69. };