123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- export function to2dArray(arr) {
- var i = 0,
- ilen = arr.length;
- while (i < ilen) {
- arr[i] = [arr[i]];
- i++;
- }
- }
- export function extendArray(arr, extension) {
- var i = 0,
- ilen = extension.length;
- while (i < ilen) {
- arr.push(extension[i]);
- i++;
- }
- }
- export function pivot(arr) {
- var pivotedArr = [];
- if (!arr || arr.length === 0 || !arr[0] || arr[0].length === 0) {
- return pivotedArr;
- }
- var rowCount = arr.length;
- var colCount = arr[0].length;
- for (var i = 0; i < rowCount; i++) {
- for (var j = 0; j < colCount; j++) {
- if (!pivotedArr[j]) {
- pivotedArr[j] = [];
- }
- pivotedArr[j][i] = arr[i][j];
- }
- }
- return pivotedArr;
- }
- /**
- * A specialized version of `.reduce` for arrays without support for callback
- * shorthands and `this` binding.
- *
- * {@link https://github.com/lodash/lodash/blob/master/lodash.js}
- *
- * @param {Array} array The array to iterate over.
- * @param {Function} iteratee The function invoked per iteration.
- * @param {*} [accumulator] The initial value.
- * @param {Boolean} [initFromArray] Specify using the first element of `array` as the initial value.
- * @returns {*} Returns the accumulated value.
- */
- export function arrayReduce(array, iteratee, accumulator, initFromArray) {
- var index = -1,
- length = array.length;
- if (initFromArray && length) {
- accumulator = array[++index];
- }
- while (++index < length) {
- accumulator = iteratee(accumulator, array[index], index, array);
- }
- return accumulator;
- }
- /**
- * A specialized version of `.filter` for arrays without support for callback
- * shorthands and `this` binding.
- *
- * {@link https://github.com/lodash/lodash/blob/master/lodash.js}
- *
- * @param {Array} array The array to iterate over.
- * @param {Function} predicate The function invoked per iteration.
- * @returns {Array} Returns the new filtered array.
- */
- export function arrayFilter(array, predicate) {
- var index = -1,
- length = array.length,
- resIndex = -1,
- result = [];
- while (++index < length) {
- var value = array[index];
- if (predicate(value, index, array)) {
- result[++resIndex] = value;
- }
- }
- return result;
- }
- /**
- * A specialized version of `.map` for arrays without support for callback
- * shorthands and `this` binding.
- *
- * @param {Array} array The array to iterate over.
- * @param {Function} iteratee The function invoked per iteration.
- * @returns {Array} Returns the new filtered array.
- */
- export function arrayMap(array, iteratee) {
- var index = -1,
- length = array.length,
- resIndex = -1,
- result = [];
- while (++index < length) {
- var value = array[index];
- result[++resIndex] = iteratee(value, index, array);
- }
- return result;
- }
- /**
- * A specialized version of `.forEach` for arrays without support for callback
- * shorthands and `this` binding.
- *
- * {@link https://github.com/lodash/lodash/blob/master/lodash.js}
- *
- * @param {Array} array The array to iterate over.
- * @param {Function} iteratee The function invoked per iteration.
- * @returns {Array} Returns `array`.
- */
- export function arrayEach(array, iteratee) {
- var index = -1,
- length = array.length;
- while (++index < length) {
- if (iteratee(array[index], index, array) === false) {
- break;
- }
- }
- return array;
- }
- /**
- * Calculate sum value for each item of the array.
- *
- * @param {Array} array The array to process.
- * @returns {Number} Returns calculated sum value.
- */
- export function arraySum(array) {
- return arrayReduce(array, function (a, b) {
- return a + b;
- }, 0);
- }
- /**
- * Returns the highest value from an array. Can be array of numbers or array of strings.
- * NOTICE: Mixed values is not supported.
- *
- * @param {Array} array The array to process.
- * @returns {Number} Returns the highest value from an array.
- */
- export function arrayMax(array) {
- return arrayReduce(array, function (a, b) {
- return a > b ? a : b;
- }, Array.isArray(array) ? array[0] : void 0);
- }
- /**
- * Returns the lowest value from an array. Can be array of numbers or array of strings.
- * NOTICE: Mixed values is not supported.
- *
- * @param {Array} array The array to process.
- * @returns {Number} Returns the lowest value from an array.
- */
- export function arrayMin(array) {
- return arrayReduce(array, function (a, b) {
- return a < b ? a : b;
- }, Array.isArray(array) ? array[0] : void 0);
- }
- /**
- * Calculate average value for each item of the array.
- *
- * @param {Array} array The array to process.
- * @returns {Number} Returns calculated average value.
- */
- export function arrayAvg(array) {
- if (!array.length) {
- return 0;
- }
- return arraySum(array) / array.length;
- }
- /**
- * Flatten multidimensional array.
- *
- * @param {Array} array Array of Arrays
- * @returns {Array}
- */
- export function arrayFlatten(array) {
- return arrayReduce(array, function (initial, value) {
- return initial.concat(Array.isArray(value) ? arrayFlatten(value) : value);
- }, []);
- }
- /**
- * Unique values in the array.
- *
- * @param {Array} array The array to process.
- * @returns {Array}
- */
- export function arrayUnique(array) {
- var unique = [];
- arrayEach(array, function (value) {
- if (unique.indexOf(value) === -1) {
- unique.push(value);
- }
- });
- return unique;
- }
|