@helpwave/hightide
Version:
helpwave's component and theming library
96 lines (95 loc) • 3.79 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/loopingArray.ts
var loopingArray_exports = {};
__export(loopingArray_exports, {
LoopingArrayCalculator: () => LoopingArrayCalculator
});
module.exports = __toCommonJS(loopingArray_exports);
var LoopingArrayCalculator = class _LoopingArrayCalculator {
constructor(length, isLooping = true, allowedOverScroll = 0.1) {
if (allowedOverScroll < 0 || length < 1) {
throw new Error("Invalid parameters: allowedOverScroll >= 0 and length >= 1 must be true");
}
this.length = length;
this.isLooping = isLooping;
this.allowedOverScroll = allowedOverScroll;
}
getCorrectedPosition(position) {
if (!this.isLooping) {
return Math.max(-this.allowedOverScroll, Math.min(this.allowedOverScroll + this.length - 1, position));
}
if (position >= this.length) {
return position % this.length;
}
if (position < 0) {
return this.length - Math.abs(position) % this.length;
}
return position;
}
static withoutOffset(position) {
return position + _LoopingArrayCalculator.getOffset(position);
}
static getOffset(position) {
return Math.round(position) - position;
}
/**
* @return absolute distance forwards or Infinity when the target cannot be reached (only possible when not isLooping)
*/
getDistanceDirectional(position, target, direction) {
if (!this.isLooping && (position < -this.allowedOverScroll || position > this.allowedOverScroll + this.length - 1)) {
throw new Error("Invalid parameters: position is out of bounds.");
}
const isForwardInvalid = direction === 1 && position > target;
const isBackwardInvalid = direction === -1 && target < position;
if (!this.isLooping && (isForwardInvalid || isBackwardInvalid)) {
return Infinity;
}
if (direction === -1) {
return this.getDistanceDirectional(target, position, 1);
}
position = this.getCorrectedPosition(position);
target = this.getCorrectedPosition(target);
let distance = (target - position) * direction;
if (distance < 0) {
distance = this.length - Math.abs(position) % this.length + target;
}
return distance;
}
getDistanceForward(position, target) {
return this.getDistanceDirectional(position, target, 1);
}
getDistanceBackward(position, target) {
return this.getDistanceDirectional(position, target, -1);
}
getDistance(position, target) {
const forwardDistance = this.getDistanceForward(position, target);
const backwardDistance = this.getDistanceBackward(position, target);
return Math.min(forwardDistance, backwardDistance);
}
getBestDirection(position, target) {
const forwardDistance = this.getDistanceForward(position, target);
const backwardDistance = this.getDistanceBackward(position, target);
return forwardDistance < backwardDistance ? 1 : -1;
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
LoopingArrayCalculator
});
//# sourceMappingURL=loopingArray.js.map