@oleksii-pavlov/ranges
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

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/ranges

Usage

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(): RangeValues

Returns the RangeValues object containing the min and max values of the range.

within(value: number)
within(value: number): number

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

Checks if the value is within the range (min, max) (exclusive).

isGreaterThanMinOrEqualAndLessThanMax(value: number)
isGreaterThanMinOrEqualAndLessThanMax(value: number): boolean

Checks if the value is within the range [min, max) (inclusive of min and exclusive of max).

isGreaterThanMinAndLessThanMaxOrEqual(value: number)
isGreaterThanMinAndLessThanMaxOrEqual(value: number): boolean

Checks if the value is within the range (min, max] (exclusive of min and inclusive of max).

isGreaterThanMinOrEqualAndLessThanMaxOrEqual(value: number)
isGreaterThanMinOrEqualAndLessThanMaxOrEqual(value: number): boolean

Checks if the value is within the range [min, max] (inclusive).

getIntersection(rangeValues: RangeValues)
getIntersection(rangeValues: RangeValues): Range | null

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

Checks if the current range is entirely within another range defined by rangeValues.

containsRange(rangeValues: RangeValues)
containsRange(rangeValues: RangeValues): boolean

Checks if the current range entirely contains another range defined by rangeValues.

mapValueToRange Function

export function mapValueToRange(value: number): RangeValues

Maps 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: 10

Checking 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: true

Finding 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: true

Checking 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: true

Mapping a Single Value to a Range

const rangeValues = mapValueToRange(5);
console.log(rangeValues); // Output: { min: 5, max: 5 }

Readme

Keywords

Package Sidebar

Install

npm i @oleksii-pavlov/ranges

Weekly Downloads

2

Version

1.0.0

License

ISC

Unpacked Size

24.1 kB

Total Files

10

Last publish

Collaborators

  • alekseypavlov9