concat-regexp
Advanced tools
Comparing version 0.0.4 to 0.0.5
49
index.js
var slice = Array.prototype.slice | ||
var boundary = /(^\/|\/$)/g | ||
var modifier = /(^|\/)([migy]*)?$/ | ||
var boundary = /(^\/|\/$)/ | ||
var modifier = /(^|\/)([migy]+)$/ | ||
module.exports = function() { | ||
var pieces = slice.call(arguments) | ||
var piece = undefined | ||
var modifiers = undefined | ||
var regexp = '' | ||
var i = 0 | ||
var piece | ||
var isRegExp | ||
var modifiers | ||
var i = -1 | ||
var last = pieces.length - 1 | ||
var loner = last === i | ||
for (; i <= last; i++) { | ||
while (i++ < last) { | ||
// Be kind, tolerate undefined | ||
@@ -21,26 +21,21 @@ if (pieces[i] === undefined) { | ||
// Remove forward-slash boundaries | ||
piece = pieces[i].toString() | ||
isRegExp = pieces[i] instanceof RegExp | ||
// Don’t strip the boundaries of | ||
// lone regular expressions | ||
if (loner && boundary.test(piece)) { | ||
regexp += piece | ||
continue | ||
} | ||
piece = isRegExp ? | ||
pieces[i].source : | ||
pieces[i].toString() | ||
// Add sanitised piece | ||
if (i < last) { | ||
regexp += piece.replace(boundary, '') | ||
continue | ||
} | ||
// Strip modifiers | ||
regexp += piece.replace(modifier, '$1') | ||
// Find modifiers in the last piece | ||
regexp += piece | ||
.replace(modifier, '') | ||
.replace(boundary, '') | ||
// Set modifiers | ||
if (i === last) { | ||
if (!isRegExp && boundary.test(pieces[i])) { | ||
continue | ||
} | ||
modifiers = (piece.match(modifier) || []) | ||
.slice(2) | ||
.join('') | ||
modifiers = (pieces[i].toString().match(modifier) || []) | ||
.slice(2) | ||
.join('') | ||
} | ||
} | ||
@@ -47,0 +42,0 @@ |
{ | ||
"name": "concat-regexp", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "A function that takes a series of regular expressions and returns them in concatenated form.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -38,10 +38,20 @@ var run = require('tape') | ||
run('it handles lone regular expressions like RegExp', function(test) { | ||
run('it doesn’t treat forward slashes in strings as pattern boundaries', function(test) { | ||
test.deepEqual( | ||
concat('/a/'), | ||
RegExp('/a/'), | ||
RegExp('/a/').toString() | ||
concat('', '/a/').source, | ||
'/a/', | ||
'/a/' | ||
) | ||
test.notOk( | ||
concat('', '/a/i').test('/A/'), | ||
'and it avoids false-positive modifiers' | ||
) | ||
test.ok( | ||
concat('', '/a/', 'i').test('/A/'), | ||
'and it avoids false-positive modifiers' | ||
) | ||
test.end() | ||
}) |
3877
81