import isStr from './isStr';
import isArr from './isArr';
import randomNum from './randomNum';

/**
 * 数组或字符串随机排序
 * @param {Array|string} value
 * @returns
 *
 * @example
 * shuffle('abcdefg');             // => 'gcfaebd'
 * shuffle(['a', 'b', 'c', 'd']);  // => ['b', 'a', 'd', 'c']
 */
function shuffle(value) {
  var isValStr = isStr(value);
  if (isValStr) value = value.split('');
  if (!isArr(value)) value = [];
  var array = value.slice();
  var arr = [];

  while (array.length) {
    var rdIndex = randomNum(array.length);
    arr = arr.concat(array.splice(rdIndex, 1));
  }

  return isValStr ? arr.join('') : arr;
}

export default shuffle;
