Skip to content

Commit f7ce5ac

Browse files
committed
wrap result
1 parent cd04d2c commit f7ce5ac

File tree

5 files changed

+97
-81
lines changed

5 files changed

+97
-81
lines changed

.verb.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ This libary generates the `source` string to be passed to `new RegExp()` for mat
88
**Example**
99

1010
```js
11-
var toRegexRange = require('{%= name %}');
12-
var regex = new RegExp(toRegexRange('15', '95'));
11+
const toRegexRange = require('{%= name %}');
12+
const regex = new RegExp(toRegexRange('15', '95'));
1313
```
1414

1515
A string is returned so that you can do whatever you need with it before passing it to `new RegExp()` (like adding `^` or `$` boundaries, defining flags, or combining it another string).
@@ -66,16 +66,16 @@ Generated regular expressions are highly optimized:
6666
Add this library to your javascript application with the following line of code
6767

6868
```js
69-
var toRegexRange = require('{%= name %}');
69+
const toRegexRange = require('{%= name %}');
7070
```
7171

7272
The main export is a function that takes two integers: the `min` value and `max` value (formatted as strings or numbers).
7373

7474
```js
75-
var source = toRegexRange('15', '95');
75+
const source = toRegexRange('15', '95');
7676
//=> 1[5-9]|[2-8][0-9]|9[0-5]
7777

78-
var re = new RegExp('^' + source + '$');
78+
const re = new RegExp('^' + source + '$');
7979
console.log(re.test('14')); //=> false
8080
console.log(re.test('50')); //=> true
8181
console.log(re.test('94')); //=> true
@@ -96,7 +96,7 @@ Wrap the returned value in parentheses when there is more than one regex conditi
9696
console.log(toRegexRange('-10', '10'));
9797
//=> -[1-9]|-?10|[0-9]
9898

99-
console.log(toRegexRange('-10', '10', {capture: true}));
99+
console.log(toRegexRange('-10', '10', { capture: true }));
100100
//=> (-[1-9]|-?10|[0-9])
101101
```
102102

@@ -112,7 +112,7 @@ Use the regex shorthand for `[0-9]`:
112112
console.log(toRegexRange('0', '999999'));
113113
//=> [0-9]|[1-9][0-9]{1,5}
114114

115-
console.log(toRegexRange('0', '999999', {shorthand: true}));
115+
console.log(toRegexRange('0', '999999', { shorthand: true }));
116116
//=> \d|[1-9]\d{1,5}
117117
```
118118

@@ -138,7 +138,7 @@ console.log(toRegexRange('-001', '100', {relaxZeros: false}));
138138
Consider the following.
139139

140140
```js
141-
var regex = toRegexRange('-001', '100');
141+
const regex = toRegexRange('-001', '100');
142142
```
143143

144144
_Note that `-001` and `100` are both three digits long_.
@@ -193,4 +193,4 @@ Repeating ranges are now grouped using quantifiers. rocessing time is roughly th
193193

194194
## Attribution
195195

196-
Inspired by the python library [range-regex](https://github.com/dimka665/range-regex).
196+
Inspired by the python library [range-regex](https://github.com/dimka665/range-regex).

examples.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const time = new Time();
1212
function toRange(min, max) {
1313
let key = 'to-range' + min + max;
1414
time.start(key);
15-
let str = toRegexRange(min, max);
15+
let str = toRegexRange(min, max, { wrap: false });
1616
return [
1717
'',
1818
'`toRegexRange(\'' + min + ', ' + max + '\')`',
@@ -59,7 +59,7 @@ let examples = [
5959
rows.push(toRange.apply(null, args));
6060
});
6161

62-
let text = table(rows, {hsep: ' | '});
62+
let text = table(rows, { hsep: ' | ' });
6363
console.log(text);
6464

6565
/**

index.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*!
22
* to-regex-range <https://github.com/micromatch/to-regex-range>
33
*
4-
* Copyright (c) 2015-2018, present, Jon Schlinkert.
4+
* Copyright (c) 2015-present, Jon Schlinkert.
55
* Released under the MIT License.
66
*/
77

@@ -39,7 +39,10 @@ function toRegexRange(min, max, options) {
3939
if (options.capture) {
4040
return '(' + result + ')';
4141
}
42-
return result;
42+
if (options.wrap === false) {
43+
return result;
44+
}
45+
return '(?:' + result + ')';
4346
}
4447

4548
let isPadded = padding(min) || padding(max);
@@ -67,8 +70,10 @@ function toRegexRange(min, max, options) {
6770
tok.positives = positives;
6871
tok.result = siftPatterns(negatives, positives, options);
6972

70-
if (options.capture && (positives.length + negatives.length) > 1) {
73+
if (options.capture) {
7174
tok.result = '(' + tok.result + ')';
75+
} else if (options.wrap !== false && positives.length + negatives.length > 1) {
76+
tok.result = '(?:' + tok.result + ')';
7277
}
7378

7479
toRegexRange.cache[key] = tok;
@@ -219,7 +224,7 @@ function filterPatterns(arr, comparison, prefix, intersection, options) {
219224
}
220225

221226
/**
222-
* Zip strings (`for in` can be used on string characters)
227+
* Zip strings
223228
*/
224229

225230
function zip(a, b) {

package.json

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
"is-number": "^7.0.0"
2828
},
2929
"devDependencies": {
30-
"fill-range": "^5.0.0",
31-
"gulp-format-md": "^1.0.0",
30+
"fill-range": "^6.0.0",
31+
"gulp-format-md": "^2.0.0",
3232
"mocha": "^5.2.0",
3333
"text-table": "^0.2.0",
3434
"time-diff": "^0.3.1"
@@ -56,17 +56,8 @@
5656
"year"
5757
],
5858
"verb": {
59-
"related": {
60-
"list": [
61-
"expand-range",
62-
"fill-range",
63-
"micromatch",
64-
"repeat-element",
65-
"repeat-string"
66-
]
67-
},
68-
"toc": false,
6959
"layout": "default",
60+
"toc": false,
7061
"tasks": [
7162
"readme"
7263
],
@@ -76,15 +67,17 @@
7667
"lint": {
7768
"reflinks": true
7869
},
79-
"helpers": [
80-
"./examples.js"
81-
],
82-
"reflinks": [
83-
"micromatch",
84-
"0-5",
85-
"0-9",
86-
"1-5",
87-
"1-9"
88-
]
70+
"helpers": {
71+
"examples": "./examples.js"
72+
},
73+
"related": {
74+
"list": [
75+
"expand-range",
76+
"fill-range",
77+
"micromatch",
78+
"repeat-element",
79+
"repeat-string"
80+
]
81+
}
8982
}
9083
}

test/test.js

Lines changed: 62 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ function inRange(min, max, num) {
8282

8383
describe('to-regex-range', function() {
8484
after(function() {
85-
let num = (+(+(count).toFixed(2))).toLocaleString();
85+
let num = (+(+count.toFixed(2))).toLocaleString();
8686
console.log();
8787
console.log(' ', num, 'values tested');
8888
});
@@ -95,11 +95,29 @@ describe('to-regex-range', function() {
9595
it('should throw an error when the second arg is invalid:', function() {
9696
assert.throws(() => toRange(1, {}), /expected/);
9797
});
98+
99+
it('should match the given numbers', function() {
100+
let oneFifty = new RegExp('^' + toRange(1, 150) + '$');
101+
assert(oneFifty.test('125'));
102+
assert(!oneFifty.test('0'));
103+
assert(oneFifty.test('1'));
104+
assert(oneFifty.test('126'));
105+
assert(oneFifty.test('150'));
106+
assert(!oneFifty.test('151'));
107+
108+
let oneTwentyFive = new RegExp('^' + toRange(1, 125) + '$');
109+
assert(oneTwentyFive.test('125'));
110+
assert(!oneTwentyFive.test('0'));
111+
assert(oneTwentyFive.test('1'));
112+
assert(!oneTwentyFive.test('126'));
113+
assert(!oneTwentyFive.test('150'));
114+
assert(!oneTwentyFive.test('151'));
115+
});
98116
});
99117

100118
describe('minimum / maximum', function() {
101119
it('should reverse `min/max` when the min is larger than the max:', function() {
102-
assert.strictEqual(toRange(55, 10), '1[0-9]|[2-4][0-9]|5[0-5]');
120+
assert.strictEqual(toRange(55, 10), '(?:1[0-9]|[2-4][0-9]|5[0-5])');
103121
});
104122
});
105123

@@ -118,74 +136,74 @@ describe('to-regex-range', function() {
118136

119137
it('should support strings:', function() {
120138
assert.strictEqual(toRange('1', '5'), '[1-5]');
121-
assert.strictEqual(toRange('10', '50'), '1[0-9]|[2-4][0-9]|50');
139+
assert.strictEqual(toRange('10', '50'), '(?:1[0-9]|[2-4][0-9]|50)');
122140
});
123141

124142
it('should support padded ranges:', function() {
125143
assert.strictEqual(toRange('001', '005'), '0{2}[1-5]');
126144
assert.strictEqual(toRange('01', '05'), '0[1-5]');
127-
assert.strictEqual(toRange('001', '100'), '0{2}[1-9]|0[1-9][0-9]|100');
128-
assert.strictEqual(toRange('0001', '1000'), '0{3}[1-9]|0{2}[1-9][0-9]|0[1-9][0-9]{2}|1000');
145+
assert.strictEqual(toRange('001', '100'), '(?:0{2}[1-9]|0[1-9][0-9]|100)');
146+
assert.strictEqual(toRange('0001', '1000'), '(?:0{3}[1-9]|0{2}[1-9][0-9]|0[1-9][0-9]{2}|1000)');
129147
});
130148

131149
it('should work when padding is imbalanced:', function() {
132-
assert.strictEqual(toRange('001', '105'), '0{2}[1-9]|0[1-9][0-9]|10[0-5]');
133-
assert.strictEqual(toRange('01', '105'), '0{2}[1-9]|0[1-9][0-9]|10[0-5]');
134-
assert.strictEqual(toRange('010', '105'), '01[0-9]|0[2-9][0-9]|10[0-5]');
135-
assert.strictEqual(toRange('010', '1005'), '0{2}1[0-9]|0{2}[2-9][0-9]|0[1-9][0-9]{2}|100[0-5]');
150+
assert.strictEqual(toRange('001', '105'), '(?:0{2}[1-9]|0[1-9][0-9]|10[0-5])');
151+
assert.strictEqual(toRange('01', '105'), '(?:0{2}[1-9]|0[1-9][0-9]|10[0-5])');
152+
assert.strictEqual(toRange('010', '105'), '(?:01[0-9]|0[2-9][0-9]|10[0-5])');
153+
assert.strictEqual(toRange('010', '1005'), '(?:0{2}1[0-9]|0{2}[2-9][0-9]|0[1-9][0-9]{2}|100[0-5])');
136154
assert.strictEqual(toRange('0001', '1000'), toRange('001', '1000'));
137155
assert.strictEqual(toRange('0001', '1000'), toRange('01', '1000'));
138156
});
139157

140158
it('should generate regex strings for negative patterns', function() {
141-
assert.strictEqual(toRange(-1, 0), '-1|0');
142-
assert.strictEqual(toRange(-1, 1), '-1|[01]');
159+
assert.strictEqual(toRange(-1, 0), '(?:-1|0)');
160+
assert.strictEqual(toRange(-1, 1), '(?:-1|[01])');
143161
assert.strictEqual(toRange(-4, -2), '-[2-4]');
144-
assert.strictEqual(toRange(-3, 1), '-[1-3]|[01]');
145-
assert.strictEqual(toRange(-2, 0), '-[12]|0');
146-
assert.strictEqual(toRange(-1, 3), '-1|[0-3]');
162+
assert.strictEqual(toRange(-3, 1), '(?:-[1-3]|[01])');
163+
assert.strictEqual(toRange(-2, 0), '(?:-[12]|0)');
164+
assert.strictEqual(toRange(-1, 3), '(?:-1|[0-3])');
147165
matchRange(-1, -1, '-1', [-1], [-2, 0, 1]);
148-
matchRange(-1, -10, '-[1-9]|-10', [-1, -5, -10], [-11, 0]);
149-
matchRange(-1, 3, '-1|[0-3]', [-1, 0, 1, 2, 3], [-2, 4]);
166+
matchRange(-1, -10, '(?:-[1-9]|-10)', [-1, -5, -10], [-11, 0]);
167+
matchRange(-1, 3, '(?:-1|[0-3])', [-1, 0, 1, 2, 3], [-2, 4]);
150168
});
151169

152170
it('should wrap patterns when options.capture is true', function() {
153-
assert.strictEqual(toRange(-1, 0, {capture: true}), '(-1|0)');
154-
assert.strictEqual(toRange(-1, 1, {capture: true}), '(-1|[01])');
155-
assert.strictEqual(toRange(-4, -2, {capture: true}), '-[2-4]');
156-
assert.strictEqual(toRange(-3, 1, {capture: true}), '(-[1-3]|[01])');
157-
assert.strictEqual(toRange(-2, 0, {capture: true}), '(-[12]|0)');
158-
assert.strictEqual(toRange(-1, 3, {capture: true}), '(-1|[0-3])');
171+
assert.strictEqual(toRange(-1, 0, { capture: true }), '(-1|0)');
172+
assert.strictEqual(toRange(-1, 1, { capture: true }), '(-1|[01])');
173+
assert.strictEqual(toRange(-4, -2, { capture: true }), '(-[2-4])');
174+
assert.strictEqual(toRange(-3, 1, { capture: true }), '(-[1-3]|[01])');
175+
assert.strictEqual(toRange(-2, 0, { capture: true }), '(-[12]|0)');
176+
assert.strictEqual(toRange(-1, 3, { capture: true }), '(-1|[0-3])');
159177
});
160178

161179
it('should generate regex strings for positive patterns', function() {
162180
assert.strictEqual(toRange(1, 1), '1');
163-
assert.strictEqual(toRange(0, 1), '0|1');
181+
assert.strictEqual(toRange(0, 1), '(?:0|1)');
164182
assert.strictEqual(toRange(0, 2), '[0-2]');
165-
assert.strictEqual(toRange(65666, 65667), '65666|65667');
166-
assert.strictEqual(toRange(12, 3456), '1[2-9]|[2-9][0-9]|[1-9][0-9]{2}|[12][0-9]{3}|3[0-3][0-9]{2}|34[0-4][0-9]|345[0-6]');
167-
assert.strictEqual(toRange(1, 3456), '[1-9]|[1-9][0-9]{1,2}|[12][0-9]{3}|3[0-3][0-9]{2}|34[0-4][0-9]|345[0-6]');
168-
assert.strictEqual(toRange(1, 10), '[1-9]|10');
169-
assert.strictEqual(toRange(1, 19), '[1-9]|1[0-9]');
170-
assert.strictEqual(toRange(1, 99), '[1-9]|[1-9][0-9]');
171-
assert.strictEqual(toRange(1, 100), '[1-9]|[1-9][0-9]|100');
172-
assert.strictEqual(toRange(1, 1000), '[1-9]|[1-9][0-9]{1,2}|1000');
173-
assert.strictEqual(toRange(1, 10000), '[1-9]|[1-9][0-9]{1,3}|10000');
174-
assert.strictEqual(toRange(1, 100000), '[1-9]|[1-9][0-9]{1,4}|100000');
175-
assert.strictEqual(toRange(1, 9999999), '[1-9]|[1-9][0-9]{1,6}');
176-
assert.strictEqual(toRange(99, 100000), '99|[1-9][0-9]{2,4}|100000');
177-
178-
matchRange(99, 100000, '99|[1-9][0-9]{2,4}|100000', [99, 999, 989, 100, 9999, 9899, 10009, 10999, 100000], [0, 9, 100001, 100009]);
183+
assert.strictEqual(toRange(65666, 65667), '(?:65666|65667)');
184+
assert.strictEqual(toRange(12, 3456), '(?:1[2-9]|[2-9][0-9]|[1-9][0-9]{2}|[12][0-9]{3}|3[0-3][0-9]{2}|34[0-4][0-9]|345[0-6])');
185+
assert.strictEqual(toRange(1, 3456), '(?:[1-9]|[1-9][0-9]{1,2}|[12][0-9]{3}|3[0-3][0-9]{2}|34[0-4][0-9]|345[0-6])');
186+
assert.strictEqual(toRange(1, 10), '(?:[1-9]|10)');
187+
assert.strictEqual(toRange(1, 19), '(?:[1-9]|1[0-9])');
188+
assert.strictEqual(toRange(1, 99), '(?:[1-9]|[1-9][0-9])');
189+
assert.strictEqual(toRange(1, 100), '(?:[1-9]|[1-9][0-9]|100)');
190+
assert.strictEqual(toRange(1, 1000), '(?:[1-9]|[1-9][0-9]{1,2}|1000)');
191+
assert.strictEqual(toRange(1, 10000), '(?:[1-9]|[1-9][0-9]{1,3}|10000)');
192+
assert.strictEqual(toRange(1, 100000), '(?:[1-9]|[1-9][0-9]{1,4}|100000)');
193+
assert.strictEqual(toRange(1, 9999999), '(?:[1-9]|[1-9][0-9]{1,6})');
194+
assert.strictEqual(toRange(99, 100000), '(?:99|[1-9][0-9]{2,4}|100000)');
195+
196+
matchRange(99, 100000, '(?:99|[1-9][0-9]{2,4}|100000)', [99, 999, 989, 100, 9999, 9899, 10009, 10999, 100000], [0, 9, 100001, 100009]);
179197
});
180198

181199
it('should optimize regexes', function() {
182-
assert.strictEqual(toRange(-9, 9), '-[1-9]|[0-9]');
183-
assert.strictEqual(toRange(-19, 19), '-[1-9]|-?1[0-9]|[0-9]');
184-
assert.strictEqual(toRange(-29, 29), '-[1-9]|-?[12][0-9]|[0-9]');
185-
assert.strictEqual(toRange(-99, 99), '-[1-9]|-?[1-9][0-9]|[0-9]');
186-
assert.strictEqual(toRange(-999, 999), '-[1-9]|-?[1-9][0-9]{1,2}|[0-9]');
187-
assert.strictEqual(toRange(-9999, 9999), '-[1-9]|-?[1-9][0-9]{1,3}|[0-9]');
188-
assert.strictEqual(toRange(-99999, 99999), '-[1-9]|-?[1-9][0-9]{1,4}|[0-9]');
200+
assert.strictEqual(toRange(-9, 9), '(?:-[1-9]|[0-9])');
201+
assert.strictEqual(toRange(-19, 19), '(?:-[1-9]|-?1[0-9]|[0-9])');
202+
assert.strictEqual(toRange(-29, 29), '(?:-[1-9]|-?[12][0-9]|[0-9])');
203+
assert.strictEqual(toRange(-99, 99), '(?:-[1-9]|-?[1-9][0-9]|[0-9])');
204+
assert.strictEqual(toRange(-999, 999), '(?:-[1-9]|-?[1-9][0-9]{1,2}|[0-9])');
205+
assert.strictEqual(toRange(-9999, 9999), '(?:-[1-9]|-?[1-9][0-9]{1,3}|[0-9])');
206+
assert.strictEqual(toRange(-99999, 99999), '(?:-[1-9]|-?[1-9][0-9]{1,4}|[0-9])');
189207
});
190208
});
191209

0 commit comments

Comments
 (0)