npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@oleksii-pavlov/ranges

v1.0.0

Published

The Ranges utils

Downloads

137

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