Range.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. "use strict";
  2. /**
  3. * @typedef {[number, boolean]} RangeValue
  4. */
  5. /**
  6. * @callback RangeValueCallback
  7. * @param {RangeValue} rangeValue
  8. * @returns {boolean}
  9. */
  10. class Range {
  11. /**
  12. * @param {"left" | "right"} side
  13. * @param {boolean} exclusive
  14. * @returns {">" | ">=" | "<" | "<="}
  15. */
  16. static getOperator(side, exclusive) {
  17. if (side === "left") {
  18. return exclusive ? ">" : ">=";
  19. }
  20. return exclusive ? "<" : "<=";
  21. }
  22. /**
  23. * @param {number} value
  24. * @param {boolean} logic is not logic applied
  25. * @param {boolean} exclusive is range exclusive
  26. * @returns {string}
  27. */
  28. static formatRight(value, logic, exclusive) {
  29. if (logic === false) {
  30. return Range.formatLeft(value, !logic, !exclusive);
  31. }
  32. return `should be ${Range.getOperator("right", exclusive)} ${value}`;
  33. }
  34. /**
  35. * @param {number} value
  36. * @param {boolean} logic is not logic applied
  37. * @param {boolean} exclusive is range exclusive
  38. * @returns {string}
  39. */
  40. static formatLeft(value, logic, exclusive) {
  41. if (logic === false) {
  42. return Range.formatRight(value, !logic, !exclusive);
  43. }
  44. return `should be ${Range.getOperator("left", exclusive)} ${value}`;
  45. }
  46. /**
  47. * @param {number} start left side value
  48. * @param {number} end right side value
  49. * @param {boolean} startExclusive is range exclusive from left side
  50. * @param {boolean} endExclusive is range exclusive from right side
  51. * @param {boolean} logic is not logic applied
  52. * @returns {string}
  53. */
  54. static formatRange(start, end, startExclusive, endExclusive, logic) {
  55. let result = "should be";
  56. result += ` ${Range.getOperator(logic ? "left" : "right", logic ? startExclusive : !startExclusive)} ${start} `;
  57. result += logic ? "and" : "or";
  58. result += ` ${Range.getOperator(logic ? "right" : "left", logic ? endExclusive : !endExclusive)} ${end}`;
  59. return result;
  60. }
  61. /**
  62. * @param {Array<RangeValue>} values
  63. * @param {boolean} logic is not logic applied
  64. * @return {RangeValue} computed value and it's exclusive flag
  65. */
  66. static getRangeValue(values, logic) {
  67. let minMax = logic ? Infinity : -Infinity;
  68. let j = -1;
  69. const predicate = logic ?
  70. /** @type {RangeValueCallback} */
  71. ([value]) => value <= minMax :
  72. /** @type {RangeValueCallback} */
  73. ([value]) => value >= minMax;
  74. for (let i = 0; i < values.length; i++) {
  75. if (predicate(values[i])) {
  76. [minMax] = values[i];
  77. j = i;
  78. }
  79. }
  80. if (j > -1) {
  81. return values[j];
  82. }
  83. return [Infinity, true];
  84. }
  85. constructor() {
  86. /** @type {Array<RangeValue>} */
  87. this._left = [];
  88. /** @type {Array<RangeValue>} */
  89. this._right = [];
  90. }
  91. /**
  92. * @param {number} value
  93. * @param {boolean=} exclusive
  94. */
  95. left(value, exclusive = false) {
  96. this._left.push([value, exclusive]);
  97. }
  98. /**
  99. * @param {number} value
  100. * @param {boolean=} exclusive
  101. */
  102. right(value, exclusive = false) {
  103. this._right.push([value, exclusive]);
  104. }
  105. /**
  106. * @param {boolean} logic is not logic applied
  107. * @return {string} "smart" range string representation
  108. */
  109. format(logic = true) {
  110. const [start, leftExclusive] = Range.getRangeValue(this._left, logic);
  111. const [end, rightExclusive] = Range.getRangeValue(this._right, !logic);
  112. if (!Number.isFinite(start) && !Number.isFinite(end)) {
  113. return "";
  114. }
  115. const realStart = leftExclusive ? start + 1 : start;
  116. const realEnd = rightExclusive ? end - 1 : end; // e.g. 5 < x < 7, 5 < x <= 6, 6 <= x <= 6
  117. if (realStart === realEnd) {
  118. return `should be ${logic ? "" : "!"}= ${realStart}`;
  119. } // e.g. 4 < x < ∞
  120. if (Number.isFinite(start) && !Number.isFinite(end)) {
  121. return Range.formatLeft(start, logic, leftExclusive);
  122. } // e.g. ∞ < x < 4
  123. if (!Number.isFinite(start) && Number.isFinite(end)) {
  124. return Range.formatRight(end, logic, rightExclusive);
  125. }
  126. return Range.formatRange(start, end, leftExclusive, rightExclusive, logic);
  127. }
  128. }
  129. module.exports = Range;