[Pkg-javascript-commits] [node-to-regex-range] 04/07: New upstream version 3.0.0
Julien Puydt
julien.puydt at laposte.net
Sat Nov 25 16:46:27 UTC 2017
This is an automated email from the git hooks/post-receive script.
jpuydt-guest pushed a commit to branch master
in repository node-to-regex-range.
commit 65a8572336d3f4027a8d366018f677f534260618
Author: Julien Puydt <julien.puydt at laposte.net>
Date: Sat Nov 25 17:28:55 2017 +0100
New upstream version 3.0.0
---
.editorconfig | 14 +++
.eslintrc.json | 117 ++++++++++++++++++
.gitattributes | 10 ++
.gitignore | 29 +++++
.travis.yml | 15 +++
.verb.md | 196 +++++++++++++++++++++++++++++++
bower.json | 63 ++++++++++
examples.js | 74 ++++++++++++
test/test.js | 365 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
9 files changed, 883 insertions(+)
diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 0000000..449f0da
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,14 @@
+# http://editorconfig.org/
+root = true
+
+[*]
+charset = utf-8
+end_of_line = lf
+indent_size = 2
+indent_style = space
+insert_final_newline = true
+trim_trailing_whitespace = true
+
+[{**/{actual,fixtures,expected,templates}/**,*.md}]
+trim_trailing_whitespace = false
+insert_final_newline = false
diff --git a/.eslintrc.json b/.eslintrc.json
new file mode 100644
index 0000000..61e8895
--- /dev/null
+++ b/.eslintrc.json
@@ -0,0 +1,117 @@
+{
+ "env": {
+ "browser": false,
+ "es6": true,
+ "node": true,
+ "mocha": true
+ },
+
+ "globals": {
+ "document": false,
+ "navigator": false,
+ "window": false
+ },
+
+ "rules": {
+ "accessor-pairs": 2,
+ "arrow-spacing": [2, { "before": true, "after": true }],
+ "block-spacing": [2, "always"],
+ "brace-style": [2, "1tbs", { "allowSingleLine": true }],
+ "comma-dangle": [2, "never"],
+ "comma-spacing": [2, { "before": false, "after": true }],
+ "comma-style": [2, "last"],
+ "constructor-super": 2,
+ "curly": [2, "multi-line"],
+ "dot-location": [2, "property"],
+ "eol-last": 2,
+ "eqeqeq": [2, "allow-null"],
+ "generator-star-spacing": [2, { "before": true, "after": true }],
+ "handle-callback-err": [2, "^(err|error)$" ],
+ "indent": [2, 2, { "SwitchCase": 1 }],
+ "key-spacing": [2, { "beforeColon": false, "afterColon": true }],
+ "keyword-spacing": [2, { "before": true, "after": true }],
+ "new-cap": [2, { "newIsCap": true, "capIsNew": false }],
+ "new-parens": 2,
+ "no-array-constructor": 2,
+ "no-caller": 2,
+ "no-class-assign": 2,
+ "no-cond-assign": 2,
+ "no-const-assign": 2,
+ "no-control-regex": 2,
+ "no-debugger": 2,
+ "no-delete-var": 2,
+ "no-dupe-args": 2,
+ "no-dupe-class-members": 2,
+ "no-dupe-keys": 2,
+ "no-duplicate-case": 2,
+ "no-empty-character-class": 2,
+ "no-eval": 2,
+ "no-ex-assign": 2,
+ "no-extend-native": 2,
+ "no-extra-bind": 2,
+ "no-extra-boolean-cast": 2,
+ "no-extra-parens": [2, "functions"],
+ "no-fallthrough": 2,
+ "no-floating-decimal": 2,
+ "no-func-assign": 2,
+ "no-implied-eval": 2,
+ "no-inner-declarations": [2, "functions"],
+ "no-invalid-regexp": 2,
+ "no-irregular-whitespace": 2,
+ "no-iterator": 2,
+ "no-label-var": 2,
+ "no-labels": 2,
+ "no-lone-blocks": 2,
+ "no-mixed-spaces-and-tabs": 2,
+ "no-multi-spaces": 2,
+ "no-multi-str": 2,
+ "no-multiple-empty-lines": [2, { "max": 1 }],
+ "no-native-reassign": 0,
+ "no-negated-in-lhs": 2,
+ "no-new": 2,
+ "no-new-func": 2,
+ "no-new-object": 2,
+ "no-new-require": 2,
+ "no-new-wrappers": 2,
+ "no-obj-calls": 2,
+ "no-octal": 2,
+ "no-octal-escape": 2,
+ "no-proto": 0,
+ "no-redeclare": 2,
+ "no-regex-spaces": 2,
+ "no-return-assign": 2,
+ "no-self-compare": 2,
+ "no-sequences": 2,
+ "no-shadow-restricted-names": 2,
+ "no-spaced-func": 2,
+ "no-sparse-arrays": 2,
+ "no-this-before-super": 2,
+ "no-throw-literal": 2,
+ "no-trailing-spaces": 0,
+ "no-undef": 2,
+ "no-undef-init": 2,
+ "no-unexpected-multiline": 2,
+ "no-unneeded-ternary": [2, { "defaultAssignment": false }],
+ "no-unreachable": 2,
+ "no-unused-vars": [2, { "vars": "all", "args": "none" }],
+ "no-useless-call": 0,
+ "no-with": 2,
+ "one-var": [0, { "initialized": "never" }],
+ "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }],
+ "padded-blocks": [0, "never"],
+ "quotes": [2, "single", "avoid-escape"],
+ "radix": 2,
+ "semi": [2, "always"],
+ "semi-spacing": [2, { "before": false, "after": true }],
+ "space-before-blocks": [2, "always"],
+ "space-before-function-paren": [2, "never"],
+ "space-in-parens": [2, "never"],
+ "space-infix-ops": 2,
+ "space-unary-ops": [2, { "words": true, "nonwords": false }],
+ "spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }],
+ "use-isnan": 2,
+ "valid-typeof": 2,
+ "wrap-iife": [2, "any"],
+ "yoda": [2, "never"]
+ }
+}
diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 0000000..660957e
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1,10 @@
+# Enforce Unix newlines
+* text eol=lf
+
+# binaries
+*.ai binary
+*.psd binary
+*.jpg binary
+*.gif binary
+*.png binary
+*.jpeg binary
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..4bf0a60
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,29 @@
+# always ignore files
+*.DS_Store
+.idea
+*.sublime-*
+
+# test related, or directories generated by tests
+test/actual
+actual
+coverage
+.nyc*
+
+# npm
+node_modules
+npm-debug.log
+
+# yarn
+yarn.lock
+yarn-error.log
+
+# misc
+_gh_pages
+_draft
+_drafts
+bower_components
+vendor
+temp
+tmp
+TODO.md
+package-lock.json
\ No newline at end of file
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..1686664
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,15 @@
+sudo: false
+os:
+ - linux
+ - osx
+language: node_js
+node_js:
+ - node
+ - '9'
+ - '8'
+ - '7'
+ - '6'
+ - '5'
+ - '4'
+ - '0.12'
+ - '0.10'
diff --git a/.verb.md b/.verb.md
new file mode 100644
index 0000000..cb59dcb
--- /dev/null
+++ b/.verb.md
@@ -0,0 +1,196 @@
+<details>
+ <summary><strong>What does this do?</strong></summary>
+
+<br>
+
+This libary generates the `source` string to be passed to `new RegExp()` for matching a range of numbers.
+
+**Example**
+
+```js
+var toRegexRange = require('{%= name %}');
+var regex = new RegExp(toRegexRange('15', '95'));
+```
+
+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).
+
+<br>
+
+</details>
+
+<details>
+ <summary><strong>Why use this library?</strong></summary>
+
+<br>
+
+### Convenience
+
+Creating regular expressions for matching numbers gets deceptively complicated pretty fast.
+
+For example, let's say you need a validation regex for matching part of a user-id, postal code, social security number, tax id, etc:
+
+- regex for matching `1` => `/1/` (easy enough)
+- regex for matching `1` through `5` => `/[1-5]/` (not bad...)
+- regex for matching `1` or `5` => `/(1|5)/` (still easy...)
+- regex for matching `1` through `50` => `/([1-9]|[1-4][0-9]|50)/` (uh-oh...)
+- regex for matching `1` through `55` => `/([1-9]|[1-4][0-9]|5[0-5])/` (no prob, I can do this...)
+- regex for matching `1` through `555` => `/([1-9]|[1-9][0-9]|[1-4][0-9]{2}|5[0-4][0-9]|55[0-5])/` (maybe not...)
+- regex for matching `0001` through `5555` => `/(0{3}[1-9]|0{2}[1-9][0-9]|0[1-9][0-9]{2}|[1-4][0-9]{3}|5[0-4][0-9]{2}|55[0-4][0-9]|555[0-5])/` (okay, I get the point!)
+
+The numbers are contrived, but they're also really basic. In the real world you might need to generate a regex on-the-fly for validation.
+
+**Learn more**
+
+If you're interested in learning more about [character classes](http://www.regular-expressions.info/charclass.html) and other regex features, I personally have always found [regular-expressions.info](http://www.regular-expressions.info/charclass.html) to be pretty useful.
+
+
+### Heavily tested
+
+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.
+
+Tests run in ~870ms on my MacBook Pro, 2.5 GHz Intel Core i7.
+
+### Highly optimized
+
+Generated regular expressions are highly optimized:
+- duplicate sequences and character classes are reduced using quantifiers
+- smart enough to use `?` conditionals when number(s) or range(s) can be positive or negative
+- uses fragment caching to avoid processing the same exact string more than once
+
+<br>
+
+</details>
+
+## Usage
+
+Add this library to your javascript application with the following line of code
+
+```js
+var toRegexRange = require('{%= name %}');
+```
+
+The main export is a function that takes two integers: the `min` value and `max` value (formatted as strings or numbers).
+
+```js
+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
+```
+
+## Options
+
+### options.capture
+
+**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.
+
+```js
+console.log(toRegexRange('-10', '10'));
+//=> -[1-9]|-?10|[0-9]
+
+console.log(toRegexRange('-10', '10', {capture: true}));
+//=> (-[1-9]|-?10|[0-9])
+```
+
+### options.shorthand
+
+**Type**: `boolean`
+
+**Deafault**: `undefined`
+
+Use the regex shorthand for `[0-9]`:
+
+```js
+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}
+```
+
+### options.relaxZeros
+
+**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*`.
+
+```js
+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
+```
+
+<details>
+ <summary><strong>Why are zeros relaxed for negative zero-padded ranges by default?</strong></summary>
+
+Consider the following.
+
+```js
+var regex = toRegexRange('-001', '100');
+```
+
+_Note that `-001` and `100` are both three digits long_.
+
+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).
+
+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`.
+
+Thus, we relax zeros by default to provide a more predictable experience for users.
+
+</details>
+
+## Examples
+
+{%= examples() %}
+
+## Heads up!
+
+**Order of arguments**
+
+When the `min` is larger than the `max`, values will be flipped to create a valid range:
+
+```js
+toRegexRange('51', '29');
+```
+
+Is effectively flipped to:
+
+```js
+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.
+
+
+## History
+
+### v2.0.0 - 2017-04-21
+
+**New features**
+
+Adds support for zero-padding!
+
+### v1.0.0
+
+**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.
+
+## Attribution
+
+Inspired by the python library [range-regex](https://github.com/dimka665/range-regex).
diff --git a/bower.json b/bower.json
new file mode 100644
index 0000000..945e348
--- /dev/null
+++ b/bower.json
@@ -0,0 +1,63 @@
+{
+ "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.",
+ "repository": "micromatch/to-regex-range",
+ "license": "MIT",
+ "homepage": "https://github.com/micromatch/to-regex-range",
+ "authors": [
+ "Jon Schlinkert (https://github.com/jonschlinkert)"
+ ],
+ "main": [
+ "index.js"
+ ],
+ "dependencies": {
+ "is-number": "^4.0.0",
+ "repeat-string": "^1.6.1"
+ },
+ "devDependencies": {
+ "fill-range": "^4.0.0",
+ "gulp-format-md": "^0.1.12",
+ "mocha": "^3.2.0",
+ "text-table": "^0.2.0",
+ "time-diff": "^0.3.1"
+ },
+ "keywords": [
+ "alpha",
+ "alphabetical",
+ "bash",
+ "brace",
+ "date",
+ "expand",
+ "expansion",
+ "glob",
+ "match",
+ "matches",
+ "matching",
+ "number",
+ "numerical",
+ "range",
+ "ranges",
+ "regex",
+ "sequence",
+ "sh",
+ "to",
+ "year"
+ ],
+ "version": "2.1.1",
+ "bugs": {
+ "url": "https://github.com/micromatch/to-regex-range/issues"
+ },
+ "files": [
+ "index.js"
+ ],
+ "ignore": [
+ "actual",
+ "bower_components",
+ "fixtures",
+ "node_modules",
+ "temp",
+ "test",
+ "test.js",
+ "tmp"
+ ]
+}
\ No newline at end of file
diff --git a/examples.js b/examples.js
new file mode 100644
index 0000000..b9fd767
--- /dev/null
+++ b/examples.js
@@ -0,0 +1,74 @@
+'use strict';
+
+var toRegexRange = require('./');
+var table = require('text-table');
+var Time = require('time-diff');
+var time = new Time();
+
+/**
+ * $ node examples.js
+ */
+
+function toRange(min, max) {
+ var key = 'to-range' + min + max;
+ time.start(key);
+ var str = toRegexRange(min, max);
+ return [
+ '',
+ '`toRegexRange(\'' + min + ', ' + max + '\')`',
+ '`' + str.split('|').join('\\|') + '`',
+ '_' + time.end(key) + '_',
+ ''
+ ];
+}
+
+toRange('1', '3');
+
+var rows = [
+ ['', '**Range**', '**Result**', '**Compile time**', ''],
+ ['', '--- ', '--- ', '---', ''],
+];
+
+var examples = [
+ // ['0001', '5555'],
+ // ['1', '5555'],
+ // ['1', '555'],
+ // ['1', '55'],
+ // ['1', '50'],
+
+ ['5', '5'],
+ ['5', '6'],
+ ['29', '51'],
+ ['31', '877'],
+ ['111', '555'],
+ ['-10', '10'],
+ ['-100', '-10'],
+ ['-100', '100'],
+ ['001', '100'],
+ ['0010', '1000'],
+ ['1', '2'],
+ ['1', '5'],
+ ['1', '10'],
+ ['1', '100'],
+ ['1', '1000'],
+ ['1', '10000'],
+ ['1', '100000'],
+ ['1', '1000000'],
+ ['1', '10000000'],
+].forEach(function(args) {
+ rows.push(toRange.apply(null, args));
+});
+
+var text = table(rows, {hsep: ' | '});
+console.log(text);
+
+/**
+ * This method is exposed as a helper, which is picked up
+ * by verb and used in the .verb.md readme template
+ */
+
+module.exports = function() {
+ return text.split('\n').map(function(line) {
+ return line.replace(/^ +/, '');
+ }).join('\n');
+};
diff --git a/test/test.js b/test/test.js
new file mode 100644
index 0000000..8a0b7f7
--- /dev/null
+++ b/test/test.js
@@ -0,0 +1,365 @@
+'use strict';
+
+require('mocha');
+var assert = require('assert');
+var fill = require('fill-range');
+var toRange = require('..');
+var count = 0;
+
+function toRangeRegex(min, max, options) {
+ return toRegex(toRange(min, max, options));
+}
+
+function toRegex(str) {
+ return new RegExp('^(?:' + str + ')$');
+}
+
+function match(min, max) {
+ var regex = toRangeRegex(min, max);
+ return function(num) {
+ return regex.test(String(num));
+ };
+}
+
+function matchRange(min, max, expected, match, notMatch) {
+ if (max - min >= 1000000) {
+ throw new RangeError('range is too big');
+ }
+
+ var actual = toRange(min, max);
+ var msg = actual + ' => ' + expected;
+
+ // test expected string
+ assert.strictEqual(actual, expected, msg);
+
+ var re = toRegex(actual);
+ for (var i = 0; i < match.length; i++) {
+ assert(re.test(match[i]), 'should match ' + msg);
+ count++;
+ }
+
+ if (!Array.isArray(notMatch)) return;
+ for (var j = 0; j < notMatch.length; j++) {
+ assert(!re.test(notMatch[j]), 'should not match ' + msg);
+ count++;
+ }
+}
+
+function verifyRange(min, max, from, to, zeros) {
+ var isMatch = match(min, max);
+ var minNum = Math.min(min, max);
+ var maxNum = Math.max(min, max);
+ var num = from - 1;
+
+ while (++num < to) {
+ var n = Number(num);
+ if (inRange(minNum, maxNum, n)) {
+ assert(isMatch(num), 'should match "' + num + '"');
+ } else {
+ assert(!isMatch(num), 'should not match "' + num + '"');
+ }
+ count++;
+ }
+}
+
+function verifyZeros(min, max, from, to) {
+ var range = fill(from, to);
+ var len = range.length;
+ var idx = -1;
+
+ var isMatch = match(min, max);
+ var minNum = Math.min(min, max);
+ var maxNum = Math.max(min, max);
+
+ while (++idx < len) {
+ var num = range[idx];
+ var n = Number(num);
+ if (inRange(minNum, maxNum, n)) {
+ assert(isMatch(num), 'should match "' + num + '"');
+ } else {
+ assert(!isMatch(num), 'should not match "' + num + '"');
+ }
+ count++;
+ }
+}
+
+function inRange(min, max, num) {
+ return min <= num && max >= num;
+}
+
+describe('to-regex-range', function() {
+ after(function() {
+ var num = (+(+(count).toFixed(2))).toLocaleString();
+ console.log();
+ console.log(' ', num, 'values tested');
+ });
+
+ describe('range', function() {
+ it('should throw an error when the first arg is invalid:', function() {
+ assert.throws(function() {
+ toRange();
+ }, /toRegexRange: first argument is invalid/);
+ });
+
+ it('should throw an error when the second arg is invalid:', function() {
+ assert.throws(function() {
+ toRange(1, {});
+ }, /toRegexRange: second argument is invalid/);
+ });
+ });
+
+ describe('minimum / maximum', function() {
+ it('should reverse `min/max` when the min is larger than the max:', function() {
+ assert.strictEqual(toRange(55, 10), '1[0-9]|[2-4][0-9]|5[0-5]');
+ });
+ });
+
+ describe('ranges', function() {
+ it('should return the number when only one argument is passed:', function() {
+ assert.strictEqual(toRange(5), '5');
+ });
+
+ it('should not return a range when both numbers are the same:', function() {
+ assert.strictEqual(toRange(5, 5), '5');
+ });
+
+ it('should support ranges than 10:', function() {
+ assert.strictEqual(toRange(1, 5), '[1-5]');
+ });
+
+ it('should support strings:', function() {
+ assert.strictEqual(toRange('1', '5'), '[1-5]');
+ assert.strictEqual(toRange('10', '50'), '1[0-9]|[2-4][0-9]|50');
+ });
+
+ it('should support padded ranges:', function() {
+ assert.strictEqual(toRange('001', '005'), '0{2}[1-5]');
+ assert.strictEqual(toRange('01', '05'), '0[1-5]');
+ assert.strictEqual(toRange('001', '100'), '0{2}[1-9]|0[1-9][0-9]|100');
+ assert.strictEqual(toRange('0001', '1000'), '0{3}[1-9]|0{2}[1-9][0-9]|0[1-9][0-9]{2}|1000');
+ });
+
+ it('should work when padding is imbalanced:', function() {
+ assert.strictEqual(toRange('001', '105'), '0{2}[1-9]|0[1-9][0-9]|10[0-5]');
+ assert.strictEqual(toRange('01', '105'), '0{2}[1-9]|0[1-9][0-9]|10[0-5]');
+ assert.strictEqual(toRange('010', '105'), '01[0-9]|0[2-9][0-9]|10[0-5]');
+ 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]');
+ assert.strictEqual(toRange('0001', '1000'), toRange('001', '1000'));
+ assert.strictEqual(toRange('0001', '1000'), toRange('01', '1000'));
+ });
+
+ it('should generate regex strings for negative patterns', function() {
+ assert.strictEqual(toRange(-1, 0), '-1|0');
+ assert.strictEqual(toRange(-1, 1), '-1|[01]');
+ assert.strictEqual(toRange(-4, -2), '-[2-4]');
+ assert.strictEqual(toRange(-3, 1), '-[1-3]|[01]');
+ assert.strictEqual(toRange(-2, 0), '-[12]|0');
+ assert.strictEqual(toRange(-1, 3), '-1|[0-3]');
+ matchRange(-1, -1, '-1', [-1], [-2, 0, 1]);
+ matchRange(-1, -10, '-[1-9]|-10', [-1, -5, -10], [-11, 0]);
+ matchRange(-1, 3, '-1|[0-3]', [-1, 0, 1, 2, 3], [-2, 4]);
+ });
+
+ it('should wrap patterns when options.capture is true', function() {
+ assert.strictEqual(toRange(-1, 0, {capture: true}), '(-1|0)');
+ assert.strictEqual(toRange(-1, 1, {capture: true}), '(-1|[01])');
+ assert.strictEqual(toRange(-4, -2, {capture: true}), '-[2-4]');
+ assert.strictEqual(toRange(-3, 1, {capture: true}), '(-[1-3]|[01])');
+ assert.strictEqual(toRange(-2, 0, {capture: true}), '(-[12]|0)');
+ assert.strictEqual(toRange(-1, 3, {capture: true}), '(-1|[0-3])');
+ });
+
+ it('should generate regex strings for positive patterns', function() {
+ assert.strictEqual(toRange(1, 1), '1');
+ assert.strictEqual(toRange(0, 1), '0|1');
+ assert.strictEqual(toRange(0, 2), '[0-2]');
+ assert.strictEqual(toRange(65666, 65667), '65666|65667');
+ 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]');
+ 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]');
+ assert.strictEqual(toRange(1, 10), '[1-9]|10');
+ assert.strictEqual(toRange(1, 19), '[1-9]|1[0-9]');
+ assert.strictEqual(toRange(1, 99), '[1-9]|[1-9][0-9]');
+ assert.strictEqual(toRange(1, 100), '[1-9]|[1-9][0-9]|100');
+ assert.strictEqual(toRange(1, 1000), '[1-9]|[1-9][0-9]{1,2}|1000');
+ assert.strictEqual(toRange(1, 10000), '[1-9]|[1-9][0-9]{1,3}|10000');
+ assert.strictEqual(toRange(1, 100000), '[1-9]|[1-9][0-9]{1,4}|100000');
+ assert.strictEqual(toRange(1, 9999999), '[1-9]|[1-9][0-9]{1,6}');
+ assert.strictEqual(toRange(99, 100000), '99|[1-9][0-9]{2,4}|100000');
+
+ matchRange(99, 100000, '99|[1-9][0-9]{2,4}|100000', [99, 999, 989, 100, 9999, 9899, 10009, 10999, 100000], [0, 9, 100001, 100009]);
+ });
+
+ it('should optimize regexes', function() {
+ assert.strictEqual(toRange(-9, 9), '-[1-9]|[0-9]');
+ assert.strictEqual(toRange(-19, 19), '-[1-9]|-?1[0-9]|[0-9]');
+ assert.strictEqual(toRange(-29, 29), '-[1-9]|-?[12][0-9]|[0-9]');
+ assert.strictEqual(toRange(-99, 99), '-[1-9]|-?[1-9][0-9]|[0-9]');
+ assert.strictEqual(toRange(-999, 999), '-[1-9]|-?[1-9][0-9]{1,2}|[0-9]');
+ assert.strictEqual(toRange(-9999, 9999), '-[1-9]|-?[1-9][0-9]{1,3}|[0-9]');
+ assert.strictEqual(toRange(-99999, 99999), '-[1-9]|-?[1-9][0-9]{1,4}|[0-9]');
+ });
+ });
+
+ describe('validate ranges', function() {
+ it('should support negative ranges:', function() {
+ verifyRange(-9, -1, -100, 100);
+ verifyRange(-99, -1, -1000, 1000);
+ verifyRange(-999, -1, -1000, 1000);
+ verifyRange(-9999, -1, -10000, 10000);
+ verifyRange(-99999, -1, -100999, 100999);
+ });
+
+ it('should support negative-to-positive ranges:', function() {
+ verifyRange(-9, 9, -100, 100);
+ verifyRange(-99, 99, -1000, 1000);
+ verifyRange(-999, 999, -1000, 1000);
+ verifyRange(-9999, 9999, -10000, 10000);
+ verifyRange(-99999, 99999, -100999, 100999);
+ });
+
+ it('should work when numbers are equal:', function() {
+ assert.strictEqual(toRange('1', '1'), '1');
+ assert.strictEqual(toRange('65443', '65443'), '65443');
+ assert.strictEqual(toRange('192', '192'), '192');
+ verifyRange(1, 1, 0, 100);
+ verifyRange(65443, 65443, 65000, 66000);
+ verifyRange(192, 192, 0, 1000);
+ });
+
+ it('should support large numbers:', function() {
+ verifyRange(100019999300000, 100020000300000, 100019999999999, 100020000200000);
+ });
+
+ it('should support large ranges:', function() {
+ verifyRange(1, 100000, 1, 1000);
+ verifyRange(1, 100000, 10000, 11000);
+ verifyRange(1, 100000, 99000, 100000);
+ verifyRange(1, 100000, 1000, 2000);
+ verifyRange(1, 100000, 10000, 12000);
+ verifyRange(1, 100000, 50000, 60000);
+ verifyRange(1, 100000, 99999, 101000);
+ verifyRange(10331, 20381, 0, 99999);
+ });
+
+ it('should support repeated digits:', function() {
+ verifyRange(111, 222, 0, 999);
+ verifyRange(111, 333, 0, 999);
+ verifyRange(111, 444, 0, 999);
+ verifyRange(111, 555, 0, 999);
+ verifyRange(111, 666, 0, 999);
+ verifyRange(111, 777, 0, 999);
+ verifyRange(111, 888, 0, 999);
+ verifyRange(111, 999, 0, 999);
+ verifyRange(0, 111, -99, 999);
+ verifyRange(0, 222, -99, 999);
+ verifyRange(0, 333, -99, 999);
+ verifyRange(0, 444, -99, 999);
+ verifyRange(0, 555, -99, 999);
+ verifyRange(0, 666, -99, 999);
+ verifyRange(0, 777, -99, 999);
+ verifyRange(0, 888, -99, 999);
+ verifyRange(0, 999, -99, 999);
+ });
+
+ it('should support repeated zeros:', function() {
+ verifyRange(10031, 20081, 0, 59999);
+ verifyRange(10000, 20000, 0, 59999);
+ });
+
+ it('should support zero one:', function() {
+ verifyRange(10301, 20101, 0, 99999);
+ verifyRange(101010, 1010101, 0, 1299999);
+ });
+
+ it('should support repeated ones:', function() {
+ verifyRange(1, 11111, 0, 1000);
+ verifyRange(1, 1111, 0, 1000);
+ verifyRange(1, 111, 0, 1000);
+ verifyRange(1, 11, 0, 1000);
+ verifyRange(1, 1, 0, 1000);
+ });
+
+ it('should support small diffs:', function() {
+ verifyRange(102, 103, 0, 1000);
+ verifyRange(102, 110, 0, 1000);
+ verifyRange(102, 130, 0, 1000);
+ });
+
+ it('should support random ranges:', function() {
+ verifyRange(4173, 7981, 0, 99999);
+ });
+
+ it('should support one digit numbers:', function() {
+ verifyRange(3, 7, 0, 99);
+ });
+
+ it('should support one digit at bounds:', function() {
+ verifyRange(1, 9, 0, 1000);
+ });
+
+ it('should support power of ten:', function() {
+ verifyRange(1000, 8632, 0, 99999);
+ });
+
+ it('should not match the negative of the same number', function() {
+ verifyRange(1, 1000, -1000, 1000);
+ verifyRange(1, 1000, '-1000', '1000');
+ });
+
+ it('should work with numbers of varying lengths:', function() {
+ verifyRange(1030, 20101, 0, 99999);
+ verifyRange(13, 8632, 0, 10000);
+ });
+
+ it('should support small ranges:', function() {
+ verifyRange(9, 11, 0, 100);
+ verifyRange(19, 21, 0, 100);
+ });
+
+ it('should support big ranges:', function() {
+ verifyRange(90, 98009, 0, 98999);
+ verifyRange(999, 10000, 1, 20000);
+ });
+
+ it('should create valid regex ranges with zero-padding:', function() {
+ verifyZeros('001', '100', '001', 100);
+ verifyZeros('001', '100', '001', '100');
+ verifyZeros('0001', '1000', '01', 1000);
+ verifyZeros('0001', '1000', '-01', 1000);
+ verifyZeros('0001', '1000', '-099', '1000');
+ verifyZeros('0001', '1000', '-010', 1000);
+ verifyZeros('0001', '1000', '-010', 1000);
+ verifyZeros('0001', '1000', '0001', '1000');
+ verifyZeros('01', '1000', '-01', '1000');
+ verifyZeros('000000001', '1000', '-010', '1000');
+ verifyZeros('00000001', '1000', '-010', '1000');
+ verifyZeros('0000001', '1000', '-010', '1000');
+ verifyZeros('000001', '1000', '-010', '1000');
+ verifyZeros('00001', '1000', '-010', '1000');
+ verifyZeros('0001', '1000', '-010', '1000');
+ verifyZeros('001', '1000', '-010', '1000');
+ verifyZeros('01', '1000', '-010', '1000');
+ verifyZeros('0001', '1000', '-010', '1000');
+ });
+
+ it('should create valid regex ranges with negative padding:', function() {
+ verifyZeros('-00001', '-1000', -1000, 1000);
+ verifyZeros('-0001', '-1000', -1000, 1000);
+ verifyZeros('-001', '-1000', -1000, 1000);
+ verifyZeros('-01', '-1000', -1000, 1000);
+ });
+
+ it('should create valid ranges with neg && pos zero-padding:', function() {
+ verifyZeros('-01', '10', '-1', '01');
+ verifyZeros('-1000', '100', -1000, 1000);
+ verifyZeros('-1000', '0100', '-010', '1000');
+ verifyZeros('-0100', '100', '-01', '100');
+ verifyZeros('-010', '100', '-01', '100');
+ verifyZeros('-01', '100', '-01', '100');
+ verifyZeros('-01000', '1000', '-010', '1000');
+ verifyZeros('-0100', '1000', '-010', '1000');
+ verifyZeros('-010', '1000', '-010', '1000');
+ verifyZeros('-01', '1000', '-010', '1000');
+ });
+ });
+});
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-to-regex-range.git
More information about the Pkg-javascript-commits
mailing list