Reusable Accessible Mapping Platform

API Docs for: 5.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", "utils/util"],
  17. function (dojoArray, dojoLang, Util) {
  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.
  50. * @param {Object} scope ???
  51. * @return {Object} first element that satisfies the given predicate; `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 a function that takes one argument and returns true if the
  71. * argument satisfies some condition, and false otherwise.
  72. * @param {Object} scope ???
  73. * @return {Number} index of the first element that satisfied the predicate; `-1` if no such element is found
  74. */
  75. indexOf: function (arr, predicate, scope) {
  76. if (typeof scope !== 'undefined') {
  77. predicate = dojoLang.hitch(scope, predicate);
  78. }
  79. var i;
  80. for (i = 0; i < arr.length; i++) {
  81. if (predicate(arr[i])) {
  82. return i;
  83. }
  84. }
  85. return -1;
  86. },
  87.  
  88. /**
  89. * Given a function which takes one argument and returns 1, 0, or -1, returns the first element
  90. * which causes the given function to return 0.
  91. *
  92. * @method binaryIndexOf
  93. * @static
  94. * @param {Array} arr Array to be searched
  95. * @param {Function} compareFcn ???
  96. * @return {Number} the index of the element that causes the given function to return 0, returns -1 if no such element exists
  97. */
  98. binaryIndexOf: function (arr, compareFcn) {
  99. var minIndex = 0,
  100. maxIndex = arr.length - 1,
  101. currentIndex,
  102. currentElement;
  103.  
  104. while (minIndex <= maxIndex) {
  105. currentIndex = (minIndex + maxIndex) / 2 | 0;
  106. currentElement = arr[currentIndex];
  107.  
  108. if (compareFcn(currentElement) < 0) {
  109. minIndex = currentIndex + 1;
  110. } else if (compareFcn(currentElement) > 0) {
  111. maxIndex = currentIndex - 1;
  112. } else {
  113. return currentIndex;
  114. }
  115. }
  116.  
  117. return -1;
  118. },
  119.  
  120. /**
  121. * Given a function which takes one argument and returns 1, 0, or -1, returns the first element
  122. * which causes the given function to return 0.
  123. *
  124. * @method binaryFind
  125. * @static
  126. * @param {Array} arr Array to be searched
  127. * @param {Function} compareFcn ???
  128. * @return {Number} the index of the element that causes the given function to return 0, returns -1 if no such element exists</returns>
  129. */
  130. binaryFind: function (arr, compareFcn) {
  131. var index = this.binaryIndexOf(arr, compareFcn);
  132. return arr[index];
  133. },
  134.  
  135. /**
  136. * Removes an item from the specified array.
  137. *
  138. * @method remove
  139. * @static
  140. * @param {Array} array Array to have the item removed from
  141. * @param {Number|String|Object} obj can be either an index of the item to be removed, a String to be removed from the array of strings, or an actual Object to be removed; if obj is an Object, you need to provide a predicate function
  142. * @param {Function} [predicate] a function that takes one argument and returns true if the argument satisfies some condition, and false otherwise.
  143. */
  144. remove: function (array, obj, predicate) {
  145. var index;
  146.  
  147. if (!Util.isNumber(obj)) {
  148. if (predicate) {
  149. index = this.indexOf(array, predicate);
  150. } else {
  151. index = array.indexOf(obj);
  152. }
  153. } else {
  154. index = obj;
  155. }
  156.  
  157. if (index !== -1) {
  158. array.splice(index, 1);
  159. }
  160. }
  161. };
  162. });