verbose-regexp
Advanced tools
Comparing version 2.0.0 to 3.0.0
@@ -8,3 +8,4 @@ "use strict"; | ||
function parse(template, ...subs) { | ||
return subs.reduce((result, sub, i) => result + String(sub) + trim(template.raw[i + 1]), trim(template.raw[0])); | ||
return subs.reduce((result, sub, i) => result + (sub instanceof RegExp ? `(?:${sub.source})` : String(sub)) + | ||
trim(template.raw[i + 1]), trim(template.raw[0])); | ||
} | ||
@@ -11,0 +12,0 @@ const _rx = ((template, ...args) => new RegExp(parse(template, ...args))); |
"use strict"; | ||
var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) { | ||
if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; } | ||
return cooked; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -52,3 +56,3 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
it('should preserve backslashes', () => { | ||
const regexp = (0, __1.rx) `jam\x20tomorrow`; | ||
const regexp = (0, __1.rx)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["jam tomorrow"], ["jam\\x20tomorrow"]))); | ||
(0, assert_1.default)(regexp instanceof RegExp); | ||
@@ -110,3 +114,20 @@ assert_1.default.strictEqual(regexp.source, 'jam\\x20tomorrow'); | ||
}); | ||
it('should allow embedded regexps', () => { | ||
const date = (0, __1.rx) `\d{4}-\d{2}-\d{2}`; | ||
const time = (0, __1.rx) `\d{2}:\d{2}:\d{2}`; | ||
const dateTime = (0, __1.rx) `(${date})T(${time})`; | ||
const result = dateTime.exec(new Date('2020-05-24T12:34:56Z').toISOString()); | ||
(0, assert_1.default)(result !== null); | ||
assert_1.default.strictEqual(result[1], '2020-05-24'); | ||
assert_1.default.strictEqual(result[2], '12:34:56'); | ||
}); | ||
it('embedded regexps should be atomic', () => { | ||
const aorb = (0, __1.rx) `a|b`; | ||
const aorbthenc = (0, __1.rx) `${aorb}c`; | ||
(0, assert_1.default)(aorbthenc.test('ac')); | ||
(0, assert_1.default)(aorbthenc.test('bc')); | ||
(0, assert_1.default)(!aorbthenc.test('a')); | ||
}); | ||
}); | ||
var templateObject_1; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "verbose-regexp", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"description": "Verbose Regular Expressions", | ||
@@ -37,8 +37,8 @@ "main": "./lib/index.js", | ||
"devDependencies": { | ||
"@types/mocha": "^9.1.1", | ||
"@types/node": "^16.11.33", | ||
"@types/mocha": "^10.0.1", | ||
"@types/node": "^18.16.16", | ||
"mocha": "^10.0.0", | ||
"ts-standard": "^11.0.0", | ||
"typescript": "^4.6.4" | ||
"ts-standard": "^12.0.2", | ||
"typescript": "^5.1.3" | ||
} | ||
} |
@@ -26,2 +26,25 @@ # verbose-regexp | ||
You can also embed regular expressions within each other, to allow a | ||
complicated regular expression to be built up in steps from simpler parts, | ||
e.g.: | ||
```javascript | ||
const date = rx`\d{4}-\d{2}-\d{2}` | ||
const time = rx`\d{2}:\d{2}:\d{2}` | ||
const dateTime = rx` | ||
(${date}) // date | ||
T // time separator | ||
(${time}) // time | ||
` | ||
console.log(dateTime.exec(new Date().toISOString())) | ||
``` | ||
Note that embedded sub-expressions are automatically surrounded with `(?:` | ||
and `)`, so that they are always treated as a self-contained unit. Also note | ||
that embedded sub-expressions will be run using the flags of the parent | ||
expression, regardless of what flags were on the sub-expression - so, for | ||
example, if the sub-expression had the 'case insensitive' flag set but the | ||
parent expression didn't, then the sub-expression will be executed in a | ||
case sensitive manner. | ||
You can use regular expression flags by accessing them as a property of `rx`, | ||
@@ -48,2 +71,6 @@ e.g.: | ||
### 3.0.0 (2023-06-04) | ||
* Allow embedding of other RegExp objects. | ||
### 2.0.0 (2022-05-03) | ||
@@ -54,4 +81,4 @@ | ||
### 1.0.0 (2021-24-05) | ||
### 1.0.0 (2021-05-24) | ||
* Iniital release. | ||
* Initial release. |
@@ -13,3 +13,4 @@ interface RX { | ||
(result: string, sub, i) => | ||
result + String(sub) + trim(template.raw[i + 1]), trim(template.raw[0]) | ||
result + (sub instanceof RegExp ? `(?:${sub.source})` : String(sub)) + | ||
trim(template.raw[i + 1]), trim(template.raw[0]) | ||
) | ||
@@ -16,0 +17,0 @@ } |
@@ -119,2 +119,22 @@ import assert from 'assert' | ||
}) | ||
it('should allow embedded regexps', () => { | ||
const date = rx`\d{4}-\d{2}-\d{2}` | ||
const time = rx`\d{2}:\d{2}:\d{2}` | ||
const dateTime = rx`(${date})T(${time})` | ||
const result = dateTime.exec( | ||
new Date('2020-05-24T12:34:56Z').toISOString() | ||
) | ||
assert(result !== null) | ||
assert.strictEqual(result[1], '2020-05-24') | ||
assert.strictEqual(result[2], '12:34:56') | ||
}) | ||
it('embedded regexps should be atomic', () => { | ||
const aorb = rx`a|b` | ||
const aorbthenc = rx`${aorb}c` | ||
assert(aorbthenc.test('ac')) | ||
assert(aorbthenc.test('bc')) | ||
assert(!aorbthenc.test('a')) | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
33939
307
82