Built to help parse, work with, and create PostgreSQL compatible range strings.
Installations
npm install pg-numrange
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
18.12.1
NPM Version
9.1.3
Score
70.7
Supply Chain
89.2
Quality
75.2
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
reeceap124
Download Statistics
Total Downloads
467
Last Day
1
Last Week
5
Last Month
19
Last Year
147
GitHub Statistics
MIT License
14 Commits
1 Watchers
3 Branches
1 Contributors
Updated on Oct 29, 2023
Package Meta Information
Latest Version
1.0.2
Package Id
pg-numrange@1.0.2
Unpacked Size
14.38 kB
Size
3.95 kB
File Count
5
NPM Version
9.1.3
Node Version
18.12.1
Published on
Nov 29, 2023
Total Downloads
Cumulative downloads
Total Downloads
467
Last Day
0%
1
Compared to previous day
Last Week
25%
5
Compared to previous week
Last Month
375%
19
Compared to previous month
Last Year
-54.1%
147
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
1
PG NumRange
The NumRange
class is a JavaScript implementation for working with numerical ranges, which allows you to define a range with lower and upper bounds and perform various operations on these ranges. This class is designed to be flexible and user-friendly for managing and manipulating numeric intervals.
Table of Contents
Features
- Create and manage numerical ranges with ease.
- Check if a number falls within the bounds of a range.
- Calculate the union and intersection of two numerical ranges.
- Convert a range to a PostgreSQL-compatible string.
Usage
Here are some examples of how to create and work with NumRanges
1const NumRange = require('pg_numrange') 2// Create a NumRange object with inclusive bounds. 3const range1 = new NumRange(1, 10, true, true) 4 5// Check if a number is within the range. 6const number = 5 7if (range1.contains(number)) { 8 console.log(`${number} is within the range.`) 9} else { 10 console.log(`${number} is outside the range.`) 11} 12 13// Calculate the union of two ranges. 14const range2 = NumRange.parse('[5,15]') 15const unionRange = range1.union(range2) 16console.log('Union Range:', unionRange.toString()) 17 18// Calculate the intersection of two ranges. 19const intersectionRange = range1.intersection(range2) 20if (intersectionRange) { 21 console.log('Intersection Range:', intersectionRange.toString()) 22} else { 23 console.log('The ranges do not intersect.') 24}
Class Properties
-
lower: The lower bound of the numerical range. This property represents the lower limit of the range.
-
upper: The upper bound of the numerical range. This property represents the upper limit of the range.
-
lowerInclusive: A boolean value indicating whether the lower bound is inclusive when working with ranges. When set to
true
, the lower bound is inclusive, meaning values equal to the lower bound are considered within the range. -
upperInclusive: A boolean value indicating whether the upper bound is inclusive when working with ranges. When set to
true
, the upper bound is inclusive, meaning values equal to the upper bound are considered within the range.
Class Methods
-
constructor(lower, upper, lowerInclusive, upperInclusive): Create a
NumRange
object with specified lower and upper bounds, and inclusive/exclusive flags. This method initializes aNumRange
instance with the provided boundaries and options. -
parse(rangeString): Parse a range string and create a
NumRange
object. This static method is used to create aNumRange
instance based on a range string in a specific format. -
contains(number): Check if a number falls within the bounds of the range. This method returns
true
if the specified number is within the range, taking into account the inclusive or exclusive bounds. -
union(otherRange): Calculate the union of two
NumRange
objects. This method returns a newNumRange
object that encompasses both input ranges, combining them into a single range. -
intersection(otherRange): Calculate the intersection of two
NumRange
objects. This method returns a newNumRange
object representing the common range between the two input ranges. If there is no intersection, it returnsnull
. -
toString(): Convert the
NumRange
object to a PostgreSQL-compatible string. This method returns a string representing theNumRange
object in a format suitable for PostgreSQL database operations.

No vulnerabilities found.

No security vulnerabilities found.