78111ce2ac9cc4d4b77cf83dff02296031cf060c3e83835fd4930fbab06801f44a2e1a88ec9ea8f20c323418a8ffaddc1c34207484e9fe9a854472e8fd46cf 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. let 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. let index = -1,
  69. length = array.length,
  70. resIndex = -1,
  71. result = [];
  72. while (++index < length) {
  73. let 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. let index = -1,
  90. length = array.length,
  91. resIndex = -1,
  92. result = [];
  93. while (++index < length) {
  94. let 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. let 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, (a, b) => (a + b), 0);
  127. }
  128. /**
  129. * Returns the highest value from an array. Can be array of numbers or array of strings.
  130. * NOTICE: Mixed values is not supported.
  131. *
  132. * @param {Array} array The array to process.
  133. * @returns {Number} Returns the highest value from an array.
  134. */
  135. export function arrayMax(array) {
  136. return arrayReduce(array, (a, b) => (a > b ? a : b), Array.isArray(array) ? array[0] : void 0);
  137. }
  138. /**
  139. * Returns the lowest value from an array. Can be array of numbers or array of strings.
  140. * NOTICE: Mixed values is not supported.
  141. *
  142. * @param {Array} array The array to process.
  143. * @returns {Number} Returns the lowest value from an array.
  144. */
  145. export function arrayMin(array) {
  146. return arrayReduce(array, (a, b) => (a < b ? a : b), Array.isArray(array) ? array[0] : void 0);
  147. }
  148. /**
  149. * Calculate average value for each item of the array.
  150. *
  151. * @param {Array} array The array to process.
  152. * @returns {Number} Returns calculated average value.
  153. */
  154. export function arrayAvg(array) {
  155. if (!array.length) {
  156. return 0;
  157. }
  158. return arraySum(array) / array.length;
  159. }
  160. /**
  161. * Flatten multidimensional array.
  162. *
  163. * @param {Array} array Array of Arrays
  164. * @returns {Array}
  165. */
  166. export function arrayFlatten(array) {
  167. return arrayReduce(array, (initial, value) => initial.concat(Array.isArray(value) ? arrayFlatten(value) : value), []);
  168. }
  169. /**
  170. * Unique values in the array.
  171. *
  172. * @param {Array} array The array to process.
  173. * @returns {Array}
  174. */
  175. export function arrayUnique(array) {
  176. let unique = [];
  177. arrayEach(array, (value) => {
  178. if (unique.indexOf(value) === -1) {
  179. unique.push(value);
  180. }
  181. });
  182. return unique;
  183. }