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