array.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. export function to2dArray(arr) {
  2. var i = 0,
  3. ilen = arr.length;
  4. while (i < ilen) {
  5. arr[i] = [arr[i]];
  6. i++;
  7. }
  8. }
  9. export function extendArray(arr, extension) {
  10. var i = 0,
  11. ilen = extension.length;
  12. while (i < ilen) {
  13. arr.push(extension[i]);
  14. i++;
  15. }
  16. }
  17. export function pivot(arr) {
  18. var pivotedArr = [];
  19. if (!arr || arr.length === 0 || !arr[0] || arr[0].length === 0) {
  20. return pivotedArr;
  21. }
  22. var rowCount = arr.length;
  23. var colCount = arr[0].length;
  24. for (var i = 0; i < rowCount; i++) {
  25. for (var j = 0; j < colCount; j++) {
  26. if (!pivotedArr[j]) {
  27. pivotedArr[j] = [];
  28. }
  29. pivotedArr[j][i] = arr[i][j];
  30. }
  31. }
  32. return pivotedArr;
  33. }
  34. /**
  35. * A specialized version of `.reduce` for arrays without support for callback
  36. * shorthands and `this` binding.
  37. *
  38. * {@link https://github.com/lodash/lodash/blob/master/lodash.js}
  39. *
  40. * @param {Array} array The array to iterate over.
  41. * @param {Function} iteratee The function invoked per iteration.
  42. * @param {*} [accumulator] The initial value.
  43. * @param {Boolean} [initFromArray] Specify using the first element of `array` as the initial value.
  44. * @returns {*} Returns the accumulated value.
  45. */
  46. export function arrayReduce(array, iteratee, accumulator, initFromArray) {
  47. var index = -1,
  48. length = array.length;
  49. if (initFromArray && length) {
  50. accumulator = array[++index];
  51. }
  52. while (++index < length) {
  53. accumulator = iteratee(accumulator, array[index], index, array);
  54. }
  55. return accumulator;
  56. }
  57. /**
  58. * A specialized version of `.filter` for arrays without support for callback
  59. * shorthands and `this` binding.
  60. *
  61. * {@link https://github.com/lodash/lodash/blob/master/lodash.js}
  62. *
  63. * @param {Array} array The array to iterate over.
  64. * @param {Function} predicate The function invoked per iteration.
  65. * @returns {Array} Returns the new filtered array.
  66. */
  67. export function arrayFilter(array, predicate) {
  68. var index = -1,
  69. length = array.length,
  70. resIndex = -1,
  71. result = [];
  72. while (++index < length) {
  73. var value = array[index];
  74. if (predicate(value, index, array)) {
  75. result[++resIndex] = value;
  76. }
  77. }
  78. return result;
  79. }
  80. /**
  81. * A specialized version of `.map` for arrays without support for callback
  82. * shorthands and `this` binding.
  83. *
  84. * @param {Array} array The array to iterate over.
  85. * @param {Function} iteratee The function invoked per iteration.
  86. * @returns {Array} Returns the new filtered array.
  87. */
  88. export function arrayMap(array, iteratee) {
  89. var index = -1,
  90. length = array.length,
  91. resIndex = -1,
  92. result = [];
  93. while (++index < length) {
  94. var value = array[index];
  95. result[++resIndex] = iteratee(value, index, array);
  96. }
  97. return result;
  98. }
  99. /**
  100. * A specialized version of `.forEach` for arrays without support for callback
  101. * shorthands and `this` binding.
  102. *
  103. * {@link https://github.com/lodash/lodash/blob/master/lodash.js}
  104. *
  105. * @param {Array} array The array to iterate over.
  106. * @param {Function} iteratee The function invoked per iteration.
  107. * @returns {Array} Returns `array`.
  108. */
  109. export function arrayEach(array, iteratee) {
  110. var index = -1,
  111. length = array.length;
  112. while (++index < length) {
  113. if (iteratee(array[index], index, array) === false) {
  114. break;
  115. }
  116. }
  117. return array;
  118. }
  119. /**
  120. * Calculate sum value for each item of the array.
  121. *
  122. * @param {Array} array The array to process.
  123. * @returns {Number} Returns calculated sum value.
  124. */
  125. export function arraySum(array) {
  126. return arrayReduce(array, function (a, b) {
  127. return a + b;
  128. }, 0);
  129. }
  130. /**
  131. * Returns the highest value from an array. Can be array of numbers or array of strings.
  132. * NOTICE: Mixed values is not supported.
  133. *
  134. * @param {Array} array The array to process.
  135. * @returns {Number} Returns the highest value from an array.
  136. */
  137. export function arrayMax(array) {
  138. return arrayReduce(array, function (a, b) {
  139. return a > b ? a : b;
  140. }, Array.isArray(array) ? array[0] : void 0);
  141. }
  142. /**
  143. * Returns the lowest value from an array. Can be array of numbers or array of strings.
  144. * NOTICE: Mixed values is not supported.
  145. *
  146. * @param {Array} array The array to process.
  147. * @returns {Number} Returns the lowest value from an array.
  148. */
  149. export function arrayMin(array) {
  150. return arrayReduce(array, function (a, b) {
  151. return a < b ? a : b;
  152. }, Array.isArray(array) ? array[0] : void 0);
  153. }
  154. /**
  155. * Calculate average value for each item of the array.
  156. *
  157. * @param {Array} array The array to process.
  158. * @returns {Number} Returns calculated average value.
  159. */
  160. export function arrayAvg(array) {
  161. if (!array.length) {
  162. return 0;
  163. }
  164. return arraySum(array) / array.length;
  165. }
  166. /**
  167. * Flatten multidimensional array.
  168. *
  169. * @param {Array} array Array of Arrays
  170. * @returns {Array}
  171. */
  172. export function arrayFlatten(array) {
  173. return arrayReduce(array, function (initial, value) {
  174. return initial.concat(Array.isArray(value) ? arrayFlatten(value) : value);
  175. }, []);
  176. }
  177. /**
  178. * Unique values in the array.
  179. *
  180. * @param {Array} array The array to process.
  181. * @returns {Array}
  182. */
  183. export function arrayUnique(array) {
  184. var unique = [];
  185. arrayEach(array, function (value) {
  186. if (unique.indexOf(value) === -1) {
  187. unique.push(value);
  188. }
  189. });
  190. return unique;
  191. }