progress-meter-data.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright 2018 Scytl Secure Electronic Voting SA
  3. *
  4. * All rights reserved
  5. *
  6. * See our extended copyright notice in *file 'Copyright.txt' which is part of this source code package
  7. */
  8. /* jshint node:true */
  9. 'use strict';
  10. module.exports = ProgressMeterTestData;
  11. /**
  12. * Provides the data and functions needed by the zero-knowledge proof of
  13. * knowledge progress meter unit tests.
  14. */
  15. function ProgressMeterTestData() {
  16. var DEFAULT_CUSTOM_PROGRESS_PERCENT_MIN_CHECK_INTERVAL = 10;
  17. var CUSTOM_PROGRESS_PERCENT_MIN_CHECK_INTERVAL = 5;
  18. var progressPercentSum_;
  19. var progressPercentLatest_;
  20. this.initializeProgressMeterData = function() {
  21. progressPercentSum_ = 0;
  22. progressPercentLatest_ = 0;
  23. };
  24. this.getDefaultProgressPercentMinCheckInterval = function() {
  25. return DEFAULT_CUSTOM_PROGRESS_PERCENT_MIN_CHECK_INTERVAL;
  26. };
  27. this.getCustomProgressPercentMinCheckInterval = function() {
  28. return CUSTOM_PROGRESS_PERCENT_MIN_CHECK_INTERVAL;
  29. };
  30. this.getProgressPercentSum = function() {
  31. return progressPercentSum_;
  32. };
  33. this.getProgressPercentLatest = function() {
  34. return progressPercentLatest_;
  35. };
  36. this.progressCallback = function(progressPercent) {
  37. progressPercentSum_ += progressPercent;
  38. progressPercentLatest_ = progressPercent;
  39. };
  40. this.calculateProgressPercentSum = function(numChecks, minCheckInterval) {
  41. var percent = 0;
  42. var lastPercent = 0;
  43. var percentChange = 0;
  44. var incrementPercentSum = false;
  45. var percentSum = 0;
  46. for (var i = 0; i < numChecks; i++) {
  47. percent = Math.floor(((i + 1) / numChecks) * 100);
  48. percent = Math.min(percent, 100);
  49. percentChange = percent - lastPercent;
  50. if (percentChange > 0) {
  51. incrementPercentSum =
  52. (percentChange >= minCheckInterval) || (percent === 100);
  53. if (incrementPercentSum) {
  54. lastPercent = percent;
  55. percentSum += percent;
  56. }
  57. }
  58. }
  59. return percentSum;
  60. };
  61. }