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

text-to-range

v1.1.0

Published

Convert a user's range specification into numbers.

Downloads

5

Readme

Text To Range

Parse text range descriptions into objects with numerical ranges. It can handle many different formats of ranges and provides the results as a structured object.

Examples:

import { rangeToEquation, rangeToInterval, textToRange } from 'text-to-range';

const example1 = textToRange('<10 >7'));
console.log(example1);

// { minInclusive: false, min: 7, max: 10, maxInclusive: false }

console.log(rangeToEquation(example1));

// <7 >10

const example2 = textToRange('2 <= x <= 100');
console.log(example2);

// { minInclusive: true, min: 2, max: 100, maxInclusive: true }

console.log(rangeToInterval(example2));

// [2,100]

const example3 = textToRange('1, 4.5 to 8, [12,14), 19-22.18');
console.log(example3);

// {
//   or: [
//     { minInclusive: true, min: 1, max: 1, maxInclusive: true },
//     { minInclusive: true, min: 4.5, max: 8, maxInclusive: true },
//     { minInclusive: true, min: 12, max: 14, maxInclusive: false },
//     { minInclusive: true, min: 19, max: 22.18, maxInclusive: true }
//   ]
// }

console.log(rangeToEquation(example3));

// 1, >=4.5 <=8, >=12 <14, >=19 <=22.18

console.log(rangeToInterval(example3));

// 1, [4.5,8], [12,14), [19,22.18]

Using the Library

This is a typical npm library. Run npm install text-to-range and then include it in your project.

import { rangeToEquation, rangeToInterval, textToRange } from 'text-to-range';

From there, it's a matter of calling textToRange to create a range object or use another exported function to convert the range to a string.

This module could run in a browser. It has no dependencies. I'd recommend making sure you bundle it using Webpack or a similar tool. Also, it relies heavily on regular expressions, so the browser's really needs to support them. There are several sample in the tests that you could use in a browser to ensure that the functionality is operating as expected.

Object Format

Ranges will either be simple ranges or joined ranges. Simple ranges have the minimum, maximum, and flags indicating if the minimum/maximum should be included. If you parse >4, minInclusive will be false, but if you parse >=4 then it will be set to true.

// Simple range
{
    minInclusive: boolean;
    min: number | Number.NEGATIVE_INFINITY;
    max: number | Number.POSITIVE_INFINITY;
    maxInclusive: boolean;
}

The joined ranges have a single or property, which is an array of simple ranges. While technically there could be any number of array elements, the practical minimum from the parser is 2.

// Joined range
{
    or: SimpleRange[];
}

Range Specifications

This is a pretty flexible library and it supports a wide range of range specifications.

  • Single numbers, which are handled as both a minimum and a maximum: 54
  • Decimal numbers are allowed by themselves or at any other time: 28.9276
  • Negative numbers always work too: -2.3
  • Ranges using "to": 3 to 11
  • Ranges using ".." like bash syntax: 7..14
  • Ranges using hyphens (negatives work too): 5-8, -4--1
  • Ranges using other symbols: 3:12, 14~18
  • Ranges with infinity: -infinity to 1, 2 to INF
  • Alternate infinity syntax: *-0, 1-$
  • Comparisons: >10 <=20
  • Equalities: =18, ==19
  • Equation ranges: 1 <= x <= 100
  • Closed intervals, which include the endpoints: [1,2]
  • Open intervals, which do not include endpoints: (1,2) <3,4> {5,6} ]7,8[
  • Open intervals with mixed up markers: (1,2> <3,4}, {5,7[, ]7,8)
  • Mixed open/close intervals using any delimiters: (1,2] [3,4)

Reversed ranges are also supported, so one can use 9 to 5 and get [5,9]. If there is a problem parsing the ranges or no ranges are found, null is returned. If the range is conflicting as with >1 <1 then null is returned. When an octal number is presented, such a 012, then null is returned.

Joining ranges can use several different delimiters. Any of the above ranges can be joined, so formats like 1 to 5, [6,9] is perfectly acceptable.

  • Spaces
  • Some punctuation: , ;
  • Text: and or
  • Symbols that represent "and" or "or": + | || & &&

Converting To A String

Once a range is parsed, the conversion back to a standard string is fairly straightforward. Use the methods rangeToEquation() or rangeToInterval to get a format you prefer.

import { rangeToEquation, rangeToInterval, textToRange } from 'text-to-range';
const example4 = textToRange('1, 2.2 to 3.3, (4, 5)');
console.log(example4);

// {
//   or: [
//     { minInclusive: true, min: 1, max: 1, maxInclusive: true },
//     { minInclusive: true, min: 2.2, max: 3.3, maxInclusive: true },
//     { minInclusive: false, min: 4, max: 5, maxInclusive: false }
//   ]
// }

console.log(rangeToEquation(example4));

// 1, >=2.2 <=3.3, >4 <5

console.log(rangeToInterval(example4));

// 1, [2.2,3.3], (4,5)