UNPKG

t-comm

Version:

专业、稳定、纯粹的工具库

49 lines (46 loc) 978 B
import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray'; /** * 打乱数组顺序 * * @param {Array<any>} array - 数组 * @returns {Array<any>} 乱序后的数组 * * @example * * shuffle([1, 2, 3, 4, 5]) * * // [3, 2, 1, 4, 5] * */ function shuffle(array) { var arr = _toConsumableArray(array); var m = arr.length; while (m > 1) { var index = Math.floor(Math.random() * m); m -= 1; var _ref = [arr[index], arr[m]]; arr[m] = _ref[0]; arr[index] = _ref[1]; } return arr; } /** * 获取累积宽度 * @param {Array<number>} cellWidthList - 宽度列表 * @param {number} idx - 当前idx * @returns {number} 累计宽度 * * @example * * getAccCellWidth([20, 10, 20, 10], 1) * * // 30 */ function getAccCellWidth(cellWidthList, idx) { var res = 0; for (var i = 0; i <= Math.min(idx, cellWidthList.length - 1); i++) { res += cellWidthList[i]; } return res; } export { getAccCellWidth, shuffle };