6fbd3a20b18d27468c95803d7ab396a8f7c8f1befed692b91e105b59c99c8701595703422fccfb1aaef80e8a19141c94600300042c4a4f27a0ada86cb556d0 5.5 KB

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