UNPKG

t-comm

Version:

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

58 lines (51 loc) 1.32 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var _toConsumableArray__default = /*#__PURE__*/_interopDefaultLegacy(_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__default["default"](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; } exports.getAccCellWidth = getAccCellWidth; exports.shuffle = shuffle;