@helpwave/hightide
Version:
helpwave's component and theming library
1 lines • 4.87 kB
Source Map (JSON)
{"version":3,"sources":["../../src/util/loopingArray.ts"],"sourcesContent":["/**\n * 1 is forwards\n *\n * -1 is backwards\n */\nexport type Direction = 1 | -1\n\nexport class LoopingArrayCalculator {\n length: number\n isLooping: boolean\n allowedOverScroll: number\n\n constructor(length: number, isLooping: boolean = true, allowedOverScroll: number = 0.1) {\n if (allowedOverScroll < 0 || length < 1) {\n throw new Error('Invalid parameters: allowedOverScroll >= 0 and length >= 1 must be true')\n }\n\n this.length = length\n this.isLooping = isLooping\n this.allowedOverScroll = allowedOverScroll\n }\n\n getCorrectedPosition(position: number): number {\n if (!this.isLooping) {\n return Math.max(-this.allowedOverScroll, Math.min(this.allowedOverScroll + this.length - 1, position))\n }\n if (position >= this.length) {\n return position % this.length\n }\n if (position < 0) {\n return this.length - (Math.abs(position) % this.length)\n }\n return position\n }\n\n static withoutOffset(position: number): number {\n return position + LoopingArrayCalculator.getOffset(position)\n }\n\n static getOffset(position: number): number {\n return Math.round(position) - position // For example: 45.5 => 46 - 45.5 = 0.5\n }\n\n /**\n * @return absolute distance forwards or Infinity when the target cannot be reached (only possible when not isLooping)\n */\n getDistanceDirectional(position: number, target: number, direction: Direction): number {\n if (!this.isLooping && (position < -this.allowedOverScroll || position > this.allowedOverScroll + this.length - 1)) {\n throw new Error('Invalid parameters: position is out of bounds.')\n }\n\n const isForwardInvalid = (direction === 1 && position > target)\n const isBackwardInvalid = (direction === -1 && target < position)\n\n if (!this.isLooping && (isForwardInvalid || isBackwardInvalid)) {\n return Infinity\n }\n\n if (direction === -1) {\n return this.getDistanceDirectional(target, position, 1)\n }\n\n position = this.getCorrectedPosition(position)\n target = this.getCorrectedPosition(target)\n\n let distance = (target - position) * direction\n if (distance < 0) {\n distance = this.length - (Math.abs(position) % this.length) + target\n }\n\n return distance\n }\n\n getDistanceForward(position: number, target: number): number {\n return this.getDistanceDirectional(position, target, 1)\n }\n\n getDistanceBackward(position: number, target: number): number {\n return this.getDistanceDirectional(position, target, -1)\n }\n\n getDistance(position: number, target: number): number {\n const forwardDistance = this.getDistanceForward(position, target)\n const backwardDistance = this.getDistanceBackward(position, target)\n\n return Math.min(forwardDistance, backwardDistance)\n }\n\n getBestDirection(position: number, target: number): Direction {\n const forwardDistance = this.getDistanceForward(position, target)\n const backwardDistance = this.getDistanceBackward(position, target)\n return forwardDistance < backwardDistance ? 1 : -1\n }\n}\n"],"mappings":";AAOO,IAAM,yBAAN,MAAM,wBAAuB;AAAA,EAKlC,YAAY,QAAgB,YAAqB,MAAM,oBAA4B,KAAK;AACtF,QAAI,oBAAoB,KAAK,SAAS,GAAG;AACvC,YAAM,IAAI,MAAM,yEAAyE;AAAA,IAC3F;AAEA,SAAK,SAAS;AACd,SAAK,YAAY;AACjB,SAAK,oBAAoB;AAAA,EAC3B;AAAA,EAEA,qBAAqB,UAA0B;AAC7C,QAAI,CAAC,KAAK,WAAW;AACnB,aAAO,KAAK,IAAI,CAAC,KAAK,mBAAmB,KAAK,IAAI,KAAK,oBAAoB,KAAK,SAAS,GAAG,QAAQ,CAAC;AAAA,IACvG;AACA,QAAI,YAAY,KAAK,QAAQ;AAC3B,aAAO,WAAW,KAAK;AAAA,IACzB;AACA,QAAI,WAAW,GAAG;AAChB,aAAO,KAAK,SAAU,KAAK,IAAI,QAAQ,IAAI,KAAK;AAAA,IAClD;AACA,WAAO;AAAA,EACT;AAAA,EAEA,OAAO,cAAc,UAA0B;AAC7C,WAAO,WAAW,wBAAuB,UAAU,QAAQ;AAAA,EAC7D;AAAA,EAEA,OAAO,UAAU,UAA0B;AACzC,WAAO,KAAK,MAAM,QAAQ,IAAI;AAAA,EAChC;AAAA;AAAA;AAAA;AAAA,EAKA,uBAAuB,UAAkB,QAAgB,WAA8B;AACrF,QAAI,CAAC,KAAK,cAAc,WAAW,CAAC,KAAK,qBAAqB,WAAW,KAAK,oBAAoB,KAAK,SAAS,IAAI;AAClH,YAAM,IAAI,MAAM,gDAAg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