Range.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 ? /** @type {RangeValueCallback} */
  70. ([value]) => value <= minMax : /** @type {RangeValueCallback} */
  71. ([value]) => value >= minMax;
  72. for (let i = 0; i < values.length; i++) {
  73. if (predicate(values[i])) {
  74. [minMax] = values[i];
  75. j = i;
  76. }
  77. }
  78. if (j > -1) {
  79. return values[j];
  80. }
  81. return [Infinity, true];
  82. }
  83. constructor() {
  84. /** @type {Array<RangeValue>} */
  85. this._left = [];
  86. /** @type {Array<RangeValue>} */
  87. this._right = [];
  88. }
  89. /**
  90. * @param {number} value
  91. * @param {boolean=} exclusive
  92. */
  93. left(value, exclusive = false) {
  94. this._left.push([value, exclusive]);
  95. }
  96. /**
  97. * @param {number} value
  98. * @param {boolean=} exclusive
  99. */
  100. right(value, exclusive = false) {
  101. this._right.push([value, exclusive]);
  102. }
  103. /**
  104. * @param {boolean} logic is not logic applied
  105. * @return {string} "smart" range string representation
  106. */
  107. format(logic = true) {
  108. const [start, leftExclusive] = Range.getRangeValue(this._left, logic);
  109. const [end, rightExclusive] = Range.getRangeValue(this._right, !logic);
  110. if (!Number.isFinite(start) && !Number.isFinite(end)) {
  111. return "";
  112. }
  113. const realStart = leftExclusive ? start + 1 : start;
  114. const realEnd = rightExclusive ? end - 1 : end;
  115. // e.g. 5 < x < 7, 5 < x <= 6, 6 <= x <= 6
  116. if (realStart === realEnd) {
  117. return `should be ${logic ? "" : "!"}= ${realStart}`;
  118. }
  119. // e.g. 4 < x < ∞
  120. if (Number.isFinite(start) && !Number.isFinite(end)) {
  121. return Range.formatLeft(start, logic, leftExclusive);
  122. }
  123. // e.g. ∞ < x < 4
  124. if (!Number.isFinite(start) && Number.isFinite(end)) {
  125. return Range.formatRight(end, logic, rightExclusive);
  126. }
  127. return Range.formatRange(start, end, leftExclusive, rightExclusive, logic);
  128. }
  129. }
  130. module.exports = Range;