closed-interval v1.2.0
closed-interval
closed-interval is a TypeScript port of the Apache Commons Range class offered for the JVM based languages.
The API aims to be identical to Apache Commons, with a few exceptions where the TypeScript implementation is simpler or not applicable.
Example usage
As usual, install the package
npm i closed-intervaland use the Range class in your code:
import Range from 'closed-interval'
// or as a CommonJS module
// const { default: Range } = require('closed-interval')
const range = Range.between(10, 100)
const containsOtherRange = range.contains(Range.between(40, 50)) // true
const isOverlappedByOtherRange = range.isOverlappedBy(Range.between(40, 50)) // falseAPI
Since the API closely resembles the implementation of Apache Commons, the documentation below borrows most of its descriptions from the relevant javadoc.
Range.between
Range.between<T>(fromInclusive: T, toInclusive: T, comparator: Comparator<T> | null = null): Range<T>Deprecated Use Range.of
Range.of
Range.of<T>(fromInclusive: T, toInclusive: T, comparator: Comparator<T> | null = null): Range<T>Creates a range with the specified minimum and maximum values (both inclusive).
The range uses the natural ordering of the elements (when applicable) to determine where values lie in the range unless a comparator is passed.
The arguments may be passed in the order (min,max) or (max,min). The getMinimum and getMaximum methods will return the correct values.
Range.is
Range.is<T>(element: T, comparator: Comparator<T> | null = null): Range<T>Creates a range using the specified element as both the minimum and maximum in this range.
The range uses the natural ordering of the elements (when applicable) to determine where values lie in the range unless a comparator is passed.
contains
contains(element: T): booleanChecks whether the specified element occurs within this range.
containsRange
containsRange(other: Range<T>): booleanChecks whether this range contains all the elements of the specified range.
This method may fail if the ranges have two different comparators.
elementCompareTo
elementCompareTo(element: T): -1 | 0 | 1Checks where the specified element occurs relative to this range.
fit
fit(element: T): TFits the given element into this range by returning the given element or, if out of bounds, the range minimum if below, or the range maximum if above.
getComparator
getComparator(): Comparator<T>Gets the comparator being used to determine if objects are within the range.
Natural ordering uses an internal comparator implementation, thus this method never returns null.
getMaximum
getMaximum(): TGets the maximum value in this range.
getMinimum
getMinimum(): TGets the minimum value in this range.
intersectionWith
intersectionWith(other: Range<T>): Range<T>Calculate the intersection of this and an overlapping Range.
isAfter
isAfter(element: T): booleanChecks whether this range is after the specified element.
isAfterRange
isAfterRange(otherRange: Range<T>): booleanChecks whether this range is completely after the specified range.
This method may fail if the ranges have two different comparators.
isBefore
isBefore(element: T): booleanChecks whether this range is before the specified element.
isBeforeRange
isBeforeRange(otherRange: Range<T>): booleanChecks whether this range is completely before the specified range.
This method may fail if the ranges have two different comparators.
isEndedBy
isEndedBy(element: T): booleanChecks whether this range ends with the specified element.
isNaturalOrdering
isNaturalOrdering(): booleanWhether or not the Range is using the natural ordering of the elements.
Natural ordering uses an internal comparator implementation, thus this method is the only way to check if a null comparator was specified.
isOverlappedBy
isOverlappedBy(otherRange: Range<T>): booleanChecks whether this range is overlapped by the specified range.
Two ranges overlap if there is at least one element in common.
This method may fail if the ranges have two different comparators.
isStartedBy
isStartedBy(element: T): booleanChecks whether this range starts with the specified element.
toString
toString(): stringGets the range as a string.
The format of the string is 'min..max'.