the target type stored by this range.
A custom spec
is required unless it's coercible to number
.
Extra care is required around handling empty ranges,
since they may have their bounds set to null
if created with Range.empty
.
Note: since it's not possible to make an infinite bound inclusive,
relevant constructor methods override the requested bound type for values that
conform to RangeSpec.isInfinity
.
Tells whether this is an empty range, i.e. the one including no elements.
Tells whether the spec
this range is built upon returns isInfinity -> true
for its lower bound.
Tells whether the spec
this range is built upon returns isInfinity -> true
for its upper bound.
Tests whether this range is adjacent on either side with the other
.
See Range.adjacentLeft
and Range.adjacentRight
.
The range to check this
against.
Tests whether other
is adjacent to this
on the left, i.e. other.upper
is connected
to this.lower
, but does not intersect with it.
Range.open(10, 20).adjacentLeft(Range.close(0, 10)); // -> true
Range.open(10, 20).adjacentLeft(Range.empty()); // -> false
Range.open(10, 20).adjacentLeft(Range.singleton(10)); // -> true
The range that is expected to be connected to this
on the left.
Tests whether other
is adjacent to this
on the right, i.e. other.lower
is connected
to this.upper
, but does not intersect with it.
Range.open(10, 20).adjacentLeft(Range.close(20, 30)); // -> true
Range.open(10, 20).adjacentLeft(Range.empty()); // -> false
Range.open(10, 20).adjacentLeft(Range.singleton(20)); // -> true
The range that is expected to be connected to this
on the right.
Tests whether this range contains the entire other
.
Empty ranges are contained in any other range, including another empty range.
Range.open(10, 20).contains(Range.open(5, 7)); // -> true
Range.open(10, 20).contains(Range.open(10, 20)); // -> true
Range.close(10, 20).contains(Range.open(10, 20)); // -> true
Range.open(10, 20).contains(Range.close(10, 20)); // -> false
Range.empty().contains(Range.open(50, 50)); // -> true
Range.singleton(10).contains(Range.empty()); // -> true
The range to test containment against.
Tests whether this range contains the element
.
Range.open(10, 20).contains(5); // -> true
Range.open(10, 20).contains(10); // -> false
Range.close(10, 20).contains(10); // -> true
Range.empty().contains(0); // -> false
Range.singleton(10).contains(10); // -> true
The element to test containment against.
Tests whether this
has the same bounds & bound inclusion as other
.
Empty ranges are always equal, regardless of what bounds they were built with.
Range.open(10, 20).equals(Range.open(10, 20)); // -> true
Range.empty().equals(Range.open(50, 50)); // -> true
Range.singleton(10).equals(Range.empty()); // -> false
The range expected to be equal to this
.
Returns an intersection of this range with other
.
The result of range intersection shall include the narrowest range that includes all the elements present in both of the source ranges.
Range.open(10, 20).intersection(Range.closeOpen(12, 16)); // -> [12; 16)
Range.open(10, 20).intersection(Range.empty()); // -> {}, an empty range
Range.empty().intersection(Range.close(10, 20)); // -> {}, an empty range
The right-hand side argument of the intersection.
Returns parts of this range, excluding all the elements present in the other
one.
Range.open(10, 20).subtract(Range.closeOpen(12, 16)); // -> [ (10; 12), [16; 20) ]
Range.open(10, 20).subtract(Range.empty()); // (10; 20)
Range.empty().subtract(Range.close(10, 20)); // -> [], an empty array
The range to subtract from this
.
A list of ranges containing all the elements present in this
, but not in other
. The list does not
include any empty ranges. The resulting list will be empty if other
contains this
.
Returns this range's textual representation.
A mathematical representation of this range, relying on lower
and upper
's toString
.
Returns {}
for empty range.
Returns a union of this range with other
.
The united range is the narrowest range that includes all the elements from both of the source ranges.
Union is impossible for non-intersecting ranges, so it might be a good idea to check if they are intersecting/adjacent to avoid excptions:
const r1 = Range.open(10, 20);
const r2 = Range.close(30, 40);
if (!r1.intersection(r2).isEmpty || r1.isAdjacent(r2)) {
const united = r1.union(r2);
// Do something with this united range
}
Examples:
Range.open(10, 20).union(Range.closeOpen(15, 25)); // -> (10; 25)
Range.open(10, 20).union(Range.empty()); // -> (10; 20)
Range.empty().union(Range.close(10, 20)); // -> (10; 20)
Range.open(10, 20).union(Range.singleton(50)); // -> Error("Cannot union non-intersecting and non-adjacent ranges")
The right-hand side argument of the intersection.
Create a closed range, i.e. the one that includes both its bounds.
const instance: Range<number> = Range.close(-1, 1); // [-1; 1]
It can also be used to implicitly create a singleton range:
Range.close(10, 10).isEqual(Range.singleton(10)); // -> true
Bounds are forcibly overriden to open for infinity bounds, i.e.
bound values that conform to RangeSpec.isInfinity
.
the target type to b represented by the resulting range.
A custom spec
is required unless it's coercible to number
.
A lower bound of the resulting range, included.
An upper bound of the resulting range, included.
Optional. See RangeSpec
.
Create a close-open range, i.e. the one that only includes its lower bound.
const instance: Range<number> = Range.closeOpen(-1, 1); // [-1; 1)
It can also be used to implicitly create an empty range:
const instance: Range<number> = Range.closeOpen(10, 10); // {}, an empty range
Bounds are forcibly overriden to open for infinity bounds, i.e.
bound values that conform to RangeSpec.isInfinity
.
the target type to b represented by the resulting range.
A custom spec
is required unless it's coercible to number
.
A lower bound of the resulting range, included.
An upper bound of the resulting range, excluded.
Optional. See RangeSpec
.
Create an empty range, i.e. the one that doesn't include anything.
const instance: Range<number> = Range.empty(); // {}, an empty range
the target type to b represented by the resulting range.
A custom spec
is required unless it's coercible to number
.
Create an open range, i.e. the one that does not include either of its bounds.
const instance: Range<number> = Range.open(-1, 1); // (-1; 1)
It can also be used to implicitly create an empty range:
const instance: Range<number> = Range.open(10, 10); // {}, an empty range
the target type to b represented by the resulting range.
A custom spec
is required unless it's coercible to number
.
A lower bound of the resulting range, excluded.
An upper bound of the resulting range, excluded.
Optional. See RangeSpec
.
Create an open-close range, i.e. one that only includes its upper bound.
const instance: Range<number> = Range.openClose(-1, 1); // (-1; 1]
It can also be used to implicitly create an empty range:
const instance: Range<number> = Range.openClose(10, 10); // {}, an empty range
Bounds are forcibly overriden to open for infinity bounds, i.e.
bound values that conform to RangeSpec.isInfinity
.
the target type to b represented by the resulting range.
A custom spec
is required unless it's coercible to number
.
A lower bound of the resulting range, excluded.
An upper bound of the resulting range, included.
Optional. See RangeSpec
.
Create a singleton range, i.e. the one that only includes a single value
const instance: Range<number> = Range.singleton(5); // [5; 5]
Note: a singleton instance cannot be created out of a [[RangeSpec.infinity
]] value,
since infinity cannot be enclosed in an interval.
the target type to b represented by the resulting range.
A custom spec
is required unless it's coercible to number
.
The value the resulting range will include.
Optional. See RangeSpec
.
Generated using TypeDoc
A convenience range/interval type.
Built around
number
by default, it supports custom types through thespec
argument of its constructor methods.Examples:
This class is fully immutable.