123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- "use strict";
- class Range {
-
- static getOperator(side, exclusive) {
- if (side === 'left') {
- return exclusive ? '>' : '>=';
- }
- return exclusive ? '<' : '<=';
- }
-
- static formatRight(value, logic, exclusive) {
- if (logic === false) {
- return Range.formatLeft(value, !logic, !exclusive);
- }
- return `should be ${Range.getOperator('right', exclusive)} ${value}`;
- }
-
- static formatLeft(value, logic, exclusive) {
- if (logic === false) {
- return Range.formatRight(value, !logic, !exclusive);
- }
- return `should be ${Range.getOperator('left', exclusive)} ${value}`;
- }
-
- static formatRange(start, end, startExclusive, endExclusive, logic) {
- let result = 'should be';
- result += ` ${Range.getOperator(logic ? 'left' : 'right', logic ? startExclusive : !startExclusive)} ${start} `;
- result += logic ? 'and' : 'or';
- result += ` ${Range.getOperator(logic ? 'right' : 'left', logic ? endExclusive : !endExclusive)} ${end}`;
- return result;
- }
-
- static getRangeValue(values, logic) {
- let minMax = logic ? Infinity : -Infinity;
- let j = -1;
- const predicate = logic ?
-
- ([value]) => value <= minMax :
-
- ([value]) => value >= minMax;
- for (let i = 0; i < values.length; i++) {
- if (predicate(values[i])) {
- [minMax] = values[i];
- j = i;
- }
- }
- if (j > -1) {
- return values[j];
- }
- return [Infinity, true];
- }
- constructor() {
-
- this._left = [];
-
- this._right = [];
- }
-
- left(value, exclusive = false) {
- this._left.push([value, exclusive]);
- }
-
- right(value, exclusive = false) {
- this._right.push([value, exclusive]);
- }
-
- format(logic = true) {
- const [start, leftExclusive] = Range.getRangeValue(this._left, logic);
- const [end, rightExclusive] = Range.getRangeValue(this._right, !logic);
- if (!Number.isFinite(start) && !Number.isFinite(end)) {
- return '';
- }
- const realStart = leftExclusive ? start + 1 : start;
- const realEnd = rightExclusive ? end - 1 : end;
- if (realStart === realEnd) {
- return `should be ${logic ? '' : '!'}= ${realStart}`;
- }
- if (Number.isFinite(start) && !Number.isFinite(end)) {
- return Range.formatLeft(start, logic, leftExclusive);
- }
- if (!Number.isFinite(start) && Number.isFinite(end)) {
- return Range.formatRight(end, logic, rightExclusive);
- }
- return Range.formatRange(start, end, leftExclusive, rightExclusive, logic);
- }
- }
- module.exports = Range;
|