to-regex-range
Advanced tools
Comparing version 0.1.1 to 0.1.2
81
index.js
@@ -12,3 +12,3 @@ /*! | ||
var isNumber = require('is-number'); | ||
var cache = {}; | ||
var cache = {range: {}, rangeToPattern: {}}; | ||
@@ -28,5 +28,5 @@ function toRegexRange(min, max) { | ||
var key = min + '-' + max; | ||
if (cache.hasOwnProperty(key)) { | ||
return cache[key]; | ||
var key = min + ':' + max; | ||
if (cache.range.hasOwnProperty(key)) { | ||
return cache.range[key]; | ||
} | ||
@@ -65,3 +65,4 @@ | ||
var res = siftPatterns(negatives, positives); | ||
return (cache[key] = res); | ||
cache.range[key] = res; | ||
return res; | ||
} | ||
@@ -105,2 +106,8 @@ | ||
function rangeToPattern(start, stop) { | ||
var key = start + ':' + stop; | ||
if (cache.rangeToPattern.hasOwnProperty(key)) { | ||
return cache.rangeToPattern[key]; | ||
} | ||
var zipped = zip(String(start), String(stop)); | ||
@@ -121,3 +128,3 @@ var len = zipped.length, i = -1; | ||
} else if (startDigit !== '0' || stopDigit !== '9') { | ||
pattern += rangify(startDigit, stopDigit); | ||
pattern += toCharacterClass(startDigit, stopDigit); | ||
@@ -134,19 +141,17 @@ } else { | ||
if (digits > 1) { | ||
pattern += toBraces(digits); | ||
pattern += limit(digits); | ||
} | ||
cache.rangeToPattern[key] = pattern; | ||
return pattern; | ||
} | ||
/** | ||
* Zip strings (`for in` can be used on string characters) | ||
*/ | ||
function zip(a, b) { | ||
var arrA = a.split(''); | ||
var arrB = b.split(''); | ||
var len = arrA.length; | ||
var i = -1; | ||
var res = []; | ||
while (++i < len) { | ||
res.push([arrA[i], arrB[i]]); | ||
} | ||
return res; | ||
var arr = []; | ||
for (var ch in a) arr.push([a[ch], b[ch]]); | ||
return arr; | ||
} | ||
@@ -156,10 +161,11 @@ | ||
var ranges = splitToRanges(min, max); | ||
var len = ranges.length, i = -1; | ||
var len = ranges.length; | ||
var idx = -1; | ||
var start = min; | ||
var subpatterns = []; | ||
var subpatterns = new Array(len); | ||
while (++i < len) { | ||
var range = ranges[i]; | ||
subpatterns.push(rangeToPattern(start, range)); | ||
while (++idx < len) { | ||
var range = ranges[idx]; | ||
subpatterns[idx] = rangeToPattern(start, range); | ||
start = range + 1; | ||
@@ -172,3 +178,4 @@ } | ||
var len = arr.length, i = -1; | ||
var res = [], intersected = []; | ||
var intersected = []; | ||
var res = []; | ||
@@ -187,13 +194,2 @@ while (++i < len) { | ||
function compare(a, b) { | ||
return a - b; | ||
} | ||
function add(arr, ele) { | ||
if (arr.indexOf(ele) === -1) { | ||
arr.push(ele); | ||
} | ||
return arr; | ||
} | ||
function countNines(num, len) { | ||
@@ -207,14 +203,21 @@ return String(num).slice(0, -len) + repeat('9', len); | ||
function toBraces(str) { | ||
function limit(str) { | ||
return '{' + str + '}'; | ||
} | ||
function toBrackets(str) { | ||
return '[' + str + ']'; | ||
function toCharacterClass(a, b) { | ||
return '[' + a + '-' + b + ']'; | ||
} | ||
function rangify(a, b) { | ||
return toBrackets(a + '-' + b); | ||
function compare(a, b) { | ||
return a - b; | ||
} | ||
function add(arr, ele) { | ||
if (arr.indexOf(ele) === -1) { | ||
arr.push(ele); | ||
} | ||
return arr; | ||
} | ||
/** | ||
@@ -221,0 +224,0 @@ * Expose `toRegexRange` |
{ | ||
"name": "to-regex-range", | ||
"description": "Returns a regex-compatible range from two numbers, min and max. Useful for creating regular expressions to validate numbers, ranges, years, etc. Returns a string, allowing the returned value to be used in regular expressions generated by other libraries.", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"homepage": "https://github.com/jonschlinkert/to-regex-range", | ||
@@ -23,8 +23,9 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
"dependencies": { | ||
"is-number": "^2.0.2", | ||
"repeat-string": "^1.5.2" | ||
"is-number": "^2.1.0", | ||
"repeat-string": "^1.5.4" | ||
}, | ||
"devDependencies": { | ||
"mocha": "*", | ||
"should": "*" | ||
"gulp-format-md": "^0.1.9", | ||
"mocha": "^2.4.5", | ||
"should": "^8.3.1" | ||
}, | ||
@@ -51,4 +52,4 @@ "keywords": [ | ||
"list": [ | ||
"expand-range", | ||
"fill-range", | ||
"expand-range", | ||
"micromatch", | ||
@@ -58,4 +59,18 @@ "repeat-element", | ||
] | ||
} | ||
}, | ||
"toc": false, | ||
"layout": "default", | ||
"tasks": [ | ||
"readme" | ||
], | ||
"plugins": [ | ||
"gulp-format-md" | ||
], | ||
"lint": { | ||
"reflinks": true | ||
}, | ||
"reflinks": [ | ||
"verb" | ||
] | ||
} | ||
} |
@@ -1,17 +0,15 @@ | ||
# to-regex-range [![NPM version](https://badge.fury.io/js/to-regex-range.svg)](http://badge.fury.io/js/to-regex-range) | ||
# to-regex-range [![NPM version](https://img.shields.io/npm/v/to-regex-range.svg?style=flat)](https://www.npmjs.com/package/to-regex-range) [![NPM downloads](https://img.shields.io/npm/dm/to-regex-range.svg?style=flat)](https://npmjs.org/package/to-regex-range) [![Build Status](https://img.shields.io/travis/jonschlinkert/to-regex-range.svg?style=flat)](https://travis-ci.org/jonschlinkert/to-regex-range) | ||
> Returns a regex-compatible range from two numbers, min and max. Useful for creating regular expressions to validate numbers, ranges, years, etc. Returns a string, allowing the returned value to be used in regular expressions generated by other libraries. | ||
Returns a regex-compatible range from two numbers, min and max. Useful for creating regular expressions to validate numbers, ranges, years, etc. Returns a string, allowing the returned value to be used in regular expressions generated by other libraries. | ||
Inspired by the python lib [range-regex](https://github.com/dimka665/range-regex), it has never been easier to validate numbers and number ranges with regex! | ||
The [unit tests generate millions of patterns](./test/test.js) to provide brute-force validation that the generated regex-ranges are correct. | ||
## Install | ||
Install with [npm](https://www.npmjs.com/) | ||
Install with [npm](https://www.npmjs.com/): | ||
```sh | ||
$ npm i to-regex-range --save | ||
$ npm install to-regex-range --save | ||
``` | ||
## Install | ||
Install with [bower](http://bower.io/) | ||
@@ -23,2 +21,6 @@ | ||
Inspired by the python lib [range-regex](https://github.com/dimka665/range-regex), it has never been easier to validate numbers and number ranges with regex! | ||
The [unit tests generate millions of patterns](./test/test.js) to provide brute-force validation that the generated regex-ranges are correct. | ||
## Usage | ||
@@ -58,8 +60,28 @@ | ||
* [expand-range](https://github.com/jonschlinkert/expand-range): Fast, bash-like range expansion. Expand a range of numbers or letters, uppercase or lowercase. See… [more](https://github.com/jonschlinkert/expand-range) | ||
* [fill-range](https://github.com/jonschlinkert/fill-range): Fill in a range of numbers or letters, optionally passing an increment or multiplier to… [more](https://github.com/jonschlinkert/fill-range) | ||
* [micromatch](https://github.com/jonschlinkert/micromatch): Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch. Just… [more](https://github.com/jonschlinkert/micromatch) | ||
* [repeat-element](https://github.com/jonschlinkert/repeat-element): Create an array by repeating the given value n times. | ||
* [repeat-string](https://github.com/jonschlinkert/repeat-string): Repeat the given string n times. Fastest implementation for repeating a string. | ||
You might also be interested in these projects: | ||
* [expand-range](https://www.npmjs.com/package/expand-range): Fast, bash-like range expansion. Expand a range of numbers or letters, uppercase or lowercase. See… [more](https://www.npmjs.com/package/expand-range) | [homepage](https://github.com/jonschlinkert/expand-range) | ||
* [fill-range](https://www.npmjs.com/package/fill-range): Fill in a range of numbers or letters, optionally passing an increment or multiplier to… [more](https://www.npmjs.com/package/fill-range) | [homepage](https://github.com/jonschlinkert/fill-range) | ||
* [micromatch](https://www.npmjs.com/package/micromatch): Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch. | [homepage](https://github.com/jonschlinkert/micromatch) | ||
* [repeat-element](https://www.npmjs.com/package/repeat-element): Create an array by repeating the given value n times. | [homepage](https://github.com/jonschlinkert/repeat-element) | ||
* [repeat-string](https://www.npmjs.com/package/repeat-string): Repeat the given string n times. Fastest implementation for repeating a string. | [homepage](https://github.com/jonschlinkert/repeat-string) | ||
## Contributing | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/to-regex-range/issues/new). | ||
## Building docs | ||
Generate readme and API documentation with [verb](https://github.com/verbose/verb): | ||
```sh | ||
$ npm install verb && npm run docs | ||
``` | ||
Or, if [verb](https://github.com/verbose/verb) is installed globally: | ||
```sh | ||
$ verb | ||
``` | ||
## Running tests | ||
@@ -70,9 +92,5 @@ | ||
```sh | ||
$ npm i -d && npm test | ||
$ npm install -d && npm test | ||
``` | ||
## Contributing | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/to-regex-range/issues/new) | ||
## Author | ||
@@ -82,12 +100,12 @@ | ||
+ [github/jonschlinkert](https://github.com/jonschlinkert) | ||
+ [twitter/jonschlinkert](http://twitter.com/jonschlinkert) | ||
* [github/jonschlinkert](https://github.com/jonschlinkert) | ||
* [twitter/jonschlinkert](http://twitter.com/jonschlinkert) | ||
## License | ||
Copyright © 2015 Jon Schlinkert | ||
Released under the MIT license. | ||
Copyright © 2016, [Jon Schlinkert](https://github.com/jonschlinkert). | ||
Released under the [MIT license](https://github.com/jonschlinkert/to-regex-range/blob/master/LICENSE). | ||
*** | ||
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on August 07, 2015._ | ||
_This file was generated by [verb](https://github.com/verbose/verb), v0.9.0, on April 27, 2016._ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10916
171
107
3
Updatedis-number@^2.1.0
Updatedrepeat-string@^1.5.4