sxoinas12 82e781b8aa Graph & Tree | преди 5 години | |
---|---|---|
.. | ||
LICENSE | преди 5 години | |
README.md | преди 5 години | |
index.js | преди 5 години | |
package.json | преди 5 години |
Pass two numbers, get a regex-compatible source string for matching ranges. Validated against more than 2.78 million test assertions.
Install with npm:
$ npm install --save to-regex-range
Install with yarn:
$ yarn add to-regex-range
Add this library to your javascript application with the following line of code
var toRegexRange = require('to-regex-range');
The main export is a function that takes two integers: the min
value and max
value (formatted as strings or numbers).
var source = toRegexRange('15', '95');
//=> 1[5-9]|[2-8][0-9]|9[0-5]
var re = new RegExp('^' + source + '$');
console.log(re.test('14')); //=> false
console.log(re.test('50')); //=> true
console.log(re.test('94')); //=> true
console.log(re.test('96')); //=> false
Type: boolean
Deafault: undefined
Wrap the returned value in parentheses when there is more than one regex condition. Useful when you're dynamically generating ranges.
console.log(toRegexRange('-10', '10'));
//=> -[1-9]|-?10|[0-9]
console.log(toRegexRange('-10', '10', {capture: true}));
//=> (-[1-9]|-?10|[0-9])
Type: boolean
Deafault: undefined
Use the regex shorthand for [0-9]
:
console.log(toRegexRange('0', '999999'));
//=> [0-9]|[1-9][0-9]{1,5}
console.log(toRegexRange('0', '999999', {shorthand: true}));
//=> \d|[1-9]\d{1,5}
Type: boolean
Default: true
This option only applies to negative zero-padded ranges. By default, when a negative zero-padded range is defined, the number of leading zeros is relaxed using -0*
.
console.log(toRegexRange('-001', '100'));
//=> -0*1|0{2}[0-9]|0[1-9][0-9]|100
console.log(toRegexRange('-001', '100', {relaxZeros: false}));
//=> -0{2}1|0{2}[0-9]|0[1-9][0-9]|100
| Range | Result | Compile time |
| --- | --- | --- |
| toRegexRange('5, 5')
| 5
| 33μs |
| toRegexRange('5, 6')
| 5\|6
| 53μs |
| toRegexRange('29, 51')
| 29\|[34][0-9]\|5[01]
| 699μs |
| toRegexRange('31, 877')
| 3[1-9]\|[4-9][0-9]\|[1-7][0-9]{2}\|8[0-6][0-9]\|87[0-7]
| 711μs |
| toRegexRange('111, 555')
| 11[1-9]\|1[2-9][0-9]\|[2-4][0-9]{2}\|5[0-4][0-9]\|55[0-5]
| 62μs |
| toRegexRange('-10, 10')
| -[1-9]\|-?10\|[0-9]
| 74μs |
| toRegexRange('-100, -10')
| -1[0-9]\|-[2-9][0-9]\|-100
| 49μs |
| toRegexRange('-100, 100')
| -[1-9]\|-?[1-9][0-9]\|-?100\|[0-9]
| 45μs |
| toRegexRange('001, 100')
| 0{2}[1-9]\|0[1-9][0-9]\|100
| 158μs |
| toRegexRange('0010, 1000')
| 0{2}1[0-9]\|0{2}[2-9][0-9]\|0[1-9][0-9]{2}\|1000
| 61μs |
| toRegexRange('1, 2')
| 1\|2
| 10μs |
| toRegexRange('1, 5')
| [1-5]
| 24μs |
| toRegexRange('1, 10')
| [1-9]\|10
| 23μs |
| toRegexRange('1, 100')
| [1-9]\|[1-9][0-9]\|100
| 30μs |
| toRegexRange('1, 1000')
| [1-9]\|[1-9][0-9]{1,2}\|1000
| 52μs |
| toRegexRange('1, 10000')
| [1-9]\|[1-9][0-9]{1,3}\|10000
| 47μs |
| toRegexRange('1, 100000')
| [1-9]\|[1-9][0-9]{1,4}\|100000
| 44μs |
| toRegexRange('1, 1000000')
| [1-9]\|[1-9][0-9]{1,5}\|1000000
| 49μs |
| toRegexRange('1, 10000000')
| [1-9]\|[1-9][0-9]{1,6}\|10000000
| 63μs |
Order of arguments
When the min
is larger than the max
, values will be flipped to create a valid range:
toRegexRange('51', '29');
Is effectively flipped to:
toRegexRange('29', '51');
//=> 29|[3-4][0-9]|5[0-1]
Steps / increments
This library does not support steps (increments). A pr to add support would be welcome.
New features
Adds support for zero-padding!
Optimizations
Repeating ranges are now grouped using quantifiers. rocessing time is roughly the same, but the generated regex is much smaller, which should result in faster matching.
Inspired by the python library range-regex.
step
to… more | homepagePull requests and stars are always welcome. For bugs and feature requests, please create an issue.
(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)
To generate the readme, run the following command:
$ npm install -g verbose/verb#dev verb-generate-readme && verb
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
$ npm install && npm test
Jon Schlinkert
Copyright © 2017, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.6.0, on April 27, 2017.