All files / addon/utils array-methods.js

100% Statements 5/5
100% Branches 0/0
100% Functions 3/3
100% Lines 4/4

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35                                    3x 3x 3x                       2x    
import { A } from '@ember/array';
 
/**
 * @public
 * @class utils
 */
 
/**
 * Given an array and an index, splits the array at index and pushes the first
 * chunk to the end of the second chunk.
 *
 * @private
 * @method arraySwap
 * @param {array} arr An array to split, shift and rejoin.
 * @param {number} index The index where the split should occur.
 * @return {array} The swapped/shifted array.
 */
export function arraySwap(arr, index) {
  const endOfArr = arr.slice(0, index);
  const beginOfArr = A(arr.slice(index));
  return beginOfArr.pushObjects(endOfArr);
}
 
/**
 * Flattens an array of arrays into a shallow array.
 *
 * @private
 * @method flatten
 * @param {arrayOfArrays} arr An array to flatten.
 * @return {array} The flattened array.
 */
export function flatten(arrayOfArrays) {
  return A(arrayOfArrays).reduce((a, b) => A(a).concat(b));
}