Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

to-regex-range

Package Overview
Dependencies
Maintainers
2
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

to-regex-range - npm Package Compare versions

Comparing version 3.0.0 to 4.0.0

115

index.js
/*!
* to-regex-range <https://github.com/micromatch/to-regex-range>
*
* Copyright (c) 2015-2017, Jon Schlinkert.
* Copyright (c) 2015-2018, present, Jon Schlinkert.
* Released under the MIT License.

@@ -10,9 +10,7 @@ */

var repeat = require('repeat-string');
var isNumber = require('is-number');
var cache = {};
const isNumber = require('is-number');
function toRegexRange(min, max, options) {
if (isNumber(min) === false) {
throw new RangeError('toRegexRange: first argument is invalid.');
throw new TypeError('toRegexRange: expected the first argument to be a number');
}

@@ -25,19 +23,19 @@

if (isNumber(max) === false) {
throw new RangeError('toRegexRange: second argument is invalid.');
throw new TypeError('toRegexRange: expected the second argument to be a number.');
}
options = options || {};
var relax = String(options.relaxZeros);
var shorthand = String(options.shorthand);
var capture = String(options.capture);
var key = min + ':' + max + '=' + relax + shorthand + capture;
if (cache.hasOwnProperty(key)) {
return cache[key].result;
let relax = String(options.relaxZeros);
let shorthand = String(options.shorthand);
let capture = String(options.capture);
let key = min + ':' + max + '=' + relax + shorthand + capture;
if (toRegexRange.cache.hasOwnProperty(key)) {
return toRegexRange.cache[key].result;
}
var a = Math.min(min, max);
var b = Math.max(min, max);
let a = Math.min(min, max);
let b = Math.max(min, max);
if (Math.abs(a - b) === 1) {
var result = min + '|' + max;
let result = min + '|' + max;
if (options.capture) {

@@ -49,7 +47,7 @@ return '(' + result + ')';

var isPadded = padding(min) || padding(max);
var positives = [];
var negatives = [];
let isPadded = padding(min) || padding(max);
let positives = [];
let negatives = [];
var tok = {min: min, max: max, a: a, b: b};
let tok = {min: min, max: max, a: a, b: b};
if (isPadded) {

@@ -61,4 +59,4 @@ tok.isPadded = isPadded;

if (a < 0) {
var newMin = b < 0 ? Math.abs(b) : 1;
var newMax = Math.abs(a);
let newMin = b < 0 ? Math.abs(b) : 1;
let newMax = Math.abs(a);
negatives = splitToPatterns(newMin, newMax, tok, options);

@@ -80,11 +78,13 @@ a = tok.a = 0;

cache[key] = tok;
toRegexRange.cache[key] = tok;
return tok.result;
}
toRegexRange.cache = {};
function siftPatterns(neg, pos, options) {
var onlyNegative = filterPatterns(neg, pos, '-', false, options) || [];
var onlyPositive = filterPatterns(pos, neg, '', false, options) || [];
var intersected = filterPatterns(neg, pos, '-?', true, options) || [];
var subpatterns = onlyNegative.concat(intersected).concat(onlyPositive);
let onlyNegative = filterPatterns(neg, pos, '-', false, options) || [];
let onlyPositive = filterPatterns(pos, neg, '', false, options) || [];
let intersected = filterPatterns(neg, pos, '-?', true, options) || [];
let subpatterns = onlyNegative.concat(intersected).concat(onlyPositive);
return subpatterns.join('|');

@@ -97,5 +97,5 @@ }

var nines = 1;
var stops = [max];
var stop = +countNines(min, nines);
let nines = 1;
let stops = [max];
let stop = +countNines(min, nines);

@@ -108,3 +108,3 @@ while (min <= stop && stop <= max) {

var zeros = 1;
let zeros = 1;
stop = countZeros(max + 1, zeros) - 1;

@@ -134,12 +134,12 @@

var zipped = zip(String(start), String(stop));
var len = zipped.length, i = -1;
let zipped = zip(String(start), String(stop));
let len = zipped.length, i = -1;
var pattern = '';
var digits = 0;
let pattern = '';
let digits = 0;
while (++i < len) {
var numbers = zipped[i];
var startDigit = numbers[0];
var stopDigit = numbers[1];
let numbers = zipped[i];
let startDigit = numbers[0];
let stopDigit = numbers[1];

@@ -165,14 +165,14 @@ if (startDigit === stopDigit) {

function splitToPatterns(min, max, tok, options) {
var ranges = splitToRanges(min, max);
var len = ranges.length;
var idx = -1;
let ranges = splitToRanges(min, max);
let len = ranges.length;
let idx = -1;
var tokens = [];
var start = min;
var prev;
let tokens = [];
let start = min;
let prev;
while (++idx < len) {
var range = ranges[idx];
var obj = rangeToPattern(start, range, options);
var zeros = '';
let range = ranges[idx];
let obj = rangeToPattern(start, range, options);
let zeros = '';

@@ -203,7 +203,7 @@ if (!tok.isPadded && prev && prev.pattern === obj.pattern) {

function filterPatterns(arr, comparison, prefix, intersection, options) {
var res = [];
let res = [];
for (var i = 0; i < arr.length; i++) {
var tok = arr[i];
var ele = tok.string;
for (let i = 0; i < arr.length; i++) {
let tok = arr[i];
let ele = tok.string;

@@ -236,4 +236,4 @@ if (options.relaxZeros !== false) {

function zip(a, b) {
var arr = [];
for (var ch in a) arr.push([a[ch], b[ch]]);
let arr = [];
for (let ch in a) arr.push([a[ch], b[ch]]);
return arr;

@@ -252,3 +252,3 @@ }

function contains(arr, key, val) {
for (var i = 0; i < arr.length; i++) {
for (let i = 0; i < arr.length; i++) {
if (arr[i][key] === val) {

@@ -262,3 +262,3 @@ return true;

function countNines(min, len) {
return String(min).slice(0, -len) + repeat('9', len);
return String(min).slice(0, -len) + '9'.repeat(len);
}

@@ -271,4 +271,4 @@

function toQuantifier(digits) {
var start = digits[0];
var stop = digits[1] ? (',' + digits[1]) : '';
let start = digits[0];
let stop = digits[1] ? (',' + digits[1]) : '';
if (!stop && (!start || start === 1)) {

@@ -290,3 +290,3 @@ return '';

if (tok.isPadded) {
var diff = Math.abs(tok.maxLen - String(val).length);
let diff = Math.abs(tok.maxLen - String(val).length);
switch (diff) {

@@ -309,2 +309,3 @@ case 0:

toRegexRange.clearCache = () => (toRegexRange.cache = {});
module.exports = toRegexRange;
{
"name": "to-regex-range",
"description": "Pass two numbers, get a regex-compatible source string for matching ranges. Validated against more than 2.78 million test assertions.",
"version": "3.0.0",
"version": "4.0.0",
"homepage": "https://github.com/micromatch/to-regex-range",

@@ -21,3 +21,3 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)",

"engines": {
"node": ">=0.10.0"
"node": ">=4.0"
},

@@ -28,4 +28,3 @@ "scripts": {

"dependencies": {
"is-number": "^4.0.0",
"repeat-string": "^1.6.1"
"is-number": "^6.0.0"
},

@@ -35,3 +34,3 @@ "devDependencies": {

"gulp-format-md": "^1.0.0",
"mocha": "^3.5.0",
"mocha": "^5.2.0",
"text-table": "^0.2.0",

@@ -87,5 +86,9 @@ "time-diff": "^0.3.1"

"reflinks": [
"micromatch"
"micromatch",
"0-5",
"0-9",
"1-5",
"1-9"
]
}
}

@@ -62,3 +62,3 @@ # 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 monthly downloads](https://img.shields.io/npm/dm/to-regex-range.svg?style=flat)](https://npmjs.org/package/to-regex-range) [![NPM total downloads](https://img.shields.io/npm/dt/to-regex-range.svg?style=flat)](https://npmjs.org/package/to-regex-range) [![Linux Build Status](https://img.shields.io/travis/micromatch/to-regex-range.svg?style=flat&label=Travis)](https://travis-ci.org/micromatch/to-regex-range)

As of November 01, 2017, 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.
As of July 04, 2018, 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.

@@ -173,21 +173,40 @@ Tests run in ~870ms on my MacBook Pro, 2.5 GHz Intel Core i7.

| --- | --- | --- |
| `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_ |
| `toRegexRange('5, 5')` | `5` | _15μs_
|
| `toRegexRange('5, 6')` | `5\ | 6` | _21μs_
|
| `toRegexRange('29, 51')` | `29\ | [34](https://github.com//0-9)\ | 5[01]` | _314μs_
|
| `toRegexRange('31, 877')` | `3[1-9]\ | [4-9](https://github.com//0-9)\ | [1-7](https://github.com//0-92)\ | 8[0-6](https://github.com//0-9)\ | 87[0-7]` | _88μs_
|
| `toRegexRange('111, 555')` | `11[1-9]\ | 1[2-9](https://github.com//0-9)\ | [2-4](https://github.com//0-92)\ | 5[0-4](https://github.com//0-9)\ | 55[0-5]` | _72μs_
|
| `toRegexRange('-10, 10')` | `-[1-9]\ | -?10\ | [0-9](https://github.com//0-9)` | _67μs_
|
| `toRegexRange('-100, -10')` | `-1[0-9](https://github.com//0-9)\ | -[2-9](https://github.com//0-9)\ | -100` | _53μs_
|
| `toRegexRange('-100, 100')` | `-[1-9]\ | -?[1-9](https://github.com//0-9)\ | -?100\ | [0-9](https://github.com//0-9)` | _68μs_
|
| `toRegexRange('001, 100')` | `0{2}[1-9]\ | 0[1-9](https://github.com//0-9)\ | 100` | _121μs_
|
| `toRegexRange('0010, 1000')` | `0{2}1[0-9](https://github.com//0-9)\ | 0{2}[2-9](https://github.com//0-9)\ | 0[1-9](https://github.com//0-92)\ | 1000` | _73μs_
|
| `toRegexRange('1, 2')` | `1\ | 2` | _10μs_
|
| `toRegexRange('1, 5')` | `[1-5]` | _26μs_
|
| `toRegexRange('1, 10')` | `[1-9]\ | 10` | _33μs_
|
| `toRegexRange('1, 100')` | `[1-9]\ | [1-9](https://github.com//0-9)\ | 100` | _67μs_
|
| `toRegexRange('1, 1000')` | `[1-9]\ | [1-9](https://github.com//0-91,2)\ | 1000` | _50μs_
|
| `toRegexRange('1, 10000')` | `[1-9]\ | [1-9](https://github.com//0-91,3)\ | 10000` | _65μs_
|
| `toRegexRange('1, 100000')` | `[1-9]\ | [1-9](https://github.com//0-91,4)\ | 100000` | _69μs_
|
| `toRegexRange('1, 1000000')` | `[1-9]\ | [1-9](https://github.com//0-91,5)\ | 1000000` | _74μs_
|
| `toRegexRange('1, 10000000')` | `[1-9]\ | [1-9](https://github.com//0-91,6)\ | 10000000` | _86μs_
|

@@ -280,3 +299,3 @@ ## Heads up!

| --- | --- |
| 51 | [jonschlinkert](https://github.com/jonschlinkert) |
| 56 | [jonschlinkert](https://github.com/jonschlinkert) |
| 2 | [realityking](https://github.com/realityking) |

@@ -288,8 +307,9 @@

* [github/jonschlinkert](https://github.com/jonschlinkert)
* [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
* [LinkedIn Profile](https://linkedin.com/in/jonschlinkert)
* [GitHub Profile](https://github.com/jonschlinkert)
* [Twitter Profile](https://twitter.com/jonschlinkert)
### License
Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).
Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert).
Released under the [MIT License](LICENSE).

@@ -299,2 +319,2 @@

_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on November 01, 2017._
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on July 04, 2018._

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc