1.1.0 • Published 1 year ago

closed-interval v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

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-interval

and 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)) // false

API

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>

Obtains 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>

Obtains 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): boolean

Checks whether the specified element occurs within this range.

containsRange

containsRange(other: Range<T>): boolean

Checks 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 | 1

Checks where the specified element occurs relative to this range.

fit

fit(element: T): T

Fits 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(): T

Gets the maximum value in this range.

getMinimum

getMinimum(): T

Gets 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): boolean

Checks whether this range is after the specified element.

isAfterRange

isAfterRange(otherRange: Range<T>): boolean

Checks whether this range is completely after the specified range.

This method may fail if the ranges have two different comparators.

isBefore

isBefore(element: T): boolean

Checks whether this range is before the specified element.

isBeforeRange

isBeforeRange(otherRange: Range<T>): boolean

Checks whether this range is completely before the specified range.

This method may fail if the ranges have two different comparators.

isEndedBy

isEndedBy(element: T): boolean

Checks whether this range ends with the specified element.

isNaturalOrdering

isNaturalOrdering(): boolean

Whether 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>): boolean

Checks 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): boolean

Checks whether this range starts with the specified element.

toString

toString(): string

Gets the range as a string.

The format of the string is 'min..max'.

1.1.0

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago