@oleksii-pavlov/ranges
v1.1.0
Published
The Ranges utils
Downloads
1
Readme
Range Utility Library
A utility library for handling and manipulating numerical ranges in JavaScript/TypeScript.
Installation
Install the package via npm:
npm install @oleksii-pavlov/rangesUsage
Import the Range class in your code:
import { Range } from '@oleksii-pavlov/ranges';API Reference
Range Class
Constructor
constructor(rangeValues: RangeValues)Initializes a Range instance with the specified rangeValues.
Methods
getValues()
getValues(): RangeValuesReturns the RangeValues object containing the min and max values of the range.
within(value: number)
within(value: number): numberConstrains a given value within the range. If the value is less than the range's min, it returns min. If the value is greater than the range's max, it returns max. Otherwise, it returns the value itself.
isGreaterThanMinAndLessThanMax(value: number)
isGreaterThanMinAndLessThanMax(value: number): booleanChecks if the value is within the range (min, max) (exclusive).
isGreaterThanMinOrEqualAndLessThanMax(value: number)
isGreaterThanMinOrEqualAndLessThanMax(value: number): booleanChecks if the value is within the range [min, max) (inclusive of min and exclusive of max).
isGreaterThanMinAndLessThanMaxOrEqual(value: number)
isGreaterThanMinAndLessThanMaxOrEqual(value: number): booleanChecks if the value is within the range (min, max] (exclusive of min and inclusive of max).
isGreaterThanMinOrEqualAndLessThanMaxOrEqual(value: number)
isGreaterThanMinOrEqualAndLessThanMaxOrEqual(value: number): booleanChecks if the value is within the range [min, max] (inclusive).
getIntersection(rangeValues: RangeValues)
getIntersection(rangeValues: RangeValues): Range | nullReturns the intersection of the current range with another range defined by rangeValues. If there is no intersection, it returns null.
isInRangeOf(rangeValues: RangeValues)
isInRangeOf(rangeValues: RangeValues): booleanChecks if the current range is entirely within another range defined by rangeValues.
containsRange(rangeValues: RangeValues)
containsRange(rangeValues: RangeValues): booleanChecks if the current range entirely contains another range defined by rangeValues.
mapValueToRange Function
export function mapValueToRange(value: number): RangeValuesMaps a single value to a RangeValues object where both min and max are equal to the given value.
RangeValues Interface
export interface RangeValues {
min: number;
max: number;
}Represents a range with a minimum (min) and a maximum (max) value.
Examples
Constraining a Value Within a Range
const range = new Range({ min: 1, max: 10 });
console.log(range.within(0)); // Output: 1
console.log(range.within(5)); // Output: 5
console.log(range.within(15)); // Output: 10Checking if a Value is Within a Specific Range
const range = new Range({ min: 1, max: 10 });
console.log(range.isGreaterThanMinAndLessThanMax(5)); // Output: true
console.log(range.isGreaterThanMinOrEqualAndLessThanMax(1)); // Output: true
console.log(range.isGreaterThanMinAndLessThanMaxOrEqual(10)); // Output: true
console.log(range.isGreaterThanMinOrEqualAndLessThanMaxOrEqual(5)); // Output: trueFinding the Intersection of Two Ranges
const range1 = new Range({ min: 1, max: 10 });
const range2 = new Range({ min: 5, max: 15 });
const intersection = range1.getIntersection(range2.getValues());
console.log(intersection?.getValues()); // Output: { min: 5, max: 10 }Checking if a Range is Within Another Range
const range1 = new Range({ min: 1, max: 10 });
const range2 = new Range({ min: 0, max: 20 });
console.log(range1.isInRangeOf(range2.getValues())); // Output: trueChecking if a Range Contains Another Range
const range1 = new Range({ min: 1, max: 10 });
const range2 = new Range({ min: 2, max: 8 });
console.log(range1.containsRange(range2.getValues())); // Output: trueChecking if a Range Contains Value
const range = new Range({ min: 1, max: 10 });
console.log(range.containsValue(3)); // Output: trueMapping a Single Value to a Range
const rangeValues = mapValueToRange(5);
console.log(rangeValues); // Output: { min: 5, max: 5 }