verbose-regexp
Advanced tools
Comparing version 1.0.0 to 2.0.0
@@ -10,10 +10,10 @@ "use strict"; | ||
} | ||
exports.rx = ((template, ...args) => new RegExp(parse(template, ...args))); | ||
const FLAGS = 'dgimsuy'; | ||
for (let i = 1; i < (1 << FLAGS.length); i++) { | ||
const flags = FLAGS.replace(/./g, (char, offset) => (i & (1 << offset)) > 0 ? char : ''); | ||
exports.rx[flags] = function (template, ...args) { | ||
return new RegExp(parse(template, ...args), flags); | ||
}; | ||
} | ||
const _rx = ((template, ...args) => new RegExp(parse(template, ...args))); | ||
exports.rx = new Proxy(_rx, { | ||
get(target, flags) { | ||
return function (template, ...args) { | ||
return new RegExp(parse(template, ...args), flags); | ||
}; | ||
} | ||
}); | ||
//# sourceMappingURL=index.js.map |
@@ -10,4 +10,4 @@ "use strict"; | ||
it('should compile regular expressions', () => { | ||
const regexp = __1.rx `hello`; | ||
assert_1.default(regexp instanceof RegExp); | ||
const regexp = (0, __1.rx) `hello`; | ||
(0, assert_1.default)(regexp instanceof RegExp); | ||
assert_1.default.strictEqual(regexp.source, 'hello'); | ||
@@ -17,3 +17,3 @@ assert_1.default.strictEqual(regexp.flags, ''); | ||
it('should ignore verbose white space', () => { | ||
const regexp = __1.rx ` | ||
const regexp = (0, __1.rx) ` | ||
@@ -28,3 +28,3 @@ | ||
`; | ||
assert_1.default(regexp instanceof RegExp); | ||
(0, assert_1.default)(regexp instanceof RegExp); | ||
assert_1.default.strictEqual(regexp.source, 'hellothere'); | ||
@@ -34,4 +34,4 @@ assert_1.default.strictEqual(regexp.flags, ''); | ||
it('should not ignore in-line white space', () => { | ||
const regexp = __1.rx ` hello there `; | ||
assert_1.default(regexp instanceof RegExp); | ||
const regexp = (0, __1.rx) ` hello there `; | ||
(0, assert_1.default)(regexp instanceof RegExp); | ||
assert_1.default.strictEqual(regexp.source, ' hello there '); | ||
@@ -41,3 +41,3 @@ assert_1.default.strictEqual(regexp.flags, ''); | ||
it('should allow comments', () => { | ||
const regexp = __1.rx ` | ||
const regexp = (0, __1.rx) ` | ||
// pay me no heed | ||
@@ -52,3 +52,3 @@ | ||
`; | ||
assert_1.default(regexp instanceof RegExp); | ||
(0, assert_1.default)(regexp instanceof RegExp); | ||
assert_1.default.strictEqual(regexp.source, 'hellothere'); | ||
@@ -58,4 +58,4 @@ assert_1.default.strictEqual(regexp.flags, ''); | ||
it('should preserve backslashes', () => { | ||
const regexp = __1.rx `jam\x20tomorrow`; | ||
assert_1.default(regexp instanceof RegExp); | ||
const regexp = (0, __1.rx) `jam\x20tomorrow`; | ||
(0, assert_1.default)(regexp instanceof RegExp); | ||
assert_1.default.strictEqual(regexp.source, 'jam\\x20tomorrow'); | ||
@@ -68,3 +68,3 @@ assert_1.default.strictEqual(regexp.flags, ''); | ||
const regexp = __1.rx.i `hello ${foo}! Good ${bar}.`; | ||
assert_1.default(regexp instanceof RegExp); | ||
(0, assert_1.default)(regexp instanceof RegExp); | ||
assert_1.default.strictEqual(regexp.source, 'hello folks! Good evening.'); | ||
@@ -75,3 +75,3 @@ assert_1.default.strictEqual(regexp.flags, 'i'); | ||
const regexp = __1.rx.i `hello`; | ||
assert_1.default(regexp instanceof RegExp); | ||
(0, assert_1.default)(regexp instanceof RegExp); | ||
assert_1.default.strictEqual(regexp.source, 'hello'); | ||
@@ -82,8 +82,17 @@ assert_1.default.strictEqual(regexp.flags, 'i'); | ||
const regexp = __1.rx.gi `hello`; | ||
assert_1.default(regexp instanceof RegExp); | ||
(0, assert_1.default)(regexp instanceof RegExp); | ||
assert_1.default.strictEqual(regexp.source, 'hello'); | ||
assert_1.default.strictEqual(regexp.flags, 'gi'); | ||
}); | ||
it('should allow multiple flags in any order', () => { | ||
const regexp = __1.rx.ig `hello`; | ||
(0, assert_1.default)(regexp instanceof RegExp); | ||
assert_1.default.strictEqual(regexp.source, 'hello'); | ||
assert_1.default.strictEqual(regexp.flags, 'gi'); | ||
}); | ||
it('should reject invalid regex flags', () => { | ||
assert_1.default.throws(() => __1.rx.x `hello`, SyntaxError); | ||
}); | ||
it('should work with the date example', () => { | ||
const dateTime = __1.rx ` | ||
const dateTime = (0, __1.rx) ` | ||
(\d{4}-\d{2}-\d{2}) // date | ||
@@ -94,3 +103,3 @@ T // time separator | ||
const result = dateTime.exec(new Date('2020-05-24T12:34:56Z').toISOString()); | ||
assert_1.default(result !== null); | ||
(0, assert_1.default)(result !== null); | ||
assert_1.default.strictEqual(result[1], '2020-05-24'); | ||
@@ -106,3 +115,3 @@ assert_1.default.strictEqual(result[2], '12:34:56'); | ||
const result = url.exec(' https://example.com/foo'); | ||
assert_1.default(result !== null); | ||
(0, assert_1.default)(result !== null); | ||
assert_1.default.strictEqual(result[1], 'https'); | ||
@@ -109,0 +118,0 @@ assert_1.default.strictEqual(result[2], 'example.com'); |
{ | ||
"name": "verbose-regexp", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "Verbose Regular Expressions", | ||
@@ -34,11 +34,11 @@ "main": "./lib/index.js", | ||
"engines": { | ||
"node": ">=10" | ||
"node": ">=14" | ||
}, | ||
"devDependencies": { | ||
"@types/mocha": "^8.2.2", | ||
"@types/node": "^15.6.0", | ||
"mocha": "^8.4.0", | ||
"ts-standard": "^10.0.0", | ||
"typescript": "^4.2.4" | ||
"@types/mocha": "^9.1.1", | ||
"@types/node": "^16.11.33", | ||
"mocha": "^10.0.0", | ||
"ts-standard": "^11.0.0", | ||
"typescript": "^4.6.4" | ||
} | ||
} |
@@ -31,7 +31,2 @@ # verbose-regexp | ||
const alpha = rx.i`[a-z]+` | ||
``` | ||
Note that if you use multiple flags, they must be in alphabetical order: | ||
```javascript | ||
const allAlpha = rx.gi`[a-z]+` | ||
@@ -50,1 +45,12 @@ ``` | ||
``` | ||
## History | ||
### 2.0.0 (2022-05-03) | ||
* Use 'Proxy' objects to avoid having to create static properties for | ||
every permutation of the possible flags. | ||
### 1.0.0 (2021-24-05) | ||
* Iniital release. |
@@ -17,16 +17,14 @@ interface RX { | ||
export const rx: RX = ( | ||
const _rx = ( | ||
(template: TemplateStringsArray, ...args: any[] | ||
): RegExp => new RegExp(parse(template, ...args))) as RX | ||
const FLAGS = 'dgimsuy' | ||
for (let i = 1; i < (1 << FLAGS.length); i++) { | ||
const flags = FLAGS.replace( | ||
/./g, (char, offset) => (i & (1 << offset)) > 0 ? char : '') | ||
rx[flags] = function ( | ||
template: TemplateStringsArray, ...args: any[] | ||
): RegExp { | ||
return new RegExp(parse(template, ...args), flags) | ||
export const rx: RX = new Proxy(_rx, { | ||
get (target, flags: string) { | ||
return function ( | ||
template: TemplateStringsArray, ...args: any[] | ||
): RegExp { | ||
return new RegExp(parse(template, ...args), flags) | ||
} | ||
} | ||
} | ||
}) |
@@ -82,2 +82,13 @@ import assert from 'assert' | ||
it('should allow multiple flags in any order', () => { | ||
const regexp = rx.ig`hello` | ||
assert(regexp instanceof RegExp) | ||
assert.strictEqual(regexp.source, 'hello') | ||
assert.strictEqual(regexp.flags, 'gi') | ||
}) | ||
it('should reject invalid regex flags', () => { | ||
assert.throws(() => rx.x`hello`, SyntaxError) | ||
}) | ||
it('should work with the date example', () => { | ||
@@ -84,0 +95,0 @@ const dateTime = rx` |
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
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
30180
266
55