Skip to content

Commit 9045d5f

Browse files
committed
update docs
1 parent baf1457 commit 9045d5f

File tree

4 files changed

+132
-180
lines changed

4 files changed

+132
-180
lines changed

.travis.yml

-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,3 @@ node_js:
88
- '10'
99
- '9'
1010
- '8'
11-
- '7'
12-
- '6'
13-
- '5'
14-
- '4'

.verb.md

+32-30
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,14 @@ If you're interested in learning more about [character classes](http://www.regul
4646

4747
### Heavily tested
4848

49-
As of {%= date() %}, this library runs [2,783,483 test assertions](./test/test.js) against generated regex-ranges to provide brute-force verification that results are indeed correct.
49+
As of {%= date() %}, this library runs [>1m test assertions](./test/test.js) against generated regex-ranges to provide brute-force verification that results are correct.
5050

51-
Tests run in ~580ms on my MacBook Pro, 2.5 GHz Intel Core i7.
51+
Tests run in ~280ms on my MacBook Pro, 2.5 GHz Intel Core i7.
5252

53-
### Highly optimized
53+
### Optimized
54+
55+
Generated regular expressions are optimized:
5456

55-
Generated regular expressions are highly optimized:
5657
- duplicate sequences and character classes are reduced using quantifiers
5758
- smart enough to use `?` conditionals when number(s) or range(s) can be positive or negative
5859
- uses fragment caching to avoid processing the same exact string more than once
@@ -75,11 +76,11 @@ The main export is a function that takes two integers: the `min` value and `max`
7576
const source = toRegexRange('15', '95');
7677
//=> 1[5-9]|[2-8][0-9]|9[0-5]
7778

78-
const re = new RegExp('^' + source + '$');
79-
console.log(re.test('14')); //=> false
80-
console.log(re.test('50')); //=> true
81-
console.log(re.test('94')); //=> true
82-
console.log(re.test('96')); //=> false
79+
const regex = new RegExp(`^${source}$`);
80+
console.log(regex.test('14')); //=> false
81+
console.log(regex.test('50')); //=> true
82+
console.log(regex.test('94')); //=> true
83+
console.log(regex.test('96')); //=> false
8384
```
8485

8586
## Options
@@ -122,35 +123,32 @@ console.log(toRegexRange('0', '999999', { shorthand: true }));
122123

123124
**Default**: `true`
124125

125-
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*`.
126+
This option relaxes matching for leading zeros when when ranges are zero-padded.
126127

127128
```js
128-
console.log(toRegexRange('-001', '100'));
129-
//=> -0*1|0{2}[0-9]|0[1-9][0-9]|100
130-
131-
console.log(toRegexRange('-001', '100', {relaxZeros: false}));
132-
//=> -0{2}1|0{2}[0-9]|0[1-9][0-9]|100
129+
const source = toRegexRange('-0010', '0010');
130+
const regex = new RegExp(`^${source}$`);
131+
console.log(regex.test('-10')); //=> true
132+
console.log(regex.test('-010')); //=> true
133+
console.log(regex.test('-0010')); //=> true
134+
console.log(regex.test('10')); //=> true
135+
console.log(regex.test('010')); //=> true
136+
console.log(regex.test('0010')); //=> true
133137
```
134138

135-
<details>
136-
<summary><strong>Why are zeros relaxed for negative zero-padded ranges by default?</strong></summary>
137-
138-
Consider the following.
139+
When `relaxZeros` is false, matching is strict:
139140

140141
```js
141-
const regex = toRegexRange('-001', '100');
142+
const source = toRegexRange('-0010', '0010', { relaxZeros: false });
143+
const regex = new RegExp(`^${source}$`);
144+
console.log(regex.test('-10')); //=> false
145+
console.log(regex.test('-010')); //=> false
146+
console.log(regex.test('-0010')); //=> true
147+
console.log(regex.test('10')); //=> false
148+
console.log(regex.test('010')); //=> false
149+
console.log(regex.test('0010')); //=> true
142150
```
143151

144-
_Note that `-001` and `100` are both three digits long_.
145-
146-
In most zero-padding implementations, only a single leading zero is enough to indicate that zero-padding should be applied. Thus, the leading zeros would be "corrected" on the negative range in the example to `-01`, instead of `-001`, to make total length of each string no greater than the length of the largest number in the range (in other words, `-001` is 4 digits, but `100` is only three digits).
147-
148-
If zeros were not relaxed by default, you might expect the resulting regex of the above pattern to match `-001` - given that it's defined that way in the arguments - _but it wouldn't_. It would, however, match `-01`. This gets even more ambiguous with large ranges, like `-01` to `1000000`.
149-
150-
Thus, we relax zeros by default to provide a more predictable experience for users.
151-
152-
</details>
153-
154152
## Examples
155153

156154
{%= examples() %}
@@ -179,6 +177,10 @@ This library does not support steps (increments). A pr to add support would be w
179177

180178
## History
181179

180+
### v5.0.0 - 2019-04-07
181+
182+
Optimizations. Updated code to use newer ES features.
183+
182184
### v2.0.0 - 2017-04-21
183185

184186
**New features**

0 commit comments

Comments
 (0)