array.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. ////////////////////////////////////////////////
  9. //
  10. // Functionalities added to JavaScript types
  11. //
  12. ////////////////////////////////////////////////
  13. /**
  14. * An equals method for arrays.
  15. *
  16. * @param array
  17. * the array to which this array should be compared.
  18. * @param strict
  19. * a boolean value which specifies whether or not the elements must
  20. * be in the same order in both arrays. This parameter is optional,
  21. * if it is not supplied, then a default value of 'true' is assigned
  22. * to it.
  23. */
  24. Array.prototype.equals = function(array, strict) {
  25. 'use strict';
  26. if (!array) {
  27. return false;
  28. }
  29. if (arguments.length === 1) {
  30. strict = true;
  31. }
  32. if (this.length !== array.length) {
  33. return false;
  34. }
  35. for (var i = 0; i < this.length; i++) {
  36. if (this[i] instanceof Array && array[i] instanceof Array) {
  37. if (!this[i].equals(array[i], strict)) {
  38. return false;
  39. }
  40. } else if (strict && (!this[i].equals(array[i]))) {
  41. return false;
  42. } else if (!strict) {
  43. return this.sort().equals(array.sort(), true);
  44. }
  45. }
  46. return true;
  47. };
  48. /**
  49. * Allows all of the elements of one array to be added into another array.
  50. */
  51. Array.prototype.addAll = function() {
  52. 'use strict';
  53. for (var a = 0; a < arguments.length; a++) {
  54. var arr = arguments[a];
  55. for (var i = 0; i < arr.length; i++) {
  56. this.push(arr[i]);
  57. }
  58. }
  59. };