
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
parse-numeric-range
Advanced tools
Takes a string, such as "1,2,3-10,5-8" and turns it into an array of numbers
The parse-numeric-range npm package is a utility for parsing numeric ranges from strings. It can handle individual numbers, ranges, and combinations of both, making it useful for scenarios where you need to interpret and work with numeric ranges from user input or configuration files.
Parse Single Numbers
This feature allows you to parse a single number from a string and returns it as an array containing that number.
const parse = require('parse-numeric-range');
const result = parse('5');
console.log(result); // [5]
Parse Ranges
This feature allows you to parse a range of numbers from a string and returns an array containing all the numbers in that range.
const parse = require('parse-numeric-range');
const result = parse('1-5');
console.log(result); // [1, 2, 3, 4, 5]
Parse Combinations of Numbers and Ranges
This feature allows you to parse a combination of individual numbers and ranges from a string, returning an array containing all the specified numbers.
const parse = require('parse-numeric-range');
const result = parse('1-3,5,7-9');
console.log(result); // [1, 2, 3, 5, 7, 8, 9]
The range-parser package is used to parse HTTP Range headers, which specify a range of bytes to be sent from a server. While it is more specialized for HTTP use cases, it can also handle numeric ranges but is not as general-purpose as parse-numeric-range.
Parses expressions like 1-10,20-30. Returns an energetic (as opposed to lazy) array.
Comprehensive supported expression examples:
Expression | result |
---|---|
[] | |
1 | [1] |
1,2 | [1,2] |
-10 | [-10] |
-3,-3 | [-3, -3] |
-1-2,-2 | [-1,0,1,2,-2] |
-1--2 | [-1,-2] |
-1..2,-2 | [-1,0,1,2,-2] |
-1...3,-2 | [-1,0,1,2,-2] |
1⋯3 | [1,2] |
1…3 | [1,2] |
1‥3 | [1,2,3] |
What's this useful for? Well, letting users input these sorts of things and then making them programmatically useful.
First, npm install parse-numeric-range
.
const rangeParser = require("parse-numeric-range");
const numbers = rangeParser("4,6,8-10,12,14..16,18,20...23");
console.log(
`The first ${numbers.length} composite numbers are: ${numbers.join(", ")}`,
);
import rangeParser from "parse-numeric-range";
const numbers = rangeParser("4,6,8-10,12,14..16,18,20...23");
console.log(
`The first ${numbers.length} composite numbers are: ${numbers.join(", ")}`,
);
FAQs
Takes a string, such as "1,2,3-10,5-8" and turns it into an array of numbers
The npm package parse-numeric-range receives a total of 865,070 weekly downloads. As such, parse-numeric-range popularity was classified as popular.
We found that parse-numeric-range demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.