@helpwave/hightide
Version:
helpwave's component and theming library
150 lines (149 loc) • 4.65 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/util/array.ts
var array_exports = {};
__export(array_exports, {
ArrayUtil: () => ArrayUtil,
closestMatch: () => closestMatch,
createLoopingList: () => createLoopingList,
createLoopingListWithIndex: () => createLoopingListWithIndex,
equalSizeGroups: () => equalSizeGroups,
getNeighbours: () => getNeighbours,
range: () => range
});
module.exports = __toCommonJS(array_exports);
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));
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
ArrayUtil,
closestMatch,
createLoopingList,
createLoopingListWithIndex,
equalSizeGroups,
getNeighbours,
range
});
//# sourceMappingURL=array.js.map