t-comm
Version:
专业、稳定、纯粹的工具库
33 lines (31 loc) • 845 B
JavaScript
function getStrListLength(list) {
return list.reduce(function (acc, item) {
return acc + item.length;
}, 0);
}
function splitLongList(list) {
var threshold = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 3800;
var max = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 3;
var strLen = getStrListLength(list);
if (strLen <= threshold) {
return [list];
}
var result = [];
var temp = [];
for (var i = 0; i < list.length; i++) {
var item = list[i];
temp.push(item);
var tempLen = getStrListLength(temp);
if (tempLen >= threshold) {
result.push(temp);
temp = [];
if (result.length >= max - 1) {
temp = list.slice(i + 1);
break;
}
}
}
result.push(temp);
return result;
}
export { getStrListLength, splitLongList };