array.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. /**
  11. * Defines an equality method for Array objects. Only intended for internal use.
  12. *
  13. * @function equals
  14. * @private
  15. * @param {Array}
  16. * array The Array object that should be checked against this Array
  17. * object for equality.
  18. * @param {boolean}
  19. * [checkOrder=false] If <code>true</code>, checks if the element
  20. * order is the same in both Array objects.
  21. * @returns {boolean} <code>true</code> if the equality holds,
  22. * <code>false</code> otherwise.
  23. */
  24. Array.prototype.equals = function(array, checkOrder) {
  25. if (!array) {
  26. return false;
  27. }
  28. if (arguments.length === 1) {
  29. checkOrder = true;
  30. }
  31. if (this.length !== array.length) {
  32. return false;
  33. }
  34. for (var i = 0; i < this.length; i++) {
  35. if (this[i] instanceof Array && array[i] instanceof Array) {
  36. if (!this[i].equals(array[i], checkOrder)) {
  37. return false;
  38. }
  39. } else if (checkOrder && (!this[i].equals(array[i]))) {
  40. return false;
  41. } else if (!checkOrder) {
  42. return this.sort().equals(array.sort(), true);
  43. }
  44. }
  45. return true;
  46. };