translateOffset.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.translateOffset = translateOffset;
  4. let warned = false;
  5. function translateOffset(start, fromOffsets, toOffsets, fromLengths, toLengths = fromLengths) {
  6. const isSorted = fromOffsets.every((value, index) => index === 0 || fromOffsets[index - 1] <= value);
  7. if (!isSorted) {
  8. for (let i = 0; i < fromOffsets.length; i++) {
  9. const fromOffset = fromOffsets[i];
  10. const fromLength = fromLengths[i];
  11. if (start >= fromOffset && start <= fromOffset + fromLength) {
  12. const toLength = toLengths[i];
  13. const toOffset = toOffsets[i];
  14. let rangeOffset = Math.min(start - fromOffset, toLength);
  15. return toOffset + rangeOffset;
  16. }
  17. }
  18. if (!warned) {
  19. warned = true;
  20. console.warn('fromOffsets should be sorted in ascending order');
  21. }
  22. }
  23. let low = 0;
  24. let high = fromOffsets.length - 1;
  25. while (low <= high) {
  26. const mid = Math.floor((low + high) / 2);
  27. const fromOffset = fromOffsets[mid];
  28. const fromLength = fromLengths[mid];
  29. if (start >= fromOffset && start <= fromOffset + fromLength) {
  30. const toLength = toLengths[mid];
  31. const toOffset = toOffsets[mid];
  32. let rangeOffset = Math.min(start - fromOffset, toLength);
  33. return toOffset + rangeOffset;
  34. }
  35. else if (start < fromOffset) {
  36. high = mid - 1;
  37. }
  38. else {
  39. low = mid + 1;
  40. }
  41. }
  42. }
  43. //# sourceMappingURL=translateOffset.js.map