UNPKG

@helpwave/hightide

Version:

helpwave's component and theming library

120 lines 3.41 kB
// src/util/array.ts var equalSizeGroups = (array, groupSize) => { if (groupSize <= 0) { console.warn(`group size should be greater than 0: groupSize = ${groupSize}`); return [[...array]]; } const groups = []; for (let i = 0; i < array.length; i += groupSize) { groups.push(array.slice(i, Math.min(i + groupSize, array.length))); } return groups; }; var defaultRangeOptions = { allowEmptyRange: false, stepSize: 1, exclusiveStart: false, exclusiveEnd: true }; var range = (endOrRange, options) => { const { allowEmptyRange, stepSize, exclusiveStart, exclusiveEnd } = { ...defaultRangeOptions, ...options }; let start = 0; let end; if (typeof endOrRange === "number") { end = endOrRange; } else { start = endOrRange[0]; end = endOrRange[1]; } if (!exclusiveEnd) { end -= 1; } if (exclusiveStart) { start += 1; } if (end - 1 < start) { if (!allowEmptyRange) { console.warn(`range: end (${end}) < start (${start}) should be allowed explicitly, set options.allowEmptyRange to true`); } return []; } return Array.from({ length: end - start }, (_, index) => index * stepSize + start); }; var closestMatch = (list, firstCloser) => { return list.reduce((item1, item2) => { return firstCloser(item1, item2) ? item1 : item2; }); }; var getNeighbours = (list, item, neighbourDistance = 2) => { const index = list.indexOf(item); const totalItems = neighbourDistance * 2 + 1; if (list.length < totalItems) { console.warn("List is to short"); return list; } if (index === -1) { console.error("item not found in list"); return list.splice(0, totalItems); } let start = index - neighbourDistance; if (start < 0) { start += list.length; } const end = (index + neighbourDistance + 1) % list.length; const result = []; let ignoreOnce = list.length === totalItems; for (let i = start; i !== end || ignoreOnce; i = (i + 1) % list.length) { result.push(list[i]); if (end === i && ignoreOnce) { ignoreOnce = false; } } return result; }; var createLoopingListWithIndex = (list, startIndex = 0, length = 0, forwards = true) => { if (length < 0) { console.warn(`createLoopingList: length must be >= 0, given ${length}`); } else if (length === 0) { length = list.length; } const returnList = []; if (forwards) { for (let i = startIndex; returnList.length < length; i = (i + 1) % list.length) { returnList.push([i, list[i]]); } } else { for (let i = startIndex; returnList.length < length; i = i === 0 ? i = list.length - 1 : i - 1) { returnList.push([i, list[i]]); } } return returnList; }; var createLoopingList = (list, startIndex = 0, length = 0, forwards = true) => { return createLoopingListWithIndex(list, startIndex, length, forwards).map(([_, item]) => item); }; var ArrayUtil = { unique: (list) => { const seen = /* @__PURE__ */ new Set(); return list.filter((item) => { if (seen.has(item)) { return false; } seen.add(item); return true; }); }, difference: (list, removeList) => { const remove = new Set(removeList); return list.filter((item) => !remove.has(item)); } }; export { ArrayUtil, closestMatch, createLoopingList, createLoopingListWithIndex, equalSizeGroups, getNeighbours, range }; //# sourceMappingURL=array.mjs.map