Reusable Accessible Mapping Platform

API Docs for: 3.0.0
Show:

File: src\js\RAMP\Utils\array.js

  1. /*global define */
  2. /*jshint bitwise: false*/
  3.  
  4. /**
  5. * @module Utils
  6. */
  7.  
  8. /**
  9. * Set of functions that deal with arrays.
  10. *
  11. * @class Array
  12. * @static
  13. * @uses dojo/_base/array
  14. * @uses dojo/_base/lang
  15. */
  16. define(["dojo/_base/array", "dojo/_base/lang"],
  17. function (dojoArray, dojoLang) {
  18. "use strict";
  19. return {
  20. /**
  21. * Returns an array that has only unique values (only the first element is kept).
  22. *
  23. * @method unique
  24. * @static
  25. * @param {Array} array Array to be searched
  26. * @return {Array} array that has only unique values
  27. */
  28. unique: function (array) {
  29. return array.reverse().filter(function (e, i, array) {
  30. return array.indexOf(e, i + 1) === -1;
  31. }).reverse();
  32. },
  33.  
  34. /**
  35. * Returns the first element in the given array that satisfies the given
  36. * predicate. Returns null if no element in the given array satisfies
  37. * the given predicate.
  38. *
  39. * #####EXAMPLE
  40. * var array = [1, 2, 3, 4]
  41. * find(array, function(a_number) { return a_number === 2 }); -> 2
  42. * find(array, function(a_number) { return a_number === 5 }); -> null
  43. *
  44. *
  45. * @method find
  46. * @static
  47. * @param {Array} arr Array to be searched
  48. * @param {Function} predicate a function that takes one argument and returns true if the
  49. * argument satisfies some condition, and false otherwise.</param>
  50. * @param {Object} scope ???
  51. * @return {Object} first elemtn that satisfies the given preidcate; `null` if no such element is found
  52. */
  53. find: function (arr, predicate, scope) {
  54. var index = this.indexOf(arr, predicate, scope);
  55. if (index === -1) {
  56. return null;
  57. } else {
  58. return arr[index];
  59. }
  60. },
  61.  
  62. /**
  63. * Returns the index of the first element in the given array that satisfies the given
  64. * predicate. Returns -1 if no element in the given array satisfies the predicate.
  65. *
  66. *
  67. * @method indexOf
  68. * @static
  69. * @param {Array} arr Array to be searched
  70. * @param {Function} predicate predicate a function that takes one argument and returns true if the
  71. * @param {Object} scope ???
  72. * @return {Number} index of the first element that satisfied the predicate; `-1` if no such element is found
  73. */
  74. indexOf: function (arr, predicate, scope) {
  75. if (typeof scope !== 'undefined') {
  76. predicate = dojoLang.hitch(scope, predicate);
  77. }
  78. var i;
  79. for (i = 0; i < arr.length; i++) {
  80. if (predicate(arr[i])) {
  81. return i;
  82. }
  83. }
  84. return -1;
  85. },
  86.  
  87. /**
  88. * Given a function which takes one argument and returns 1, 0, or -1, returns the first element
  89. * which causes the given function to return 0.
  90. *
  91. * @method binaryIndexOf
  92. * @static
  93. * @param {Array} arr Array to be searched
  94. * @param {Function} compareFcn ???
  95. * @return {Number} the index of the element that causes the given function to return 0, returns -1 if no such element exists
  96. */
  97. binaryIndexOf: function (arr, compareFcn) {
  98. var minIndex = 0,
  99. maxIndex = arr.length - 1,
  100. currentIndex,
  101. currentElement;
  102.  
  103. while (minIndex <= maxIndex) {
  104. currentIndex = (minIndex + maxIndex) / 2 | 0;
  105. currentElement = arr[currentIndex];
  106.  
  107. if (compareFcn(currentElement) < 0) {
  108. minIndex = currentIndex + 1;
  109. } else if (compareFcn(currentElement) > 0) {
  110. maxIndex = currentIndex - 1;
  111. } else {
  112. return currentIndex;
  113. }
  114. }
  115.  
  116. return -1;
  117. },
  118.  
  119. /**
  120. * Given a function which takes one argument and returns 1, 0, or -1, returns the first element
  121. * which causes the given function to return 0.
  122. *
  123. * @method binaryFind
  124. * @static
  125. * @param {Array} arr Array to be searched
  126. * @param {Function} compareFcn ???
  127. * @return {Number} the index of the element that causes the given function to return 0, returns -1 if no such element exists</returns>
  128. */
  129. binaryFind: function (arr, compareFcn) {
  130. var index = this.binaryIndexOf(arr, compareFcn);
  131. return arr[index];
  132. }
  133. };
  134. });